Skip to content

Commit

Permalink
fix timezone conversion (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
guruthree committed Oct 10, 2024
1 parent e8def28 commit 10d1b67
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions datebook.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ SECURE=true

## optional items

# local time zone (otherwise UTC is assumed)
#TIMEZONE="Europe/London"
# local time zone in TZ format, otherwise UTC is assumed (ETC/UTC)
# see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones for a list of time zones
#TIMEZONE="America/New_York"

# ignore calendar events from before this year (useful for very big calendars)
#FROMYEAR=2023
Expand Down
14 changes: 10 additions & 4 deletions sync-calendar2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ int main(int argc, char **argv) {
std::cout << " ==> Timezone Conversion <==" << std::endl << std::flush;

// mktime apparently reads off the TZ environment variable
// per https://rl.se/convert-utc-to-local-time
char *tz = getenv("TZ");
timezone = "TZ=" + timezone;
putenv((char*)timezone.c_str());
Expand All @@ -935,15 +936,20 @@ int main(int argc, char **argv) {
continue;
}

// per https://rl.se/convert-utc-to-local-time convert to time_t then back to gm
time_t stamp;

stamp = mktime(&Appointments[i].begin);
// timegm takes the tm struct and ignores TZ converting to time_t (assuming UTC, which it is)
// localtime take time_t and converts to a tm struct taking TZ into account
stamp = timegm(&Appointments[i].begin);
Appointments[i].begin = *localtime(&stamp);

stamp = mktime(&Appointments[i].end);
stamp = timegm(&Appointments[i].end);
Appointments[i].end = *localtime(&stamp);

// std::cout << " Summary: " << Appointments[i].description << std::endl;
// std::cout << " Start " << Appointments[i].begin.tm_zone << ": " << asctime(&Appointments[i].begin);
// std::cout << " End " << Appointments[i].end.tm_zone << ": " << asctime(&Appointments[i].end) << std::endl;

// repeat end? don't need to as it always is 23:59:59 on the day
// loop exceptions? don't need to because also only a day
}
Expand All @@ -957,7 +963,7 @@ int main(int argc, char **argv) {
putenv((char*)"TZ=");
}

std::cout << " Converted to " << timezone.substr(3) << std::endl;
std::cout << " Converted to " << timezone.substr(3) << std::endl << std::endl;
}


Expand Down

0 comments on commit 10d1b67

Please sign in to comment.