date.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 <sys/time.h>
  19. /* XXX Why do we have our own list of weekdays */
  20. char * daysofweeks[] = {
  21. "Sunday",
  22. "Monday",
  23. "Tuesday",
  24. "Wednesday",
  25. "Thursday",
  26. "Friday",
  27. "Saturday"
  28. };
  29. void print_time(time_t time) {
  30. struct tm * date = localtime(&time);
  31. if (!date) {
  32. fprintf(stderr, "Failure.\n");
  33. } else {
  34. printf("%d-%02d-%02d %02d:%02d:%02d (%s, day %d)\n",
  35. date->tm_year + 1900,
  36. date->tm_mon + 1,
  37. date->tm_mday,
  38. date->tm_hour,
  39. date->tm_min,
  40. date->tm_sec,
  41. daysofweeks[date->tm_wday],
  42. date->tm_yday);
  43. }
  44. }
  45. int main(int argc, char * argv[]) {
  46. struct timeval tv;
  47. gettimeofday(&tv, NULL);
  48. print_time(tv.tv_sec);
  49. return 0;
  50. }