From -1807537863666793548
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,3b2d33f1a39395a6
X-Google-Attributes: gidf78e5,public
From: David R Tribble <dtribble@technologist.com>
Subject: Re: Getting system time
Date: 1999/03/16
Message-ID: <36EEB462.8B549C4F@technologist.com>#1/1
X-Deja-AN: 455794482
Content-Transfer-Encoding: 7bit
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <Pine.SGI.3.96.990312091120.4086F-100000@aisa> <36E9A091.8E626706@technologist.com> <isvG2.191$TY3.231@news.get2net.dk>
X-Original-Date: Tue, 16 Mar 1999 13:43:30 -0600
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 921621770 9293 128.250.29.16 (16 Mar 1999 22:02:50 GMT)
Organization: File by pile
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANu7U5+EDnX0m9pzZAQHT4QF+Lk7Lu4GKvglof/iyJlnPPnMN6Thk7XIl UMM+p9PdlGEAS9fhQKe9+e1fnE9zwWJk =j0tc
Mime-Version: 1.0
NNTP-Posting-Date: 16 Mar 1999 22:02:50 GMT
Newsgroups: comp.std.c++

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              ]



