[comp.sources.wanted] YYMMDDHHMM.SS -> long tval

sl@van-bc.UUCP (Stuart Lynne) (08/06/87)

I need a C library routine to convert an ASCII string to the Unix long int 
representation for time (number of seconds since Jan/70).

Does anyone have anything that might to the trick? I was mildly suprised
that there is no standard Unix library routine for doing this. (Well if
there is it's well hidden!)

Thanks in advance for anyone who has written such a routine and is willing
to post or mail it.

guy%gorodish@Sun.COM (Guy Harris) (08/09/87)

> I need a C library routine to convert an ASCII string to the Unix long int 
> representation for time (number of seconds since Jan/70).
> 
> Does anyone have anything that might to the trick? I was mildly suprised
> that there is no standard Unix library routine for doing this. (Well if
> there is it's well hidden!)

It's not well hidden; it really is absent.  There are several
versions of code to do this buried in various UNIX utilities.

The public-domain "ctime" package by Arthur Olson, in addition to
handling the DST rules problem more sanely than the standard UNIX way
of handling it, also includes two routines "timelocal" and "timegm"
that are the "inverses" of "localtime" and "gmtime" ("inverses" is in
quotes because "localtime" is, due to the way DST works, a
many-to-one function, and not invertible; if handed one of the local
times that can result from two different values being handed to
"localtime", I don't know which one "timelocal" picks).

This code was posted to mod.sources (now named comp.sources.unix) a
while ago.

This does most of the work; converting the string in question to a
"struct tm", which is what you hand "timelocal", is pretty
straightforward.

Alternatively, Steve Bellovin wrote a routine called "getdate" that
takes a pointer to a character string and returns a "time_t" whose
value is the time that the string represents.  It doesn't take the
format listed above - it takes a human-readable date, in any one of a
large number of forms, and converts it.  (A YACC grammar does the
parsing.)  If you can find a copy of this (I don't know whether he
considers it public-domain or not), you can extract the relevant code
from it.

ANSI C specifies a routine "mktime" which behaves in a fashion
similar to that of "timelocal" (the difference is that "mktime" will
"canonicalize" the time first, so that you can add two "struct tm"s
as mixed-radix numbers, without having to worry about propagating
carries, and hand it to "mktime"); the adoption of ANSI C will
finally plug that rather annoying gap in the UNIX C library.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

dave@lsuc.UUCP (08/10/87)

> I need a C library routine to convert an ASCII string to the Unix long int 
> representation for time (number of seconds since Jan/70).

Use getdate(3). It's available as part of the netnews source,
and has also been posted to *.sources separately.

David Sherman
The Law Society of Upper Canada
Toronto
-- 
{ uunet!mnetor  pyramid!utai  decvax!utcsri  ihnp4!utzoo } !lsuc!dave