1
0

ts.c 815 B

12345678910111213141516171819202122232425262728293031323334353637
  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. size_t siz = strlen(argv[1]) + strlen(argv[2]) + 1 + 1;
  13. time_t tim = 0;
  14. Time = calloc(1, siz);
  15. #ifdef __openbsd__
  16. snprintf(Time, siz, "%s %s", argv[1], argv[2]);
  17. #else
  18. sprintf(Time, "%s %s", argv[1], argv[2]);
  19. #endif
  20. strptime(Time, Format, &ts);
  21. free(Time);
  22. tim = mktime(&ts);
  23. printf("%ld\n", tim);
  24. } else if (argc == 2) { //18734563281
  25. const time_t tm = atol(argv[1]);
  26. char s[11] = "";
  27. strftime(s, 11, "%m.%d.%Y", localtime(&tm));
  28. printf("%s\n", s);
  29. } else {
  30. return 1;
  31. }
  32. return 0;
  33. }