ts.c 674 B

123456789101112131415161718192021222324252627282930
  1. #define _XOPEN_SOURCE
  2. #include <string.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. int main(int argc, char *argv[])
  7. {
  8. if (argc == 3) { //2006-01-01 00:00:00"
  9. char *Time = NULL;
  10. const char *Format = "%Y-%m-%d %H:%M:%S";
  11. struct tm ts;
  12. Time = calloc(1, strlen(argv[1]) + strlen(argv[2]) + 1 + 1);
  13. sprintf(Time, "%s %s", argv[1], argv[2]);
  14. strptime(Time, Format, &ts);
  15. free(Time);
  16. printf("%ld\n", mktime(&ts));
  17. } else if (argc == 2) { //18734563281
  18. const time_t tm = atol(argv[1]);
  19. char s[11] = "";
  20. strftime(s, 11, "%m.%d.%Y", localtime(&tm));
  21. printf("%s\n", s);
  22. } else {
  23. return 1;
  24. }
  25. return 0;
  26. }