in the Linux environment to write programs in C language to get the current time as long as the call its internal function. These functions are in the Time.h header file, the first function prototype:
① time_t Time (time_t *t), the Linux man is also handy to find instructions for this function:
In the command-line mode of the Linux environment, you can find the description of the time function by entering man 2 time, which calculates the total number of seconds from January 1, 1970 to the current.
A function prototype for the second function is:
②STRUCT TM *localtime (const time_t *TIMEP)
A description of the time function can be found by entering man localtime in the command-line mode of the Linux environment. With these two functions you can write the program, the program is as follows:
- #include <stdio.h>
- #include <time.h>
- int main (void)
- {
- time_t t;
- T = time (NULL);
- printf ("Time in Seconds:%d\n", t);
- struct tm *p = localtime (&t);
- printf ("%d-", 1900+ p->tm_year); Year needs to add 1900
- printf ("%d-", 1+p->tm_mon); Month needs to add 1
- printf ("%d\t", p->tm_mday); Day
- printf ("%d:", p->tm_hour); Hour
- printf ("%d:", p->tm_min); Minute
- printf ("%d\t", p->tm_sec); Second
- printf ("week=%d\n", p->tm_wday); Week
- return 0;
- }
The result of the operation is:
C language Gets the current time (Linux environment)