Date-Time question on seconds since epoch

I'd like to determine the total number of seconds between a date and the epoch ('00:00:00 1970-01-01 UTC'), The date can be in a timezone that has daylight savings. The date should be converted to UTC, with appropriate daylight savings adjustments made, and then the epoch subtracted from it. This should mirror the unix GNU 'date' utility, which can be run as: % TZ="America/New_York" date --date='2004-10-04 12:14:32' +%s 1096906472 I've looked at the doc, but it's not clear how to do this. Could someone give me a few pointers, please? Thanks. Bill

On Tue, 11 Jan 2005 07:26:20 -0600, Bill Lear
I'd like to determine the total number of seconds between a date and the epoch ('00:00:00 1970-01-01 UTC'), The date can be in a timezone that has daylight savings. The date should be converted to UTC, with appropriate daylight savings adjustments made, and then the epoch subtracted from it. This should mirror the unix GNU 'date' utility, which can be run as:
% TZ="America/New_York" date --date='2004-10-04 12:14:32' +%s 1096906472
I've looked at the doc, but it's not clear how to do this. Could someone give me a few pointers, please?
I would make use of the standard library facilities timelocal or timegm to convert a struct tm to time_t format. The former assumes the time is expressed in local time (as specified by the TZ environment variable or via tzset) and the latter assumes it is in GMT. The support for timezone conversions in Boost.DateTime is rudimentary at best and does not currently make use of operating system facilities like the zoneinfo database. -- Caleb Epstein caleb dot epstein at gmail dot com

On Tue, 11 Jan 2005 09:22:06 -0500, Caleb Epstein wrote
On Tue, 11 Jan 2005 07:26:20 -0600, Bill Lear
wrote: I'd like to determine the total number of seconds between a date and the epoch ('00:00:00 1970-01-01 UTC'), The date can be in a timezone that has daylight savings. The date should be converted to UTC, with appropriate daylight savings adjustments made, and then the epoch subtracted from it. This should mirror the unix GNU 'date' utility, which can be run as:
% TZ="America/New_York" date --date='2004-10-04 12:14:32' +%s 1096906472
I've looked at the doc, but it's not clear how to do this. Could someone give me a few pointers, please?
I would make use of the standard library facilities timelocal or timegm to convert a struct tm to time_t format. The former assumes the time is expressed in local time (as specified by the TZ environment variable or via tzset) and the latter assumes it is in GMT.
The support for timezone conversions in Boost.DateTime is rudimentary at best and does not currently make use of operating system facilities like the zoneinfo database.
From the timezone you can determine the utc offset and dst_offset as well as
This is true, but in 1.33 this will be vastly improved.
In fact, the experimental version of this is already in CVS. Have a look at
libs/date_time/test/local_time/testtz_database.cpp. There you will find
testcode for a timezone database feature that will allow you do convert
directly based on a regional timezone specification (there's also a posix
timezone specification class). Bascially you load in the timezone data from a
csv file -- one is provided in the libs/date_time/data/ directory that derives
from the timezone database project.
tz_database tz_db;
tz_db.load_from_file("../data/date_time_zonespec.csv");
boost::shared_ptr

On Tue, 11 Jan 2005 09:22:06 -0500, Caleb Epstein wrote Not sure what happened to my reply 12 hours ago, but I'll try again...
On Tue, 11 Jan 2005 07:26:20 -0600, Bill Lear
wrote: I'd like to determine the total number of seconds between a date and the epoch ('00:00:00 1970-01-01 UTC'), The date can be in a timezone that has daylight savings. The date should be converted to UTC, with appropriate daylight savings adjustments made, and then the epoch subtracted from it. This should mirror the unix GNU 'date' utility, which can be run as:
% TZ="America/New_York" date --date='2004-10-04 12:14:32' +%s 1096906472
I've looked at the doc, but it's not clear how to do this. Could someone give me a few pointers, please?
I would make use of the standard library facilities timelocal or timegm to convert a struct tm to time_t format. The former assumes the time is expressed in local time (as specified by the TZ environment variable or via tzset) and the latter assumes it is in GMT.
The support for timezone conversions in Boost.DateTime is rudimentary at best and does not currently make use of operating system facilities like the zoneinfo database.
Caleb is right, the released functionality in date-time makes this difficult
to do. However, the current CVS actually has everything you need. I wrote
the following small program that does the calculation using new features which
will be released as part of 1.33:
//convert.cpp
#include "boost/date_time/local_time/local_time.hpp"
#include <iostream>
int main()
{
using namespace boost::gregorian;
using namespace boost::local_time;
using namespace boost::posix_time;
tz_database tz_db;
tz_db.load_from_file("date_time_zonespec.csv");
boost::shared_ptr

On Tue, 11 Jan 2005 19:59:35 -0700, Jeff Garland
BTW, the tz_database class is reading a file you will find in cvs at libs/date_time/data/date_time_zonespec.csv. This CSV file contains a dump of time zone database data into a form that is handing for reading, porting, and editing. This is what allows the regional time_zone specification that contains all the dst rules, etc.
From what I can tell, the functional approach you have doesn't allow for oddball historial anomalies like the ones shown below. This is
It might be nice to be able to make use of the considerable library of "tzfile" data available on many UNIX systems. This data is invaluable for getting accurate localtime values for historical dates (e.g. when DST rules were in flux). partial output from a Perl script (attached) which dumps tzfile-formatted data (see http://www.gsp.com/cgi-bin/man.cgi?section=5&topic=tzfile for the gory details): [...] Mon Feb 9 03:00:00 1942 => 2 Tue Aug 14 19:00:00 1945 => 3 [...] ttinfo[2]: gmtoff=-14400 isdst=1 abbrind=8 ttinfo[3]: gmtoff=-14400 isdst=1 abbrind=12 abbrevs: EDT, EST, EWT, EPT So for a period of time from Feb 9 1942 to Aug 14 1945, the East Coast of the US was in "EWT" which I can only assume stands for "Eastern War Time". The down-side of the tzfile data is that there is a big lookup table of all DST changeovers from 1918 until 2037. Clearly the functional approach is cleaner in the case where DST rules have been standardized. -- Caleb Epstein caleb dot epstein at gmail dot com

On Wed, 12 Jan 2005 22:00:58 -0500, Caleb Epstein wrote
It might be nice to be able to make use of the considerable library of "tzfile" data available on many UNIX systems. This data is invaluable for getting accurate localtime values for historical dates (e.g. when DST rules were in flux).
My goal was to write something that is portable, so making use of this isn't really helpful. But I believe the timezone abstraction should be sufficient to allow others to use other api's to make these adjustments if they desire.
From what I can tell, the functional approach you have doesn't allow for oddball historial anomalies like the ones shown below. This is partial output from a Perl script (attached) which dumps tzfile-formatted data (see http://www.gsp.com/cgi-bin/man.cgi?section=5&topic=tzfile for the gory details):
[...] Mon Feb 9 03:00:00 1942 => 2 Tue Aug 14 19:00:00 1945 => 3 [...] ttinfo[2]: gmtoff=-14400 isdst=1 abbrind=8 ttinfo[3]: gmtoff=-14400 isdst=1 abbrind=12 abbrevs: EDT, EST, EWT, EPT
So for a period of time from Feb 9 1942 to Aug 14 1945, the East Coast of the US was in "EWT" which I can only assume stands for "Eastern War Time". The down-side of the tzfile data is that there is a big lookup table of all DST changeovers from 1918 until 2037. Clearly the functional approach is cleaner in the case where DST rules have been standardized.
Yes, I'm aware of all this historical 'fun and games' with the dst rules. And again, I believe the time zone base class provides a sufficient abstraction to plug in these adjustments should someone choose to deal with the performance and other costs associated with handling all the historical timezones. My design choice was to focus on providing what I believe to be a couple practical solutions (regional and posix string specs) to the time zone adjustment issue for applications that deal with the current time space. Part of this comes from my belief that there are a large number of applications that need to do local adjustments for the current times and a only few applications that need to go back in time. Anyway, if you have an application for the historical cases I'm willing to work with you on writing timezone derivative and adaptor for these API's. I'd like to prove that the date-time can be extended for these cases even if it can't be portably supported. Jeff

On Wed, 12 Jan 2005 20:29:33 -0700, Jeff Garland
Part of this comes from my belief that there are a large number of applications that need to do local adjustments for the current times and a only few applications that need to go back in time.
Fair enough. I suspect you're probably right that getting a super-accurate local time for some date before the time_t epoch is not likely to be a very common requirement. -- Caleb Epstein caleb dot epstein at gmail dot com

On Tue, 11 Jan 2005 19:59:35 -0700, Jeff Garland
BTW, the tz_database class is reading a file you will find in cvs at libs/date_time/data/date_time_zonespec.csv. This CSV file contains a dump of time zone database data into a form that is handing for reading, porting, and editing. This is what allows the regional time_zone specification that contains all the dst rules, etc.
From what I can tell, the functional approach you have doesn't allow for oddball historial anomalies like the ones shown below. This is
It might be nice to be able to make use of the considerable library of "tzfile" data available on many UNIX systems. This data is invaluable for getting accurate localtime values for historical dates (e.g. when DST rules were in flux). partial output from a Perl script (attached) which dumps tzfile-formatted data (see http://www.gsp.com/cgi-bin/man.cgi?section=5&topic=tzfile for the gory details): [...] Mon Feb 9 03:00:00 1942 => 2 Tue Aug 14 19:00:00 1945 => 3 [...] ttinfo[2]: gmtoff=-14400 isdst=1 abbrind=8 ttinfo[3]: gmtoff=-14400 isdst=1 abbrind=12 abbrevs: EDT, EST, EWT, EPT So for a period of time from Feb 9 1942 to Aug 14 1945, the East Coast of the US was in "EWT" which I can only assume stands for "Eastern War Time". The down-side of the tzfile data is that there is a big lookup table of all DST changeovers from 1918 until 2037. Clearly the functional approach is cleaner in the case where DST rules have been standardized. -- Caleb Epstein caleb dot epstein at gmail dot com
participants (3)
-
Bill Lear
-
Caleb Epstein
-
Jeff Garland