strftim.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* Time Functions Example
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <time.h>
  15. #include <stdio.h>
  16. #define SIZE 256
  17. int
  18. main (void)
  19. {
  20. char buffer[SIZE];
  21. time_t curtime;
  22. struct tm *loctime;
  23. /* Get the current time. */
  24. curtime = time (NULL);
  25. /* Convert it to local time representation. */
  26. loctime = localtime (&curtime);
  27. /* Print out the date and time in the standard format. */
  28. fputs (asctime (loctime), stdout);
  29. /*@group*/
  30. /* Print it out in a nice format. */
  31. strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
  32. fputs (buffer, stdout);
  33. strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
  34. fputs (buffer, stdout);
  35. return 0;
  36. }
  37. /*@end group*/