date.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * date - Print the current date and time.
  7. *
  8. * TODO: The traditional POSIX version of this tool is supposed
  9. * to accept a format *and* allow you to set the time.
  10. * We currently lack system calls for setting the time,
  11. * but when we add those this should probably be updated.
  12. *
  13. * At the very least, improving this to print the "correct"
  14. * default format would be good.
  15. */
  16. #include <stdio.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <sys/time.h>
  20. static void show_usage(int argc, char * argv[]) {
  21. printf(
  22. "%s - print the time and day\n"
  23. "\n"
  24. "usage: %s [-?] +FORMAT\n"
  25. "\n"
  26. " Note: This implementation is not currently capable of\n"
  27. " setting the system time.\n"
  28. "\n"
  29. " -? \033[3mshow this help text\033[0m\n"
  30. "\n", argv[0], argv[0]);
  31. }
  32. int main(int argc, char * argv[]) {
  33. char * format = "%a %b %d %T %Y";
  34. struct tm * timeinfo;
  35. struct timeval now;
  36. char buf[BUFSIZ] = {0};
  37. int opt;
  38. while ((opt = getopt(argc,argv,"?")) != -1) {
  39. switch (opt) {
  40. case '?':
  41. show_usage(argc,argv);
  42. return 1;
  43. }
  44. }
  45. if (optind < argc && *argv[optind] == '+') {
  46. format = &argv[optind][1];
  47. }
  48. gettimeofday(&now, NULL); //time(NULL);
  49. timeinfo = localtime((time_t *)&now.tv_sec);
  50. strftime(buf,BUFSIZ,format,timeinfo);
  51. puts(buf);
  52. return 0;
  53. }