time-key.c 945 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Outputs the value of time(2) with the 16 least significant bits zeroed out.
  3. * For use in context keyed payload encoding.
  4. *
  5. * Author: Dimitris Glynos <dimitris at census-labs.com>
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #define __USE_XOPEN
  10. #include <time.h>
  11. char *app = NULL;
  12. void croak_usage(void)
  13. {
  14. fprintf(stderr, "usage: %s [date & time]\n"
  15. "\tSupported date & time format: 'YYYY-MM-DD HH:MM:SS'\n"
  16. "\te.g. %s '2003-11-04 14:23:10'\n",
  17. app, app);
  18. exit(1);
  19. }
  20. time_t parse_time(const char *input)
  21. {
  22. struct tm t;
  23. char *p;
  24. p = strptime(input, "%Y-%m-%d %H:%M:%S", &t);
  25. if ((!p) || (*p != '\0')) {
  26. fprintf(stderr, "error while processing time spec!\n");
  27. croak_usage();
  28. }
  29. return mktime(&t);
  30. }
  31. int main(int argc, char *argv[])
  32. {
  33. time_t t;
  34. app = argv[0];
  35. if (argc > 2)
  36. croak_usage();
  37. if (argc == 2)
  38. t = parse_time(argv[1]);
  39. else
  40. t = time(NULL);
  41. printf("%#.8lx\n", t & 0xffff0000);
  42. return 0;
  43. }