For example, get the current year:
/* Get current system time temporarily not used
int iyear = 0;
int sysyear = 0;
time_t now;
struct TM *timenow;
Time (&now);
TimeNow = LocalTime (&now);
Sysyear = timenow->tm_year+1900;
*/
How to get system time under Linux
You can use the LocalTime function to get the numeric value of the day and the month respectively.
The implementation method of C language for acquiring system time under Linux:
1. You can use the LocalTime function to obtain the numeric value of the day and the month respectively.
Header files for #include <time.h>//C language
#include <stdio.h>//C-language I/O
void Main ()
{
time_t now; Instantiate the time_t structure
struct TM *timenow; Instantiate a TM structure pointer
Time (&now);
The time function reads the current (international standard Time is not Beijing time) and then passes the value to now
TimeNow = LocalTime (&now);
The LocalTime function converts the time that you have obtained from hours to the time in your computer (the region you set up)
printf ("Local time is%s/n", Asctime (TimeNow));
In the previous sentence, the Asctime function converts the time into a character and outputs it through the printf () function.
}
Note: time_t is a struct that is defined in the time.h. The prototype of the TM structure is as follows:
struct TM
{
int Tm_sec;//seconds 0-61
int Tm_min;//minutes 1-59
int tm_hour;//hours 0-23
int tm_mday;//day of the month 1-31
int tm_mon;//months since Jan 0-11
int Tm_year;//years from 1900
int tm_wday;//days since Sunday, 0-6
int tm_yday;//days since Jan 1, 0-365
int Tm_isdst;//daylight Saving Time indicator
};
2. Linux provides gettimeofday () for certain requirements that require high accuracy.
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
int main (int argc, char **argv)
{
struct Timeval Start,stop,diff;
Gettimeofday (&start,0);
Do what you have to do ...
Gettimeofday (&stop,0);
Tim_subtract (&diff,&start,&stop);
printf ("Total elapsed:%d milliseconds/n", diff.tv_usec);
}
int tim_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
{
int nsec;
if (X->tv_sec > Y->tv_sec)
return-1;
if ((x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec))
return-1;
Result->tv_sec = (y->tv_sec-x->tv_sec);
Result->tv_usec = (y->tv_usec-x->tv_usec);
if (result->tv_usec<0)
{
result->tv_sec--;
result->tv_usec+=1000000;
}
return 0;
}
Linux C Language Acquisition system time information