diff --git a/app/actions.cpp b/app/actions.cpp index 570eb74d64..38108f11a0 100644 --- a/app/actions.cpp +++ b/app/actions.cpp @@ -75,9 +75,6 @@ int str2Tm(const std::string& timeStr, tm* tm); //! Convert a localtime to a string "YYYY:MM:DD HH:MI:SS", "" on error std::string time2Str(time_t time); -//! Convert a tm structure to a string "YYYY:MM:DD HH:MI:SS", "" on error -std::string tm2Str(const tm* tm); - /*! @brief Copy metadata from source to target according to Params::copyXyz @@ -630,7 +627,7 @@ int Rename::run(const std::string& path) { std::cerr << _("Image file creation timestamp not set in the file") << " " << path << "\n"; return 1; } - tm tm; + std::tm tm; if (str2Tm(v, &tm) != 0) { std::cerr << _("Failed to parse timestamp") << " `" << v << "' " << _("in the file") << " " << path << "\n"; return 1; @@ -1394,7 +1391,7 @@ int Adjust::adjustDateTime(Exiv2::ExifData& exifData, const std::string& key, co std::cout << " " << adjustment_ << _("s"); } } - tm tm; + std::tm tm; if (str2Tm(timeStr, &tm) != 0) { if (Params::instance().verbose_) std::cout << '\n'; @@ -1645,21 +1642,20 @@ int str2Tm(const std::string& timeStr, tm* tm) { } // str2Tm std::string time2Str(time_t time) { - auto tm = localtime(&time); - return tm2Str(tm); -} // time2Str - -std::string tm2Str(const tm* tm) { + std::tm r; +#ifdef _WIN32 + auto tm = localtime_s(&r, &time) ? nullptr : &r; +#else + auto tm = localtime_r(&time, &r); +#endif if (!tm) return ""; - std::ostringstream os; - os << std::setfill('0') << tm->tm_year + 1900 << ":" << std::setw(2) << tm->tm_mon + 1 << ":" << std::setw(2) - << tm->tm_mday << " " << std::setw(2) << tm->tm_hour << ":" << std::setw(2) << tm->tm_min << ":" << std::setw(2) - << tm->tm_sec; - - return os.str(); -} // tm2Str + const size_t m = 20; + char s[m]; + std::strftime(s, m, "%Y:%m:%d %T", tm); + return s; +} // time2Str std::string temporaryPath() { static int count = 0; diff --git a/samples/geotag.cpp b/samples/geotag.cpp index ab448bdd1b..42a7719a21 100644 --- a/samples/geotag.cpp +++ b/samples/geotag.cpp @@ -373,34 +373,17 @@ time_t parseTime(const char* arg, bool bAdjust) { // West of GMT is negative (PDT = Pacific Daylight = -07:00 == -25200 seconds int timeZoneAdjust() { - [[maybe_unused]] time_t now = time(nullptr); - int offset; - #if defined(_WIN32) TIME_ZONE_INFORMATION TimeZoneInfo; GetTimeZoneInformation(&TimeZoneInfo); - offset = -(TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias * 60); -#elif defined(__CYGWIN__) - struct tm lcopy = *localtime(&now); - time_t gmt = timegm(&lcopy); // timegm modifies lcopy - offset = (int)(((long signed int)gmt) - ((long signed int)now)); -#elif defined(OS_SOLARIS) || defined(__sun__) - struct tm local = *localtime(&now); - time_t local_tt = mktime(&local); - time_t time_gmt = mktime(gmtime(&now)); - offset = time_gmt - local_tt; + return -(TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias * 60); #else - struct tm local = *localtime(&now); - offset = local.tm_gmtoff; - -#ifdef EXIV2_DEBUG_MESSAGES - struct tm utc = *gmtime(&now); - printf("utc : offset = %6d dst = %d time = %s", 0, utc.tm_isdst, asctime(&utc)); - printf("local: offset = %6d dst = %d time = %s", offset, local.tm_isdst, asctime(&local)); - printf("timeZoneAdjust = %6d\n", offset); -#endif + std::tm r; + + auto now = std::time(nullptr); + auto local = localtime_r(&now, &r); + return local->tm_gmtoff; #endif - return offset; } std::string getExifTime(const time_t t) { diff --git a/src/crwimage_int.cpp b/src/crwimage_int.cpp index 28e09e2b4f..c10edf279f 100644 --- a/src/crwimage_int.cpp +++ b/src/crwimage_int.cpp @@ -728,22 +728,22 @@ void CrwMap::decode0x180e(const CiffComponent& ciffComponent, const CrwMapping* ULongValue v; v.read(ciffComponent.pData(), 8, byteOrder); time_t t = v.value_.at(0); - tm r; + std::tm r; #ifdef _WIN32 auto tm = localtime_s(&r, &t) ? nullptr : &r; #else auto tm = localtime_r(&t, &r); #endif - if (tm) { - const size_t m = 20; - char s[m]; - std::strftime(s, m, "%Y:%m:%d %H:%M:%S", tm); - - ExifKey key(pCrwMapping->tag_, Internal::groupName(pCrwMapping->ifdId_)); - AsciiValue value; - value.read(std::string(s)); - image.exifData().add(key, &value); - } + if (!tm) + return; + const size_t m = 20; + char s[m]; + std::strftime(s, m, "%Y:%m:%d %T", tm); + + ExifKey key(pCrwMapping->tag_, Internal::groupName(pCrwMapping->ifdId_)); + AsciiValue value; + value.read(s); + image.exifData().add(key, &value); } // CrwMap::decode0x180e void CrwMap::decode0x1810(const CiffComponent& ciffComponent, const CrwMapping* pCrwMapping, Image& image, @@ -920,7 +920,7 @@ void CrwMap::encode0x180e(const Image& image, const CrwMapping& pCrwMapping, Cif time_t t = 0; const ExifKey key(pCrwMapping.tag_, Internal::groupName(pCrwMapping.ifdId_)); if (auto ed = image.exifData().findKey(key); ed != image.exifData().end()) { - tm tm = {}; + std::tm tm = {}; if (exifTime(ed->toString().c_str(), &tm) == 0) { t = ::mktime(&tm); }