Use the time function in C language to get the system time

  • 2020-04-02 01:11:37
  • OfStack

The time() function can be used to get the current Calendar time of the computer system. The functions that deal with the date time are calculated based on the return value of this function. Its prototype is:
Time_t time (time_t * t);
If you have declared parameter t, you can return the current calendar time from parameter t, and you can also return the current calendar time by the return value, which is the number of seconds from a point in time (for example, 0 minutes 0 seconds on January 1, 1970) to the present time. If the argument is NULL, the function simply returns the current calendar time with a return value, as shown in the following example:

#include <SPAN style="FONT-FAMILY: Times New Roman"><stdio.h></SPAN>
int main(void) {
    time_t t;
    t=time(NULL);
    printf("The number of seconds since January 1, 1970 is  %dn",t);
    return 0;
}

The result of running is related to the time at that time. The result of my running is:  
The Calendar Time now is 1266637045
Where 1266637045 is the calendar time when I run the program. The number of seconds from 0 minutes 0 seconds on January 1, 1970 to this time.
Line 6 sets the parameter of the time function to NULL to get the exact number of seconds.
Line 6 can be rewritten as follows:
Time (& t);
The variable t holds the current date and time (equivalent to the return value of the function).

Related articles: