© Slobodan Dmitrović 2021
S. DmitrovićModern C for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6643-4_49

Time and Date

Slobodan Dmitrović1  
(1)
Belgrade, Serbia
 

The <time.h> header declares functions that allow us to work with date/time. In this appendix, we explain how to obtain and format the current time and date.

The time function is declared inside the <time.h> header and returns the current date-time (date-time since epoch) as an object of type time_t. The function has the following signature:
timet_ time(time_t *arg);
The type time_t is a type capable of storing times. The time function can return the calendar time when arg is NULL:
#include <stdio.h>
#include <time.h>
int main(void)
{
      time_t mytime = time(NULL);
      printf("Obtained the current time to a mytime variable. ");
}
or store it inside an object pointed to by arg :
#include <stdio.h>
#include <time.h>
int main(void)
{
      time_t mytime;
      time(&mytime);
      printf("Obtained the current time to a mytime variable. ");
}
There are a number of steps involved when getting and formatting the time.
  • Get the current date-time using a time function.

  • Store/convert the obtained date-time into a tm struct using localtime or gmtime.

  • Format the obtained time using the strftime.

The following example obtains a date-time and stores it into a tm struct using a localtime function :
#include <stdio.h>
#include <time.h>
int main(void)
{
      time_t mytime = time(NULL);
      struct tm *now;
      now = localtime(&mytime);
      printf("Obtained and stored the current time. ");
}
The localtime function converts obtained local time to a tm calendar time. The tm structure holds the calendar date and time. The tm structure has the following predefined member fields of type int:
  • tm_sec – seconds from 0 to 60

  • tm_min – minutes from 0 to 59

  • tm_hour – hours from 0 to 23

  • tm_mday – days from 1 to 31

  • tm_mon – months from 0 to 11

  • tm_year – years since 1900

  • tm_wday – days since Sunday from 0 to 6

  • tm_yday – days since January the 1st from 0 to 365

  • tm_isdst – daytime saving value, positive if active, zero if not

The final thing left to do is to convert the tm time to a string using a strftime function and appropriate format specifiers:
#include <stdio.h>
#include <time.h>
int main(void)
{
      time_t mytime = time(NULL);
      struct tm *nowtm;
      char str[70];
      nowtm = localtime(&mytime);
      strftime(str, sizeof str, "%T", nowtm);
      printf("The time is: %s ", str);
}
Output:
The time is: 23:02:10

The strftime function converts the calendar date/time stored inside the tm structure to a string according to format specifiers used. Here we used the %T format specifier, which is the same as the %H:%M:%S format.

To format the obtained date/time as a date only, we can use the %D format specifier. Example:
#include <stdio.h>
#include <time.h>
int main(void)
{
      time_t mytime = time(NULL);
      struct tm *nowtm;
      char str[70];
      nowtm = localtime(&mytime);
      strftime(str, sizeof str, "%D", nowtm);
      printf("The date is: %s ", str);
}
Output:
The date is: 11/26/20

This example uses the %D format specifier inside the strftime function to output only the date part of the obtained date-time. The %D format specifier is equivalent to %m/%d/%y format.

When we populate the tm structure , we can access its individual fields. For example, if we need to access and display minutes and seconds as integers, we write:
#include <stdio.h>
#include <time.h>
int main(void)
{
      time_t mytime = time(NULL);
      struct tm *nowtm;
      nowtm = localtime(&mytime);
      printf("Minutes and seconds are: %d:%d ", nowtm->tm_min, nowtm->tm_sec);
}
Output:
Minutes and seconds are: 42:12

In this example, we do not convert the obtained date-time to a string strftime function. We simply use the tm structure’s fields representing minutes and numbers, called tm_min and tm_sec, and print them out using the printf function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.143.181