/*********************************************************
* From C PROGRAMMING: A MODERN APPROACH, Second Edition *
* By K. N. King *
* Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
* All rights reserved. *
* This program may be freely distributed for class use, *
* provided that this copyright notice is retained. *
*********************************************************/
/* datetime.c (Chapter 26, page 700) */
/* Displays the current date and time in three formats */
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t current = time(NULL);
struct tm *ptr;
char date_time[21];
int hour;
char am_or_pm;
/* Print date and time in default format */
puts(ctime(¤t));
/* Print date and time, using strftime to format */
strftime(date_time, sizeof(date_time),
"%m-%d-%Y %I:%M%p\n", localtime(¤t));
puts(date_time);
/* Print date and time, using printf to format */
ptr = localtime(¤t);
hour = ptr->tm_hour;
if (hour <= 11)
am_or_pm = 'a';
else {
hour -= 12;
am_or_pm = 'p';
}
if (hour == 0)
hour = 12;
printf("%.2d-%.2d-%d %2d:%.2d%c\n", ptr->tm_mon + 1,
ptr->tm_mday, ptr->tm_year + 1900, hour,
ptr->tm_min, am_or_pm);
return 0;
}