/*
* print out the number of seconds since the epoch, the current time,
* and a pbreakdown of both the current local and UTC times
*
* Matt Bishop
* -- May 22 2024 original program
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
/*
* translate number of the day of the week into a 3-char string
* handle unknown and unexpected problems
*/
char *dow(int n)
{
/* get the day corresponding to n, which is to be 0-6 */
switch(n){
case 0: return("Sun");
case 1: return("Mon");
case 2: return("Tue");
case 3: return("Wed");
case 4: return("Thu");
case 5: return("Fri");
case 6: return("Sat");
default: return("???"); /* some other value, unknown day */
}
/* Can't happen */
fprintf(stderr, "dow: went past switch with all returns\n");
exit(1);
}
/*
* translate number of the month into a 3-char string
* handle unknown and unexpected problems
*/
char *moy(int n)
{
/* get the month corresponding to n, which is to be 0-11 */
switch(n){
case 0: return("Jan");
case 1: return("Feb");
case 2: return("Mar");
case 3: return("Apr");
case 4: return("May");
case 5: return("Jun");
case 6: return("Jul");
case 7: return("Aug");
case 8: return("Sep");
case 9: return("Oct");
case 10: return("Nov");
case 11: return("Dec");
default: return("???"); /* some other value. unknown month */
}
/* Can't happen */
fprintf(stderr, "dow: went past switch with all returns\n");
exit(1);
}
/*
* print all the details about the time
*/
void prtime(struct tm *parts)
{
#ifdef TIME_ZOME
printf("\tTime zone: %s (%d seconds offset from UTC time 0\n",
parts->tm_zone, parts->tm_gmtoff);
#endif
printf("\tIs it daylight savings time? %s\n",
parts->tm_isdst ? "yes" : "no");
printf("\tDay of the year: %d\n", parts->tm_yday);
printf("\tDay of the week: %d (%s)\n",
parts->tm_wday, dow(parts->tm_wday));
printf("\tYear (-1900): %d (%d)\n", parts->tm_year,
parts->tm_year + 1900);
printf("\tMonth: %d (%s)\n", parts->tm_mon, moy(parts->tm_mon));
printf("\tDay of the month: %d\n", parts->tm_mday);
printf("\tHour: %d\n", parts->tm_hour);
printf("\tMinute: %d\n", parts->tm_min);
printf("\tSecond: %d\n", parts->tm_sec);
}
/*
* we're off! The main routine
*/
int main(void)
{
time_t tick; /* seconds since the epoch */
char *tock; /* points to (ctime) date and time */
struct tm *parts; /* broken down time */
/*
* get the internal representation of time
*/
if ((tick = time(NULL)) == -1){
perror("time");
return(1);
}
printf("time in seconds is %ld\n", (long) tick);
/*
* print it as an intelligible character string
*/
if ((tock = ctime(&tick)) == NULL){
perror("ctime");
return(1);
}
/* now let's break it down */
if ((parts = localtime(&tick)) == NULL){
perror("localtime");
return(1);
}
printf("Here are the details of the local time:\n");
prtime(parts);
/* and here it is in UTC */
if ((parts = gmtime(&tick)) == NULL){
perror("gmtime");
return(1);
}
printf("Here are the details of the UTC time:\n");
prtime(parts);
/* th-th-that's all, folks! -- Porky Pig */
return(0);
}
|
ECS 36A, Programming & Problem Solving Version of April 2, 2024 at 12:13PM
|
You can get the raw source code here. |