2
0

strftime.c 628 B

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