获得本地时间并格式化为http需要的格式:比如(Date: Fri, 31 Dec 1999 23:59:59 GMT)

 

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>



char localtm[80];

void getlocaltime()

{

        time_t now; //实例化time_t结构

        struct tm *timenow; //实例化tm结构指针

        time(&now);     //time函数读取现在的时间(国际标准时间非北京时间),然后传值给now



        timenow = localtime(&now);



        char daytime[50];

        asctime_r(timenow, daytime);



        printf("%s", daytime);

        char *pch, *week, *mon, *day, *time, *year;

        pch = strtok(daytime, " ");

        int i = 0;

        while(pch != NULL)

        {

                printf("%s\n", pch);

                switch(i)

                {

                        case 0: week = pch; break;

                        case 1: mon = pch; break;

                        case 2: day = pch; break;

                        case 3: time = pch; break;

                        case 4: year = pch; break;

                        default: break;

                }

                pch = strtok(NULL, " ");

                i++;    

        }

        

        char realyear[4];

        strncpy(realyear, year, 4);     

        //char localtime[80];

        printf("Date: %s, %s %s %s %s GMT\n", week, day, mon, realyear, time);

        sprintf(localtm, "Date: %s, %s %s %s %s GMT\n", week, day, mon, realyear, time);

        //return localtime;

}



你可能感兴趣的:(http)