Prev: overloading for multiple arithmetic operations
Next: Perfect forwarding of default constructor?
From: Hei on 13 Jul 2010 11:49 Hi, I am trying to get the current time based on an input timezone with the consideration of daylight saving, and do some simple arithmetic (e.g. "add" 1 day to or "subtract" 1 day from it), Is there a function like that in std C++? I have searched online for awhile...I don't see any (besides boost)...maybe I miss it? Thanks in advance. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bart van Ingen Schenau on 14 Jul 2010 01:22 On Jul 14, 4:49 am, Hei <hchan1...(a)gmail.com> wrote: > Hi, > > I am trying to get the current time based on an input timezone with > the consideration of daylight saving, and do some simple arithmetic > (e.g. "add" 1 day to or "subtract" 1 day from it), > > Is there a function like that in std C++? Unfortunately, no. C++ inherited all date/time handling features from C. For retrieving the current time, those functions are limited to UTC or your local timezone (whatever your system thinks that is). Take a look at the documentation for time(), gmtime(), localtime() and related stuff. Perhaps you can build something off that. Bart v Ingen Schenau -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 15 Jul 2010 08:46 On 2010-07-13 19:49, Hei wrote: > I am trying to get the current time based on an input timezone with > the consideration of daylight saving, Not in "standard" C++, but the GNU C Library has functions that deal with any given timezone, including support for daylight saving: http://www.gnu.org/software/libc/manual/html_node/Calendar-Time.html A man page of tzset(3) says "CONFORMING TO: SVr4, POSIX.1-2001, 4.3BSD", so I think you can use similar functions if you use an implementation that conforms to any of these standards. (And POSIX isn't rare :)) #include <ctime> using namespace std; time_t now = time(0); setenv("TZ", ":Asia/Seoul", 1); tzset(); struct tm t = *localtime(&now); cout << asctime(&t); > and do some simple arithmetic > (e.g. "add" 1 day to or "subtract" 1 day from it), I'm not sure if the "standard" C or C++ provides any defined way for such a thing: it only provides difftime(time_t, time_t), and it makes me wonder, "Then why not addition?" Anyway, if you're under POSIX, it is amazingly simple: time_t is an arithmetic type representing the number of seconds elapsed since the Epoch (1970-01-01T00:00:00Z, i.e. the midnight starting 1 January 1970 in UTC), and you just add or subtract seconds. One day is 86400 seconds. -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: nmm1 on 16 Jul 2010 01:31 In article <i1kr1i$ijb$1(a)usenet.stanford.edu>, Seungbeom Kim <musiphil(a)bawi.org> wrote: >On 2010-07-13 19:49, Hei wrote: >> I am trying to get the current time based on an input timezone with >> the consideration of daylight saving, > >Not in "standard" C++, but the GNU C Library has functions that deal >with any given timezone, including support for daylight saving: Those are the C ones. Using setenv("TZ",... is a thoroughly bad idea, though, and using locales is better (but still problematic). Not all C++ implementations will allow you to set the environment, or use the value you have set if you do. >> and do some simple arithmetic >> (e.g. "add" 1 day to or "subtract" 1 day from it), > >I'm not sure if the "standard" C or C++ provides any defined way for >such a thing: it only provides difftime(time_t, time_t), and it makes >me wonder, "Then why not addition?" Yes, it does. Convert it to broken-down time (in GMT), manipulate that, and convert it back again. Ugh. >Anyway, if you're under POSIX, it is amazingly simple: time_t is an >arithmetic type representing the number of seconds elapsed since the Epoch >(1970-01-01T00:00:00Z, i.e. the midnight starting 1 January 1970 in UTC), >and you just add or subtract seconds. One day is 86400 seconds. Provided that you don't need to handle time precisely - POSIX times are unreliable in the region around leap seconds or over durations spanning leap seconds, to within an error limited by the number of leap seconds involved. And I mean "unreliable", too. Regards, Nick Maclaren. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 16 Jul 2010 07:58 On 2010-07-16 09:31, nmm1(a)cam.ac.uk wrote: > In article <i1kr1i$ijb$1(a)usenet.stanford.edu>, > Seungbeom Kim <musiphil(a)bawi.org> wrote: >> On 2010-07-13 19:49, Hei wrote: >>> I am trying to get the current time based on an input timezone with >>> the consideration of daylight saving, >> >> Not in "standard" C++, but the GNU C Library has functions that deal >> with any given timezone, including support for daylight saving: > > Those are the C ones. And hence usable in C++ programs. > Using setenv("TZ",... is a thoroughly bad idea, though, Can you explain why? I have used the trick (in perl, not in C or C++, though, but the basic idea is the same), and it has worked well in the web program that shows the date and time in each user's timezone as specified in his/her settings. > and using locales is better (but still problematic). You mean locales such as en_US.utf8 or ko_KR.eucKR? How are they involved in timezones? > Not all C++ implementations will allow you to set the environment, > or use the value you have set if you do. That's why I said 'not in "standard" C++, but the GNU C Library...', of course. But isn't it specified in POSIX? (I see that setenv conforms to POSIX, but I don't know exactly what POSIX says about setenv.) >>> and do some simple arithmetic >>> (e.g. "add" 1 day to or "subtract" 1 day from it), >> >> I'm not sure if the "standard" C or C++ provides any defined way for >> such a thing: it only provides difftime(time_t, time_t), and it makes >> me wonder, "Then why not addition?" > > Yes, it does. Convert it to broken-down time (in GMT), manipulate > that, and convert it back again. Ugh. Sorry, but I'm not sure if that's meant as a joke. Even if I start with a broken-down time, I will convert it to time_t and manipulate that. Manipulating broken-down times is reinventing the wheel, and not a trivial task. >> Anyway, if you're under POSIX, it is amazingly simple: time_t is an >> arithmetic type representing the number of seconds elapsed since the Epoch >> (1970-01-01T00:00:00Z, i.e. the midnight starting 1 January 1970 in UTC), >> and you just add or subtract seconds. One day is 86400 seconds. > > Provided that you don't need to handle time precisely - POSIX times > are unreliable in the region around leap seconds or over durations > spanning leap seconds, to within an error limited by the number of > leap seconds involved. And I mean "unreliable", too. Correct. But they are good enough for many purposes, and prevalent in Unix or Linux systems (they are often called 'Unix times'). If you need better precision, POSIX provides timeval or timespec, with millionths or billionths of seconds. They still ignore leap seconds, but neverthe- less they are used as system times (returned by gettimeofday) or file timestamps (ctime, mtime, atime). I have never needed anything beyond this, but of course your mileage may vary: if you need more accuracy with leap seconds, there should be some special-purpose libraries, but I wouldn't recommend them to anyone not explicitly asking for such accuracy. -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 Prev: overloading for multiple arithmetic operations Next: Perfect forwarding of default constructor? |