date_time: convert std::time_t to printable date in local time
Hello boost-users, I am quite new to Boost and this is my first post to this list. My problem is quite similar to one posted to this list two years ago [1]. Since there don't seem to be any follow-ups to that message I hope to find an answer by posting here. A program of mine reads a Unix timestamp from a database (the field is simply of type NUMERIC(10,0), written to by PHP's time() [2] function) into a `std::time_t time' object, holding the seconds since the epoch, in UTC. No time zone adjustments are applied as far as I understood. I need to convert this time_t to a ptime (or any other kind of printable, readable string) with time zone adjustments applied. Converting with the from_time_t() method works fine, except that the resulting date and time are still in UTC. What would be the most simple way to have the time_t converted to a ptime with local time zone adjustments? The time zone could be any but it would suffice if it is the one the computer running the program is in. I could even hardcode the time zone into the program. Hopefully I made enough sense to have someone knowing help me out. RTFM answers are most welcome, although I have read much of the docs on the date_time library on the Boost homepage. So if possible please point me to the section in the that is relevant to my problem. If this is not possible with Boost are there any other functions I could use, probably from the standard library? Thanks a lot in advance! Andreas [1] Very long Google cache link: http://www.google.com/search?q=cache:s-MYu63Ev8UJ:lists.boost.org/MailArchives/boost-users/msg07310.php+convert+time_t+to+local+time+with+time+zone+site:boost.org&hl=en&lr=&client=firefox&strip=1 [2] http://www.php.net/manual/en/function.time.php -- Andreas "daff" Ntaflos Vienna, Austria GPG Fingerprint: 6234 2E8E 5C81 C6CB E5EC 7E65 397C E2A8 090C A9B4
Try this example:
Local to UTC Conversion and vice versa:
http://tinyurl.com/mloh3
Chris
On 9/20/06, Andreas Ntaflos
Hello boost-users,
I am quite new to Boost and this is my first post to this list. My problem is quite similar to one posted to this list two years ago [1]. Since there don't seem to be any follow-ups to that message I hope to find an answer by posting here.
A program of mine reads a Unix timestamp from a database (the field is simply of type NUMERIC(10,0), written to by PHP's time() [2] function) into a `std::time_t time' object, holding the seconds since the epoch, in UTC. No time zone adjustments are applied as far as I understood.
I need to convert this time_t to a ptime (or any other kind of printable, readable string) with time zone adjustments applied. Converting with the from_time_t() method works fine, except that the resulting date and time are still in UTC.
What would be the most simple way to have the time_t converted to a ptime with local time zone adjustments? The time zone could be any but it would suffice if it is the one the computer running the program is in. I could even hardcode the time zone into the program.
Hopefully I made enough sense to have someone knowing help me out. RTFM answers are most welcome, although I have read much of the docs on the date_time library on the Boost homepage. So if possible please point me to the section in the that is relevant to my problem.
If this is not possible with Boost are there any other functions I could use, probably from the standard library?
Thanks a lot in advance!
Andreas
[1] Very long Google cache link: http://www.google.com/search?q=cache:s-MYu63Ev8UJ:lists.boost.org/MailArchives/boost-users/msg07310.php+convert+time_t+to+local+time+with+time+zone+site:boost.org&hl=en&lr=&client=firefox&strip=1 [2] http://www.php.net/manual/en/function.time.php -- Andreas "daff" Ntaflos Vienna, Austria
GPG Fingerprint: 6234 2E8E 5C81 C6CB E5EC 7E65 397C E2A8 090C A9B4
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Chris Weed wrote:
Try this example: Local to UTC Conversion and vice versa: http://tinyurl.com/mloh3
Or: http://www.boost.org/doc/html/date_time/examples.html#date_time.examples.sim... http://www.boost.org/doc/html/date_time/examples.html#date_time.examples.sec... http://www.boost.org/doc/html/date_time/local_time.html#date_time.local_time... There are now several ways to do timezone conversions and output. Hopefully one of them will cover what you need. Jeff
On Thursday 21 September 2006 01:23, Jeff Garland wrote:
Chris Weed wrote:
Try this example: Local to UTC Conversion and vice versa: http://tinyurl.com/mloh3
Or:
http://www.boost.org/doc/html/date_time/examples.html#date_time.examp les.simple_time_zone http://www.boost.org/doc/html/date_time/examples.html#date_time.examp les.seconds_since_epoch http://www.boost.org/doc/html/date_time/local_time.html#date_time.loc al_time.local_date_time
There are now several ways to do timezone conversions and output. Hopefully one of them will cover what you need.
Jeff
Thank you both for your replies! Constructing a posix_time_zone (CET with DST), a ptime from the timestamp (time_t) using from_time_t() and then creating a local_date_time object from the constructed posix_time_zone and the ptime did exactly what I initially wanted. I also considered the method shown in the Local to UTC Conversion example which uses the c_local_adjustor, but it's apparently very undocumented, which is probably why I didn't find it when searching through the docs. This method seems to have the advantage that I do not need to hard code the time zone into the program or read it from an external database file. BTW: Why would it be dangerous to rely on the machine's time zone settings? It seems to me that this would be a more robust and convenient way of applying time zone adjustments. In fact, how would I be able to run my program in, say, NYC without constructing a new time zone and recompiling it, should the company I write it for ever decide to move there? Just curious. Thanks a lot! Andreas -- Andreas "daff" Ntaflos Vienna, Austria GPG Fingerprint: 6234 2E8E 5C81 C6CB E5EC 7E65 397C E2A8 090C A9B4
Andreas Ntaflos wrote:
Thank you both for your replies!
Constructing a posix_time_zone (CET with DST), a ptime from the timestamp (time_t) using from_time_t() and then creating a local_date_time object from the constructed posix_time_zone and the ptime did exactly what I initially wanted.
I also considered the method shown in the Local to UTC Conversion example which uses the c_local_adjustor, but it's apparently very undocumented, which is probably why I didn't find it when searching through the docs. This method seems to have the advantage that I do not need to hard code the time zone into the program or read it from an external database file.
Yes, but it has other limitations.
BTW: Why would it be dangerous to rely on the machine's time zone settings? It seems to me that this would be a more robust and convenient way of applying time zone adjustments.
It's *dangerous* because it's basically impossible for a program to tell if the settings are correct. On some OS's, average users can alter these settings and so your program may fail in unexpected ways if you expect the settings to be correct. BTW, the various 'clock' classes already retrieve dates and times using the machine tz settings. The real issue here is that there is no portable way to retrieve a timezone from the machine configuration that has all of the information to construct a timezone type. That is, what *can* be determined is the current difference between UTC and local time, but not what the rules for daylight savings switchover, strings for DST versus non-DST, etc. That's why the tz database stuff if provided as a separate configurable file that you can edit for your use.
In fact, how would I be able to run my program in, say, NYC without constructing a new time zone and recompiling it, should the company I write it for ever decide to move there?
Your company would need to adopt a standard policy for machine configuration. For example, setting an environment variable on all machines that contains a POSIX timezone string. Note that if you are doing any sort of storage of dates/times in a database you really, really should use UTC if you have a system with sites in multiple timezones. Jeff
How about calling localtime() on the time_t value, and then strftime()? Moshe
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Andreas Ntaflos Sent: Thursday, September 21, 2006 12:54 AM To: boost-users@lists.boost.org Subject: [Boost-users] date_time: convert std::time_t to printable date inlocal time
Hello boost-users,
I am quite new to Boost and this is my first post to this list. My problem is quite similar to one posted to this list two years ago [1]. Since there don't seem to be any follow-ups to that message I hope to find an answer by posting here.
A program of mine reads a Unix timestamp from a database (the field is simply of type NUMERIC(10,0), written to by PHP's time() [2] function) into a `std::time_t time' object, holding the seconds since the epoch, in UTC. No time zone adjustments are applied as far as I understood.
I need to convert this time_t to a ptime (or any other kind of printable, readable string) with time zone adjustments applied. Converting with the from_time_t() method works fine, except that the resulting date and time are still in UTC.
What would be the most simple way to have the time_t converted to a ptime with local time zone adjustments? The time zone could be any but it would suffice if it is the one the computer running the program is in. I could even hardcode the time zone into the program.
Hopefully I made enough sense to have someone knowing help me out. RTFM answers are most welcome, although I have read much of the docs on the date_time library on the Boost homepage. So if possible please point me to the section in the that is relevant to my problem.
If this is not possible with Boost are there any other functions I could use, probably from the standard library?
Thanks a lot in advance!
Andreas
[1] Very long Google cache link: http://www.google.com/search?q=cache:s-MYu63Ev8UJ:lists.boost. org/MailArchives/boost-> users/msg07310.php+convert+time_t+to+local+time+with+time+zone +site:boost.org&hl=en&lr=&client=firefox&strip=1 [2] http://www.php.net/manual/en/function.time.php -- Andreas "daff" Ntaflos Vienna, Austria
GPG Fingerprint: 6234 2E8E 5C81 C6CB E5EC 7E65 397C E2A8 090C A9B4
participants (4)
-
Andreas Ntaflos
-
Chris Weed
-
Jeff Garland
-
Moshe Matitya