简体   繁体   中英

Converting UNIX timestamp using ctime

I have a unix time stamp as follows

  char timestamp[100];
  strcpy(timestamp,"701729943");
  time_t timeval=ctime(timestamp);
  printf("Time %s",timeval);

If the check the value of the timestamp in the online unix time convertor it shows 27th march 1992, but if the check the program's output it shows feb 25,1996. How to rectify this?

You're using ctime the wrong way around: it expects a pointer to a time_t and returns a string, whereas you're passing it a string and expect it to return a time_t . Does your compiler not warn you about that?

Anyway, it is meant to be used this way:

time_t timeval = 701729943;
printf("Time %s", ctime(&timeval));

If you only have the UNIX timestamp as a string, use strtoul or atoi to so to make a time_t from it, then do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM