strftime.c 596 B

123456789101112131415161718192021
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. void print(time_t timestamp, char* fmt) {
  5. char* out = malloc(50);
  6. size_t n = strftime(out, 50, fmt, localtime(&timestamp));
  7. printf("%zu: %s\n", n, out);
  8. free(out);
  9. }
  10. int main() {
  11. print(1531808742, "%a %A %b %B");
  12. print(1531808742, "The %Cst century");
  13. print(1531808742, "%I:%M:%S %p");
  14. print(1531839600, "%r");
  15. print(1531839600, "%R");
  16. print(1531839600, "%H %s %u");
  17. print(1531839600, "%j %U");
  18. print(1531839600, "%+");
  19. print(1533669431, "%+%+%+%+%+"); // will overflow 50 characters
  20. }