# time_t, arithmetic types, difftime, and -

Discussion in 'C Programming' started by Beej Jorgensen, Jun 11, 2009.

1. ### Beej JorgensenGuest

That got me thinking:

time_t is an arithmetic type, and so the '-' operator will work on it.

But difftime() exists because there's nothing in the Standard that
specifies the encoding of a time_t variable, so this might BE true:

(t1 - t0) != difftime(t1, t0)

is this line of thinking correct?

Is this was the Rationale means when it says, "No arithmetic properties
of [clock_t and time_t] are defined by the Standard", that is, "When you
difftime() time 7 from time 10, you might not get time 3, even though
you will get 3 when you use the '-' operator."

-Beej

Beej Jorgensen, Jun 11, 2009

2. ### jameskuyperGuest

Beej Jorgensen wrote:
> That got me thinking:
>
> time_t is an arithmetic type, and so the '-' operator will work on it.
>
> But difftime() exists because there's nothing in the Standard that
> specifies the encoding of a time_t variable, so this might BE true:
>
> (t1 - t0) != difftime(t1, t0)
>
> is this line of thinking correct?

Yes. For instance, the time_t value corresponding to
2009-06-11T21:11:02Z might be 20090611.211102; the value for
2009-06-11T21:10:59Z might be 20090611.211059. difftime(t1, t0) must
return 3.0, but t1-t0 would be 0.000043.

> Is this was the Rationale means when it says, "No arithmetic properties
> of [clock_t and time_t] are defined by the Standard", that is, "When you
> difftime() time 7 from time 10, you might not get time 3, even though
> you will get 3 when you use the '-' operator."

Yes. The manner in which time_t values are encoded is completely
unspecified by the standard. Emphasize the word "completely". Think

jameskuyper, Jun 11, 2009

3. ### FredGuest

On Jun 11, 2:47 pm, jameskuyper <> wrote:
> Beej Jorgensen wrote:
> > That got me thinking:

>
> > time_t is an arithmetic type, and so the '-' operator will work on it.

>
> > But difftime() exists because there's nothing in the Standard that
> > specifies the encoding of a time_t variable, so this might BE true:

>
> >   (t1 - t0) != difftime(t1, t0)

>
> > is this line of thinking correct?

>
> Yes. For instance, the time_t value corresponding to
> 2009-06-11T21:11:02Z might be 20090611.211102; the value for
> 2009-06-11T21:10:59Z might be 20090611.211059. difftime(t1, t0) must
> return 3.0, but t1-t0 would be 0.000043.
>
> > Is this was the Rationale means when it says, "No arithmetic properties
> > of [clock_t and time_t] are defined by the Standard", that is, "When you
> > difftime() time 7 from time 10, you might not get time 3, even though
> > you will get 3 when you use the '-' operator."

>
> Yes. The manner in which time_t values are encoded is completely
> unspecified by the standard. Emphasize the word "completely".  Think

Even if you know the encoding, you cannot rely on your own
subtractions to get the correct delta time.

A correct implementation of difftime must also know about all of
the leap-seconds that have been added between the two input times
in order to correctly determine the difference.

For example, the last three leap-seconda were added on:
Jun 30 1998,
Jan 31 2005, and
Jan 31 2008

It is possible that the leap-second will be abolished in the
future, which will cause UTC to begin drifting from solar time.

--
Fred Kleinschmidt

Fred, Jun 11, 2009
4. ### Eric SosmanGuest

[OT] Re: time_t, arithmetic types, difftime, and -

Jack Klein wrote:
> On Thu, 11 Jun 2009 20:22:22 -0500, pete <>
> wrote in comp.lang.c:
>
>> Eric Sosman wrote:
>>>[...]
>>> Bits 0-5: Seconds (0-60)
>>> Bits 6-11: Minutes (0-59)
>>> Bits 12-16: Hours (0-23)
>>> Bits 17-21: Day of month (1-31)
>>> Bits 22-25: Month (1-12)
>>> Bits 26-32: Year minus 1957 (0-127, i.e., 1957-2084)
>>>[...]

> Assuming that we correct for Eric's use of 33 bits,

I *might* have made an off-by-one error, but isn't it
*far* more likely that CHAR_BIT == 11, sizeof(int) == 3?
Yes, yes, *of course* that's the explanation ...

Trivia question: Why 1957 for Year Zero?

--
Eric Sosman
lid

Eric Sosman, Jun 12, 2009
5. ### Lew PitcherGuest

Re: [OT] Re: time_t, arithmetic types, difftime, and -

On June 12, 2009 09:39, in comp.lang.c, Eric Sosman
() wrote:

> Jack Klein wrote:
>> On Thu, 11 Jun 2009 20:22:22 -0500, pete <>
>> wrote in comp.lang.c:
>>
>>> Eric Sosman wrote:
>>>>[...]

[snip]
>>>> Bits 26-32: Year minus 1957 (0-127, i.e., 1957-2084)
>>>>[...]

>> Assuming that we correct for Eric's use of 33 bits,

>
> I *might* have made an off-by-one error, but isn't it
> *far* more likely that CHAR_BIT == 11, sizeof(int) == 3?
> Yes, yes, *of course* that's the explanation ...
>
> Trivia question: Why 1957 for Year Zero?
>

Guesses:
1) The year of the passing of John von Neumann,
2) The year of the founding of Digital Equipment Corporation, or
3) The year of the founding of the Advanced Projects Research Agency of
the US Department of Defense

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Lew Pitcher, Jun 12, 2009
6. ### Beej JorgensenGuest

Re: [OT] Re: time_t, arithmetic types, difftime, and -

Lew Pitcher <> wrote:
>On June 12, 2009 09:39, in comp.lang.c, Eric Sosman
>> Trivia question: Why 1957 for Year Zero?

>
>Guesses:
> 1) The year of the passing of John von Neumann,
> 2) The year of the founding of Digital Equipment Corporation, or
> 3) The year of the founding of the Advanced Projects Research Agency of
> the US Department of Defense

No, no! It was the year the first Frisbee flying disc was produced!
Yes!

A legitimate guess: birth year of FORTRAN?

-Beej

Beej Jorgensen, Jun 12, 2009
7. ### Lew PitcherGuest

Re: [OT] Re: time_t, arithmetic types, difftime, and -

On June 12, 2009 14:14, in comp.lang.c, Beej Jorgensen () wrote:

> Lew Pitcher <> wrote:
>>On June 12, 2009 09:39, in comp.lang.c, Eric Sosman
>>> Trivia question: Why 1957 for Year Zero?

>>
>>Guesses:
>> 1) The year of the passing of John von Neumann,
>> 2) The year of the founding of Digital Equipment Corporation, or
>> 3) The year of the founding of the Advanced Projects Research Agency of
>> the US Department of Defense

>
> No, no! It was the year the first Frisbee flying disc was produced!
> Yes!
>
> A legitimate guess: birth year of FORTRAN?

And I counter with
the year that the first "recursive stack" (aka lifo) was implemented in a
computer language
or
the year that the Athena computer was completed
or
the year that the first all-solid-state computer (the NCR 304) was built

But, I think your FORTRAN guess is closer to the money

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Lew Pitcher, Jun 12, 2009
8. ### Eric SosmanGuest

Re: [OT] Re: time_t, arithmetic types, difftime, and -

Lew Pitcher wrote:
> On June 12, 2009 09:39, in comp.lang.c, Eric Sosman
> () wrote:
>
>> Jack Klein wrote:
>>> On Thu, 11 Jun 2009 20:22:22 -0500, pete <>
>>> wrote in comp.lang.c:
>>>
>>>> Eric Sosman wrote:
>>>>> [...]

> [snip]
>>>>> Bits 26-32: Year minus 1957 (0-127, i.e., 1957-2084)
>>>>> [...]
>>> Assuming that we correct for Eric's use of 33 bits,

>> I *might* have made an off-by-one error, but isn't it
>> *far* more likely that CHAR_BIT == 11, sizeof(int) == 3?
>> Yes, yes, *of course* that's the explanation ...
>>
>> Trivia question: Why 1957 for Year Zero?
>>

>
> Guesses:
> 1) The year of the passing of John von Neumann,
> 2) The year of the founding of Digital Equipment Corporation, or
> 3) The year of the founding of the Advanced Projects Research Agency of
> the US Department of Defense

As with all such, there are many candidates. (3) was
closest to the one I had in mind, because even though ARPA
was founded in 1958 rather than 1957, it was in reaction to
the 1957 event. Without the scare Sputnik gave the States,
and the concomitant flow of funding to fight off fear, we
might still be using 4MHz machines with half-megabyte memories.

There was a piece in The Register recently about the first
"portable computer:" No, not the Osborne or some such, but the
on-board navigational system of the Minuteman I, something of
a triumph of miniaturization at the time. According to the
article, the U.S. military was for a time the world's largest
purchaser of those newfangled integrated circuits, and the
principal reason for IC makers to go into and stay in business.

--
Eric Sosman
lid

Eric Sosman, Jun 12, 2009
9. ### Richard BosGuest

Re: [OT] Re: time_t, arithmetic types, difftime, and -

Beej Jorgensen <> wrote:

> Lew Pitcher <> wrote:
> >On June 12, 2009 09:39, in comp.lang.c, Eric Sosman
> >> Trivia question: Why 1957 for Year Zero?

> >
> >Guesses:
> > 1) The year of the passing of John von Neumann,
> > 2) The year of the founding of Digital Equipment Corporation, or
> > 3) The year of the founding of the Advanced Projects Research Agency of
> > the US Department of Defense

>
> No, no! It was the year the first Frisbee flying disc was produced!
> Yes!
>
> A legitimate guess: birth year of FORTRAN?

Birth year of a certain Mr. E. Sosman?

Richard

Richard Bos, Jun 13, 2009
10. ### Steve AllenGuest

On Jun 11, 3:12 pm, Fred <> wrote:
> A correct implementation of difftime must also know about all of
> theleap-secondsthat have been added between the two input times
> in order to correctly determine the difference.

a correct implementation of difftime must make a policy decision
of the sort that federal governments around the world have not
been able to make in a consistent fashion: to decide what kind of
seconds are supposed to be counted. See
http://www.ucolick.org/~sla/leapsecs/epochtime.html
for a javascript example of the issues.

Steve Allen, Jun 19, 2009