[comp.lang.c] Seconds from 19xx to Date

pozar@hoptoad.uucp (Tim Pozar) (10/04/88)

    This has most likly been covered, and if so, please forgive
me.
    Does anyone have a routine to change back and forth between
seconds from 19xx to a Year, Month, Day sort of Date?  If not
could some one point me in the proper direction?
	     Thanks
	       Tim

-- 
 ...sun!hoptoad!\                                     Tim Pozar
                 >fidogate!pozar               Fido:  1:125/406
  ...lll-winken!/                            PaBell:  (415) 788-3904
       USNail:  KKSF / 77 Maiden Lane /  San Francisco CA 94108

danr@hcx2.SSD.HARRIS.COM (10/05/88)

>/* Written 10:18 pm  Oct  3, 1988 by pozar@hoptoad.UUCP in hcx2:comp.lang.c */
 [edited slightly without permission]
>/* ---------- "Seconds from 19xx to Date (and visa" ---------- */
>
>   Does anyone have a routine to change back and forth between
>seconds from 19xx to a Year, Month, Day sort of Date?  
>	     Thanks
>	       Tim
>/* End of text from hcx2:comp.lang.c */

	You might want to look at the man page for ctime(), if you have 
access to one.  it is in the standard library, so you don't need to write 
anything bu the function call.

	char *ctime (clock)
	long *clock;

where clock is a pointer to a long integer containing a number of seconds
since 00:00:00 GMT, January 1, 1970 (when time began...)

CAVEAT:  The return value is to a static buffer, so something like

	printf (" %s %s ", ctime (&time1), ctime (&time2));

will print out ctime (&time[12]) for both (depending on the order of 
evaluation of parameters, should some compiler do it backwards).
If you need to do this, copy one of the ctime() return values to another
string before calling ctime() again.

Hope this will do!!!

-danr@hcx1.ssd.harris.com

diamond@csl.sony.JUNET (Norman Diamond) (10/06/88)

In article <5522@hoptoad.uucp>, pozar@hoptoad.uucp (Tim Pozar) writes:
 
>     Does anyone have a routine to change back and forth between
> seconds from 19xx to a Year, Month, Day sort of Date?

You need more than a routine to do this.  You need a database of when
leap seconds have occurred.  Sometimes on June 30th or December 31st,
the last minute runs from 11:59:00 to 11:59:60 (sixty-one seconds) in
order to compensate for wobbles in the earth's rotation.  These do
not happen as regularly as leap days, so their occurrences cannot be
computed by a a routine; you need a database.

(The dpANSI C standard had the field for seconds redefined to allow a
value of 60 for exactly this reason.  I proposed the same correction
to the committees standardizing Extended Pascal.)
-- 
-------------------------------------------------------------------------------
  The above opinions are my own.   |   Norman Diamond
  If they're also your opinions,   |   Sony Computer Science Laboratory, Inc.
  you're infringing my copyright.  |   diamond%csl.sony.jp@relay.cs.net

jfh@rpp386.Dallas.TX.US (The Beach Bum) (10/06/88)

In article <44100014@hcx2> danr@hcx2.SSD.HARRIS.COM writes:
>>   Does anyone have a routine to change back and forth between
>>seconds from 19xx to a Year, Month, Day sort of Date?  
>
>	You might want to look at the man page for ctime(), if you have 
>access to one.  it is in the standard library, so you don't need to write 
>anything bu the function call.

I'd forgotten completely about unctime [ emitc ] which was recently posted.

There is a recently posted function which understands ctime output format
and can convert a ctime string into a long integer time.

It was posted in comp.sources.misc in the last few months.
-- 
John F. Haugh II (jfh@rpp386.Dallas.TX.US)                   HASA, "S" Division

      "Why waste negative entropy on comments, when you could use the same
                   entropy to create bugs instead?" -- Steve Elias

albaugh@dms.UUCP (Mike Albaugh) (10/08/88)

From article <7618@rpp386.Dallas.TX.US>, by jfh@rpp386.Dallas.TX.US (The Beach Bum):
> In article <44100014@hcx2> danr@hcx2.SSD.HARRIS.COM writes:
>>>   Does anyone have a routine to change back and forth between
>>>seconds from 19xx to a Year, Month, Day sort of Date?  
>>
>>	You might want to look at the man page for ctime(), if you have 
>>access to one.  it is in the standard library, so you don't need to write 
>>anything bu the function call.
> 
> I'd forgotten completely about unctime [ emitc ] which was recently posted.

	Well... If you guys already have library routines for both
directions, you may wish to have some fun. I already sent Tim Pozar some
non-library stuff I wrote to do this on an embedded system (no library),
and in the course of testing it discovered some "interesting" behavior
of the VAX11C ctime(). Before you unlimber your flamethrowers about
people who are dumb enough to use VMS, I would like to point out that
friends of mine were prompted by my discovery to check out their own
various systems. ONE (I forget which) did what "people" expected
even when the msb of the time was one. ONE more (I think it was SUNOS)
did a plausible (and ANSI mandated, as I read the "not-yet-standard")
thing and returned a negative date (i.e. before 1970). ALL the rest
we tried did very strange things after about 2038 (+/- 3 yrs). A few
muffed as early as 2000. Just write a little for loop that adds a days
worth of seconds to the binary time and do a sanity check on ctime().
Have fun :-)

| Mike Albaugh (albaugh@dms.UUCP || {...decwrl!turtlevax!}weitek!dms!albaugh)
| Atari Games Corp (Arcade Games, no relation to the makers of the ST)
| 675 Sycamore Dr. Milpitas, CA 95035		voice: (408)434-1709
| The opinions expressed are my own (Boy, are they ever)

guy@auspex.UUCP (Guy Harris) (10/28/88)

>>     Does anyone have a routine to change back and forth between
>> seconds from 19xx to a Year, Month, Day sort of Date?
>
>You need more than a routine to do this.  You need a database of when
>leap seconds have occurred.  Sometimes on June 30th or December 31st,
>the last minute runs from 11:59:00 to 11:59:60 (sixty-one seconds) in
>order to compensate for wobbles in the earth's rotation.  These do
>not happen as regularly as leap days, so their occurrences cannot be
>computed by a a routine; you need a database.

The "Arthur Olson" time handling code, posted to "comp.sources.unix" (I
think), handles this in its latest version (it even includes a database
of leap seconds up to the present).

>(The dpANSI C standard had the field for seconds redefined to allow a
>value of 60 for exactly this reason.  I proposed the same correction
>to the committees standardizing Extended Pascal.)

Unfortunately, POSIX explicitly rules *out* doing this, so you probably
don't want to do this on UNIX.

Note that in 99 44/100% of the cases it doesn't make a bit of
difference; I have a Sun which means I don't trust it to get the time
right down to the second anyway....

guido@cwi.nl (Guido van Rossum) (10/28/88)

Hmm.  Leap seconds.  My quartz watch doesn't understand them, and I can
live with this.  Every so often I resynchronize with a more accurate
source of time.  Since the prime reason for time calculations in
operating systems is to use them for time stamps of files, not for
calculating durations in the order of years accurately to the second, I
propose that we define the time kept by Unix as follows:

	time = (
		(number of whole years since the epoch) * 365
		+
		(number of leap days since the epoch)
	       )
	       *
	       24*3600
	      +
	       (number of seconds since 00:00 this morning).

This makes for easy, compact conversion between time stamp values and
more conventional ways of displaying time.  On the night of a leap
second, we have an ambiguity lasting one second. Big deal.

--
Guido van Rossum, Centre for Mathematics and Computer Science (CWI), Amsterdam
guido@piring.cwi.nl or mcvax!piring!guido or guido%piring.cwi.nl@uunet.uu.net

guy@auspex.UUCP (Guy Harris) (10/29/88)

>...I propose that we define the time kept by Unix as follows:
>
>	time = (
>		(number of whole years since the epoch) * 365
>		+
>		(number of leap days since the epoch)
>	       )
>	       *
>	       24*3600
>	      +
>	       (number of seconds since 00:00 this morning).

If you change that to:

	time = (
		(number of whole years since the epoch) * 365
		+
		(number of leap days since the epoch)
		+
		(number of days since the first day of this year)
	       )
	       *
	       24*3600
	      +
	       (number of seconds since 00:00 this morning).

(or perform some equivalent transformation, so UNIX doesn't think it's
always January 1) I'd vote for that, especially since, as I understand
it, it's precisely what POSIX specifies (traditional UNIX time, no
leap-second correction). 

friedl@vsi.COM (Stephen J. Friedl) (10/31/88)

In article <10030@socslgw.csl.sony.JUNET> diamond@csl.sony.JUNET (Norman Diamond) writes:
(The dpANSI C standard had the field for seconds redefined to allow a
>value of 60 for exactly this reason.

In article <8788@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn ) writes:
> It now ranges from 0 through 61.  You really don't want to know...


OK, I'll ask.

"Doug, why does a time_t range from 0 through 61?"

-- 
Steve Friedl    V-Systems, Inc.  +1 714 545 6442    3B2-kind-of-guy
friedl@vsi.com     {backbones}!vsi.com!friedl    attmail!vsi!friedl
----Nancy Reagan on 120MB SCSI cartridge tape: "Just say *now*"----

gwyn@smoke.BRL.MIL (Doug Gwyn ) (10/31/88)

In article <916@vsi.COM> friedl@vsi.COM (Stephen J. Friedl) writes:
>"Doug, why does a time_t range from 0 through 61?"

It's not time_t, but rather the number of seconds in a struct tm's
tm_sec member.

The answer is, there was one magic moment in history when TWO leap
seconds were added to the same minute.

I told you you didn't really want to know.

friedl@vsi.COM (Stephen J. Friedl) (11/01/88)

In article <8810@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn ) writes:
> 
> The answer is, there was one magic moment in history when TWO leap
> seconds were added to the same minute.
> 

I think I remember that -- the Earth wobbled a little bit
extra that day :-)

-- 
Steve Friedl    V-Systems, Inc.  +1 714 545 6442    3B2-kind-of-guy
friedl@vsi.com     {backbones}!vsi.com!friedl    attmail!vsi!friedl
----Nancy Reagan on 120MB SCSI cartridge tape: "Just say *now*"----

devine@cookie.dec.com (Bob Devine) (11/01/88)

Stephen Friedl asked:
>"Doug, why does a time_t range from 0 through 61?"
 
Doug Gwyn replied:
> The answer is, there was one magic moment in history when TWO leap
> seconds were added to the same minute.

  No, there is not a minute that contains two leap seconds.  However
the year 1972 contains one at the end of June and another at the
end of December.  Yep, leap seconds are ugly!

  So the range of 0-60 is sufficient with 0-59 being the usual values.

Bob Devine

smryan@garth.UUCP (Steven Ryan) (11/03/88)

In article <920@vsi.COM> friedl@vsi.COM (Stephen J. Friedl) writes:
>In article <8810@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn ) writes:
>> 
>> The answer is, there was one magic moment in history when TWO leap
>> seconds were added to the same minute.
>
>I think I remember that -- the Earth wobbled a little bit
>extra that day :-)

Was that the day System V was released?

--------------------
OSF is the AntiUnix.