Home > Archive > Unix Programming > January 2005 > generic time parsing functions, getdate() DATEMSK examples, and strptime()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
generic time parsing functions, getdate() DATEMSK examples, and strptime()
|
|
| Paul Sheer 2005-01-29, 3:57 pm |
|
I would like a function that parses an ascii string
into a struct tm. The function should support a
variety of time formats for a variety of locales.
It should not only parse the time, but determine
what standard of format has been given.
It seems silly that no such function has yet been
written. Is it not possible? or is there some
reason why one would never want to do this?
Currently there are a variety of date formats like
ISO, W3C, RFC822, asctime() and others. If I don't
know what kind of time format I am going to get,
I would like the function to tell me,
as well as the locale. If there is an
ambiguity, to indicate a list of probable
formats ordered by likelihood. The latter is a
tall order, I know, but a nice-to-have. The
function should also understand all known time-zone
abbreviations and return the GMT offset if possible.
If the time format seems to have missing offset
information, then this should be indicated as a
warning.
Alternatively, the getdate() man page mentions
a DATEMSK envvar that points to a file. Where can
I get an example of such a file that contains all
the common time formats in use? This would mostly
solve my problem.
Note that I have read the man pages: localtime(3),
strftime(3), strptime(3), time(3), date(1),
gettimeofday(2), newctime(3), time(2), utime(2),
clock(3), difftime(3), strftime(3), tzset(3),
getdate().
(Also, to really be useful, the source for this
function must be available in both Pascal, Java,
and ANSI-C. And it must also have a reverse
function.)
Thanks for any help!
-paul
| |
| Martin Blume 2005-01-29, 3:57 pm |
| "Paul Sheer" schrieb
>
> I would like a function that parses an ascii string
> into a struct tm. The function should support a
> variety of time formats for a variety of locales.
Sounds like strptime() to me.
> It should not only parse the time, but determine
> what standard of format has been given.
Yes? How?
What's the difference between 01/02/2004 and 01-02-2004?
Which one is February 1st 2004 and which one is
January 2nd 2004?
>
> It seems silly that no such function has yet been
> written. Is it not possible? or is there some
> reason why one would never want to do this?
>
I have made the experience that if something seems
exceedingly silly or wrong to me, then in all probability
I have a wrong understanding of the situation.
Only in some circumstances it is advisable to use more
force or to use a bigger hammer.
> [...]
> Note that I have read the man pages: localtime(3),
> strftime(3), strptime(3), time(3), date(1),
^^^^^^^^
This would be closest to what you're looking for:
"The strptime() function is the converse function to
strf_time() and converts the character string pointed to
by s to values which are stored in the tm structure
pointed to by tm, using the format specified by format."
>
> ([...] And it must also have a reverse
> function.)
>
The reverse function of strptime is strftime.
>
> Thanks for any help!
>
Why not just try it out?
The smallest useful program:
# strpftime <in-format> <date> <out-format>
# reads <date> in <in-format> style,
# transforms it using strptime,
# transforms it using strftime,
# and writes it out again.
# TBD error checking etc.
#include <time.h>
int main(int argc, char *argv[])
{
struct tm tms;
char tout[256];
memset(&tms, 0, sizeof(tms));
strptime(argv[2], argv[1], &tms);
strftime(tout, sizeof(tout)-1, argv[3], &tms);
printf("%s\n", tout);
return 0;
}
and this is some output:
$ ./strpftime "%Y-%m-%d" "2005-01-28" "%Y-%B-%A %H:%M:%S"
2005-January-Friday 00:00:00
$ ./strpftime "%s" "`date +%s`" "%c"
Sat Jan 29 19:49:23 2005
HTH
Martin
| |
| Paul Sheer 2005-01-30, 3:57 am |
| On Sat, 29 Jan 2005 19:53:42 +0100, Martin Blume wrote:
> [...]
> exceedingly silly or wrong to me, then in all probability
> I have a wrong understanding of the situation.
> [...]
how apt
-paul
| |
| Maurizio Loreti 2005-01-31, 4:00 pm |
| "Paul Sheer" <psheer@WITHOUTicon.co.za> writes:
> I would like a function that parses an ascii string
> into a struct tm. The function should support a
> variety of time formats for a variety of locales.
If you speak C++, have a look to the Boost date_time classes.
http://www.boost.org/ . Wonderful...
--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: ybergv@cq.vasa.vg
|
|
|
|
|