Topic: Getting system time
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/15 Raw View
Lars Thorup wrote:
>
> David R Tribble wrote in message <36E9A091.8E626706@technologist.com>...
> >Robert Batusek wrote:
> >> Is there any _portable_ (ANSI compatible) C++ possibility to get the
> >> system time?
> >> I have tried ftime but it doesn't work together with fstreams.
> >
> >Use time(), which is declared in <ctime> and <time.h>. It's the
> >same as the standard C (C89) library function. (ftime() is System
> >V Unix, and is deprecated.)
>
> Be aware though, that the type time_t, which is the result type
> of the time() function, on many platforms cannot hold dates after the
> year 2038.
Presumably, that will get fixed some time before 2040 :-).
> So even though time() is portable between different platforms it is _not_
> portable between different decades... :-(
The solution to that problem, of course, is to convert the time returned
into a struct tm using gmtime(), and from there converting it into a
text string using asctime() or strftime() or time_put<>. In the reverse
direction, you can only use time_get<>, since neither asctime() nor
strftime() has a standardized inverse function.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/03/16 Raw View
Robert Batusek wrote:
>> Is there any _portable_ (ANSI compatible) C++ possibility to get the
>> system time?
>> I have tried ftime but it doesn't work together with fstreams.
David R Tribble wrote:
> Use time(), which is declared in <ctime> and <time.h>. It's the
> same as the standard C (C89) library function. (ftime() is System
> V Unix, and is deprecated.)
Lars Thorup wrote:
> Be aware though, that the type time_t, which is the result type
> of the time() function, on many platforms cannot hold dates after the
> year 2038.
>
> So even though time() is portable between different platforms it is
> _not_ portable between different decades... :-(
It's even worse than that. (I wrote about this in the Letters
section of the C/C++ Users Journal, Mar 1999.)
The only guarantees you have about time_t are that it is an
arithmetic type, and that it can be converted into a struct 'tm'
value and back. There are no other requirements about its type,
precision, or range.
Implementations are free to provide any precision they want,
whether it's to the nearest microsecond or the nearest day, and
any range of dates they see fit, whether it spans thousands of
years or only a single day. Most implementations that mimic the
POSIX implementation use a 32-bit signed integer count of the
number of seconds since 1970-01-01 00:00:00 UT, which provides a
1-second accuracy over an epoch that ends on 2038-01-17. But
don't count on this for portability. Most operating systems are
capable of determining the time to subsecond precision, but few
of them reflect this fact in their time_t representations.
I and others have argued for a better specification of the time
functions and types, but it looks like <time.h> is going to stay
the way it is for a while longer. (It's in the hands of the
C9X committee, since C++ adopts the semantics of the C library.)
(I myself proposed a new extended-precision time type, but it
was greeted with little enthusiasm. I abandoned the proposal,
but if you're curious, you can still read it at:
<http://www.flash.net/~dtribble/text/c0xtime.htm>.)
-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Robert Batusek <xbatusek@fi.muni.cz>
Date: 1999/03/12 Raw View
Is there any _portable_ (ANSI compatible) C++ possibility to get the
system time?
I have tried ftime but it doesn't work together with fstreams.
Robert Batusek
**********************************************************************
Robert Batusek Ph. D. student
xbatusek@fi.muni.cz Faculty of Informatics
http://www.fi.muni.cz/~xbatusek Masaryk University Brno
**********************************************************************
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/03/13 Raw View
Robert Batusek wrote:
> Is there any _portable_ (ANSI compatible) C++ possibility to get the
> system time?
> I have tried ftime but it doesn't work together with fstreams.
Use time(), which is declared in <ctime> and <time.h>. It's the
same as the standard C (C89) library function. (ftime() is System
V Unix, and is deprecated.)
If you want more precision, you could try a combination of time()
and clock() calls.
There are no other options for writing a *portable*, ISO-conformant
time-sensitive program.
However, if you're using a POSIX variant, you can try gettimeofday()
or clock_gettime(). If you're using OSF, you can try getclock().
If you're using Win32, you can try GetSystemTime() or
GetSystemTimeAsFileTime().
-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/03/13 Raw View
In article <Pine.SGI.3.96.990312091120.4086F-100000@aisa>,
xbatusek@fi.muni.cz says...
>
>
> Is there any _portable_ (ANSI compatible) C++ possibility to get the
> system time?
Other than time(), you mean? If you want anything more precise than
it provides (1 second at best) you have to do it non-portably, AFAIK.
> I have tried ftime but it doesn't work together with fstreams.
I'm not sure how ftime would relate to fstreams, but it's definitely
NOT at all portable -- it's definitely not in the C or C++ standard
libraries, and it doesn't even appear to be part of POSIX...
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Lars Thorup" <lth@scanjour.dk>
Date: 1999/03/14 Raw View
David R Tribble wrote in message <36E9A091.8E626706@technologist.com>...
>Robert Batusek wrote:
>> Is there any _portable_ (ANSI compatible) C++ possibility to get the
>> system time?
>> I have tried ftime but it doesn't work together with fstreams.
>
>Use time(), which is declared in <ctime> and <time.h>. It's the
>same as the standard C (C89) library function. (ftime() is System
>V Unix, and is deprecated.)
Be aware though, that the type time_t, which is the result type
of the time() function, on many platforms cannot hold dates after the
year 2038.
So even though time() is portable between different platforms it is _not_
portable between different decades... :-(
/ Lars Thorup, Denmark
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]