#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#define DAY_MIN (24 * HOUR_MIN)
#define HOUR_MIN 60
#define MIN_SEC 60
int main(int argc, char **argv)
{
time_t now;
struct tm *lt;
struct tm gmt;
int gmtoff;
char vp[100];
char *p = vp;
time(&now);
gmt = *gmtime(&now);
lt = localtime(&now);
gmtoff = (lt->tm_hour - gmt.tm_hour) * HOUR_MIN + lt->tm_min - gmt.tm_min;
if (lt->tm_year < gmt.tm_year)
gmtoff -= DAY_MIN;
else if (lt->tm_year > gmt.tm_year)
gmtoff += DAY_MIN;
else if (lt->tm_yday < gmt.tm_yday)
gmtoff -= DAY_MIN;
else if (lt->tm_yday > gmt.tm_yday)
gmtoff += DAY_MIN;
if (lt->tm_sec <= gmt.tm_sec - MIN_SEC)
gmtoff -= 1;
else if (lt->tm_sec >= gmt.tm_sec + MIN_SEC)
gmtoff += 1;
#ifdef MISSING_STRFTIME_E
#define STRFTIME_FMT "%a, %d %b %Y %H:%M:%S "
#else
#define STRFTIME_FMT "%a, %e %b %Y %H:%M:%S "
#endif
int len = strftime(p, 100, STRFTIME_FMT, lt);
p += len;
if (gmtoff < -DAY_MIN || gmtoff > DAY_MIN)
printf("UTC time offset %d is larger than one day", gmtoff);
len = sprintf(p, "%+03d%02d", (int)(gmtoff / HOUR_MIN),
(int)(abs(gmtoff) % HOUR_MIN));
p += len;
len = strftime(p, 100, " (%Z)", lt);
printf("%s\n", vp);
return 0;
}
网友评论