Skip to content

Commit

Permalink
simplify localtime code
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Mar 5, 2025
1 parent d9648be commit 5874b21
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 52 deletions.
30 changes: 13 additions & 17 deletions app/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 6 additions & 23 deletions samples/geotag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 12 additions & 12 deletions src/crwimage_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 5874b21

Please sign in to comment.