C 時間轉換時間戳

raysuen發表於2017-05-08

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>

void help()

{

    printf("\n");

    printf("\n");

    printf("TimeTOTimestamp \"date\" \"date format\"\n");

    printf("For example:\n");

    printf("    TimeTOTimestamp \"20170508\" \"%%Y%%m%%d\"\n");

    printf("format infomation:\n");

    printf("    %%a: Abbreviated weekday name---Thu\n");

    printf("    %%A: Full weekday name---Thursday\n");

    printf("    %%b: Abbreviated month name---Aug\n");

    printf("    %%B: Full month name---August\n");

    printf("    %%c: Date and Time representation---Thu Aug 23 14:55:01 2001\n");

    printf("    %%C: Year divided by 100 and truncated to integer(00-99)---20\n");

    printf("    %%d: Day of month,zero-padded(01-31)---23\n");

    printf("    %%D: Shor MM/DD/YY,equivalent %%m/%%d/%%y---08/23/01\n");

    printf("    %%e: Doy of month,space-padded(1-31)---23\n");

    printf("    %%F: Short YYYY-MM-DD date,equivalent %%Y-%%m-%%d---2001-08-23\n");

    printf("    %%g: Week base year,last two digits(00-99)---01\n");

    printf("    %%G: Week base year---2001\n");

    printf("    %%h: Abbreviated month name(same as %%b)---Aug\n");

    printf("    %%H: Hour in 24h format(00-23)---14\n");

    printf("    %%I: Hour in 12h format(01-12)---02\n");

    printf("    %%j: Day of the year(001-366)---235\n");

    printf("    %%m: Month as a decimal number(01-12)---08\n");

    printf("    %%M: Minute(00-59)---55\n");

    printf("    %%n: New line character(\'\\n\')\n");

    printf("    %%p: AM or PM designation---PM\n");

    printf("    %%r: 12-hour clock time---02:55:02 PM\n");

    printf("    %%R: 24-hour HH:MM time,equivalent %%H:%%M---14:55\n");

    printf("    %%S: Second(00-59)---02\n");

    printf("    %%t: Horizental-tab character(\'\\t\')\n");

    printf("    %%T: ISO 8601 time format(HH:MM:SS),equivalent %%H:%%M:%%S---14:55:02\n");

    printf("    %%u: ISO 8601 weekday as number with Monday as 1(1-7)---4\n");

    printf("    %%U: Week number with the first Sunday as the first day of week one(00-53)---33\n");

    printf("    %%V: IOS 8601 week number(00-53)---34\n");

    printf("    %%w: Weekday as decimal number with Sunday(0-6)---4\n");

    printf("    %%W: Week number with the first Monday as the first day of week one (00-53)---34\n");

    printf("    %%x: Date representation---08/23/01\n");

    printf("    %%X: Time representation---14:55:02\n");

    printf("    %%y: Year,last two digits(00-99)---01\n");

    printf("    %%Y: Year---2001\n");

    printf("\n");

    printf("\n");

    

}

int main(int argc, const char * argv[]) {

    int ret = 0;

    struct tm * tmp_time = (struct tm *)malloc(sizeof(struct tm));

    time_t tt;

    if (argc < 3){

        if (argc == 2) {

            if (strcmp(argv[1],"--help") == 0) {

                help();

                goto End;

            }else{

                printf("The program need two parameters!!!\n");

                goto End;

            }

        }

        else{

            printf("The program need two parameters!!!\n");

            goto End;

        }

    }else if (argc > 3){

        printf("The program just need two parameters!!!\n");

        goto End;

    }

    memset(tmp_time, 0sizeof(struct tm));

    strptime(argv[1],argv[2],tmp_time);

    tt = mktime(tmp_time);

    printf("%ld\n",tt);

End:

    if (tmp_time != NULL) {

        free(tmp_time);

        tmp_time = NULL;

    }

    return ret;


}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28572479/viewspace-2138694/,如需轉載,請註明出處,否則將追究法律責任。

相關文章