On Thu, Jul 31, 2003 at 02:56:52PM +0200, Martin Schroeder wrote:
On 2003-07-31 14:37:19 +0200, Heiko Oberdiek wrote:
In my manual page it was marked as glibc extension, so other systems without can miss support of %z. How the time zone difference can be calculated there in a save way? Have to be used the following procedure or are there better ways?
There is. :-)
<quote src=utils.c#13> The main difficulty is get the time zone offset. strftime() does this in ISO C99 (e.g. newer glibc) with %z, but we have to work with other systems (e.g. Solaris 2.5). So we must use localtime(), which sets the external variable timezone (either int or time_t) to the seconds since UTC. */ void printcreationdate() { time_t t; struct tm *tt; size_t size; char time_str[40]; int hours, minutes;
/* get the time */ t = time(NULL); tt = localtime(&t); size = strftime(time_str, sizeof(time_str), "%Y%m%d%H%M%S", tt); /* expected format: "YYYYmmddHHMMSS" */
/* correction for seconds: %S can be in range 00..61, the PDF reference expects 00..59, therefore we map "60" and "61" to "59" */ if (time_str[12] == '6' && time_str[13] != 0) { time_str[12] == '5'; time_str[13] == '9'; }
/* get the time zone offset */ hours = ((long int)timezone)/3600; minutes = labs((long int)timezone) - (abs(hours) * 3600L); sprintf (&time_str[size], "%+03i'%02d'", hours, minutes);
* Not really, the minutes are seconds ...
* timezone: GMT - Localtime? But wanted is: Localtime - GMT?
/* test.c */
#include
The offset is off by one hour on Suse 7.0/7.1, but I regard this as a minor misfeature. :-)
This is the reason, why I have not used it. The same here (SuSE 7.2),
the test file generates:
+01'00'
But
$ date --rfc-822
Don, 31 Jul 2003 16:00:21 +0200
The same under SunOS Release 5.7:
+01'00'
My man page says:
| The localtime() function converts the calendar time timep
| to broken-time representation, expressed relative to the
| user's specified time zone. The function sets the
| external variables tzname with information about the curÂ
| rent time zone, timezone with the difference between CoorÂ
| dinated Universal Time (UTC) and local standard time in
| seconds, and daylight to a non-zero value if standard US
| daylight savings time rules apply.
Perhaps we have to take daylight into account in some way:
if (daylight) {
diff_minutes += 60;
}
Yours sincerely
Heiko