2

I would like to convert between date-times and timestamps on arbitrary time locations (eg. America/New_York)

  1. (Time location, (year, ..., sec)) ==> UTC timestamp
  2. (UTC timestamp, time location) ==> (year, ..., sec, dayOfWeek)

I'm doing to do these conversions on multiple threads and different time zones. The time locations for a given thread are not changing so I can store some time-zone structures for repeated usage.

I know that the first is ambiguous when a DST change happens (2:30 two or zero times in a day). It would be an extra if a situation like that would be reported, but it is absolutely not a priority.

Update: I would like to have a cross platform solution. Linux + Windows is fine. By arbitrary I mean that it comes from the user and has no relation to the processing machine's location.

Notinlist
  • 16,144
  • 10
  • 57
  • 99
  • An "arbitrary location" could be anything, but `America/New_York` is a specific [tzdb identifier](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). Please clarify the intent. You also need to explain what type of solution you are looking for (library? native? cross-plat? os-specific?), what you have tried already, etc. I assumed you have already *searched* for a solution - if not, start by doing that. – Matt Johnson-Pint Nov 10 '15 at 16:34
  • And... what have you tried??? – Matt Johnson-Pint Nov 10 '15 at 16:39
  • Have you read [Daylight saving time and time zone best practices](http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices)? There are specific recommendations for C++ there. – Matt Johnson-Pint Nov 10 '15 at 16:41
  • I searched the net and I did not find the post you just referred. In theory I tried to go with boost, but its 377 record long database seemed very short for me. I noticed Howard Hinnant's TZ library and I hesitated to use it, because I did not see any statement of will about future development. I noticed ICU, but I got distracted from it. I haven't seen cctz before. My search started to seem endless. – Notinlist Nov 10 '15 at 16:57

2 Answers2

3

Try this free, open source, modern parser of the complete IANA timezone database:

http://howardhinnant.github.io/date/tz.html

It requires C++11 or better. It currently requires you to download and maintain your own copy of the IANA database. This could be a blessing if you don't want to have to wait for OS platforms to update (you can be as responsive as the database maintainers).

Here is sample code:

#include <chrono>
#include <iostream>
#include "date.h"
#include "tz.h"

int
main()
{
    using namespace std::chrono_literals;
    using namespace date;
    //  Dave was born April 24, 1954. 10:03 AM pst
    //  Want to know when he is 2 Gigaseconds old
    auto birthday = make_zoned("America/Los_Angeles",
                               local_days{apr/24/1954} + 10h + 3min);
    std::cout << "born        : " << birthday << '\n';
    birthday = birthday.get_sys_time() + 2'000'000'000s;
    std::cout << "2Gs birthday: " << birthday << '\n';
}

This finds Dave's 2Gs (gigasecond) birthday, and outputs:

born        : 1954-04-24 10:03:00 PST
2Gs birthday: 2017-09-08 14:36:20 PDT

Note that all the arithmetic is done in the (implied) UTC timezone, so that changes in the local timezone ("America/Los_Angeles") are correctly accounted for. I.e. the birthday is during PST, and 2Gs later it is daylight saving time: PDT.

Tested on gcc-5.2, clang, VS-2015, and most recently, gcc-4.8.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • Hi Howard! Question: Have you tested on Windows? I assume since you provide your own tzdb that it should work there. – Matt Johnson-Pint Nov 10 '15 at 17:28
  • @MattJohnson: I was writing that update as you asked! :-) It has not been back-ported to VS-2013. – Howard Hinnant Nov 10 '15 at 17:29
  • That's ok. If it compiles on VS2015 and has no OS data dependency, then I think it's a great choice. ICU is good, but has overhead that might not be always desired. CCTZ is good too, but currently doesn't run on Windows. So your library is a great option. Thanks! – Matt Johnson-Pint Nov 10 '15 at 17:41
  • We have VS-2013. Will see if it fits. If not, then I think we should go with ICU. Can we say that if it compiles and runs then it is correct? – Notinlist Nov 11 '15 at 08:30
  • VS2013: No `constexpr`, no `inline namespace`. – Notinlist Nov 11 '15 at 09:47
  • 1
    @Notinlist: This timezone library has recently been ported to VS-2013. – Howard Hinnant Feb 02 '16 at 00:24
  • @HowardHinnant Great! The ship has sailed unfortunately. For the record we are leaning on boost for the time being, but later we will have to do something sustainable. :-) – Notinlist Feb 02 '16 at 12:50
3

You may like to have a look at Google C++ Time Zone Library:

CCTZ (C++ Time Zone) is a library for translating between absolute times and civil times (see the Fundamental Concepts section below for an explanation of these terms) using the rules defined by a time zone.

This library currently works on Linux and Mac OS X, using the standard IANA time zone data installed on the system in /usr/share/zoneinfo.

Community
  • 1
  • 1
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271