strtotime
,
timetostr
— date and time
conversions.
#include
<strfunc.h>
time_t
strtotime
(char
*datestring);
char *
timetostr
(time_t
value, int
flags);
Those functions used to convert time format from string to binary
time_t and vice versa.
strtotime
(char
*) takes one string argument that represents the date and time in a
number of formats, briefly shown below. strtotime
()
is immune to trailing spaces, newlines and other non-printable and printable
tails. It also tries to recognize elder time formats variants and strings
created by broken implementations.
1
. ISO-8601 notation. This form best
suited for use in mixed human/automatic parsing and comparisons. The typical
date string looks like:
20000930T172458+0400
or
2000-09-30T17:24:58+0400
or
2000-09-30T21:24:58Z
2
. RFC-822 (updated by RFC-1123). This
form used in the E-mail headers, like "Data:" and other. The date
in this format looks like:
Sat, 30 Sep 2000 17:24:58 +0400 (MSD)
or
Sat, 30 Sep 2000 17:24:58 +0400
or even
Sat, 30 Sep 2000 17:24:58 MSD
that is but widely used, but lame,
though correctly handled by strtotime.
3
.
ctime(3)
or
asctime(3)
form:
4
. Syslog daemon's form:
5
. Unix time in numeric form. This form
specifies the number of seconds passed since the Epoch (00:00:00 UTC,
January 1, 1970; see
time(3)
). Typical form is "970257600" (signed long number).
Other types
. A set of other formats that
are widely used is supported too.
Thursday, 10-Jun-93 01:29:59 GMT
Thu, 10 Jan 1993 01:29:59 GMT
Wed Jun 9 01:29:59 1993 GMT
1997/06/23 13:22:33
and derivatives.
Support for other time representation formats can be added upon
request.
timetostr
(time_t
value, int flags) used to convert the time value
to the user-specified representation format, specified by flags. Flags are
defined in strfunc.h:
TFMT_UNIX
- simple numeric unix_time
format.
TFMT_CTIME
-
ctime(3)
and
asctime(3)
format.
TFMT_RFC822
- format defined in RFC's 822
and 1123.
TFMT_X208
- format specified in X.208.
TFMT_ISO8601
- general representation
according to ISO-8601 conventions.
Those flags are present to specify the output format. And the
following ones exist to slightly modify the output:
TFMT_LOCAL
- construct string representing
the time within the local timezone instead of default GMT (zero timezone
offset).
TFMT_UF
- user-friendly output. This flag
turns on some additional features like dashes and colons within the ISO-8601
output or timezone abbreviation within the RFC-822 date string.
TFMT_OLD822GMT
- use old RFC-822 scheme
with the hard-coded GMT zone abbreviation. This flag also disables the
TFMT_LOCAL and TFMT_UF flag will be ignored. The typical string in this
format will look like "Sat, 30 Sep 2000 21:24:58 GMT". This time
format is widely used by the HTTP browsers and CGI scripts to exchange the
"expiration" and "modified" times. This flag should be
used in conjunction with TFMT_RFC822.
Those flags can be mixed by OR'ing.
The strtotime
() function returns the value
of given time representation in seconds since 0 hours, 0 minutes, 0 seconds,
January 1, 1970, Coordinated Universal Time. If parse error occured, zero is
returned and global variable errno is set to EINVAL
to indicate an error.
timetostr
() always returns pointer to the
internal static object with string representation of given time_t value.
void test() {
char *timeString = "Sat, 1 Oct 2000 01:34:00 +0400";
time_t timeValue = 970348600;
printf("timeString -> time_t: %ld\n", (long)strtotime(timeString));
printf("timeValue -> string: %s\n",
timetostr(timeValue,
TFMT_RFC822 | TFMT_LOCAL | TFMT_UF));
}
Lev Walkin <vlm@lionet.info>