美文网首页
生成时间格式为:Thu, 27 May 2010 12:12:3

生成时间格式为:Thu, 27 May 2010 12:12:3

作者: 墨迹MoJi_5b88 | 来源:发表于2019-03-28 11:26 被阅读0次
#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;
}

相关文章

网友评论

      本文标题:生成时间格式为:Thu, 27 May 2010 12:12:3

      本文链接:https://www.haomeiwen.com/subject/jotqbqtx.html