gmtime
is a function in C that takes time (given in UTC) as its input and creates an object containing each unit of time (like seconds, hours, days, etc.) as its elements.
gmtime
takes only one mandatory argument, a pointer to an object of type time_t
.
gmtime
returns an object of type tm
. Below is the declaration of the tm
structure :
tm_sec
: Seconds passed in a minute seconds (0-59)
tm_min
: Minutes passed in an hour (0-59)
tm_hour
: Hours passed in a day (0-23)
tm_mday
: Days passed in a month (1-31)
tm_mon
: Months passed in a year (0-11)
tm_wday
: Days passed in a week (0-6)
tm_yday
: Days passed in a year (0-365)
tm_year
: Years passed since 1900
The following example uses the time
function to generate a UTC value for the current time that is then stored into a tm
object using the gmtime
function. Later, we print the individual components of the current time by accessing the specific elements of the tm
object:
#include <stdio.h>#include <time.h>int main () {// creating an object to put the current time intime_t current_time;// inserting th current UTC time in the objecttime(¤t_time);// creating a tm object to hold the output of// the gmt functionstruct tm *curr_t_struct;curr_t_struct = gmtime(¤t_time );//printing individual elements of the current timeprintf("This is the current year : %2d\n", (curr_t_struct->tm_year + 1900));printf("This is the current month number: %2d\n", curr_t_struct->tm_mon);printf("This is the number of hours past today : %2d\n", curr_t_struct->tm_hour );return(0);}
Free Resources