Obtain the detailed solution of time function based on Linux

  • 2020-04-02 00:46:35
  • OfStack

//-------------------------------------------------------------//
Asctime (represent time and date as a string)
# include < Time. H >
Define a function
Char * asctime(const struct tm * timeptr);
Function description
Asctime () converts the information in the tm structure referred to by the parameter timeptr into the time and date representation method used in the real world, and then returns the result as a string. This function has been converted from time zone to local time and the string format is: "Wed Jun 30 21:49:08 1993\n"
The return value
This string may be corrupted if the associated time and date function is called again. This function differs from ctime in that the parameters passed in are of a different structure.
Additional instructions
Returns a string representing the current local time and date.
sample

#include <time.h> 
main() 
{ 
time_t timep; 
time (&timep); 
printf( " %s " ,asctime(gmtime(&timep))); 
}
 perform 
Sat Oct 28 02:10:06 2000

//-------------------------------------------------------------//
Ctime (represents time and date as a string)
Header file
# include < Time. H >
Define a function
(const char * ctime time_t * timep);
Function description
Ctime () converts the information in the time_t structure referred to by the parameter timep to the time and date representation method used in the real world, and then returns the result as a string. This function has been converted from time zone to local time and the string format is "Wed Jun 30 21:49:08 1993\n". This string may be corrupted if the associated time and date function is called again.
The return value
Returns a string representing the current local time and date.
sample

#include<time.h> 
main() 
{ 
time_t timep; 
time (&timep); 
printf( " %s " ,ctime(&timep)); 
}
 perform 
Sat Oct 28 10 : 12 : 05 2000

Gettimeofday (gets the current time)
Header file
# include < Sys/time. H >
# include < Unistd. H >
Define a function
Int gettimeofday (struct timeval * TV, struct timezone * tz)
Function description
Gettimeofday () returns the current time to the tv-referential structure, and the local time zone information to the tz referential structure.
The timeval structure is defined as:
Struct timeval {
Long tv_sec;
Long tv_usec;
};
Timezone structure is defined as:
Struct timezone {
Int tz_minuteswest;
Int tz_dsttime;
};
Both structures are defined in /usr/include/sys/time.h. The state represented by tz_dsttime is as follows
DST_NONE
DST_USA
DST_AUST
DST_WET
DST_MET
DST_EET
DST_CAN
DST_GB
DST_RUM
DST_TUR
DST_AUSTALT
The return value
0 is returned on success, -1 is returned on failure, and the error code is stored in errno. Additional note that EFAULT pointer TV and tz refer to memory space that exceeds access rights.
sample

#include<sys/time.h> 
#include<unistd.h> 
main(){ 
struct timeval tv; 
struct timezone tz; 
gettimeofday (&tv , &tz); 
printf( " tv_sec; %dn " , tv,.tv_sec) ; 
printf( " tv_usec; %dn " ,tv.tv_usec); 
printf( " tz_minuteswest; %dn " , tz.tz_minuteswest); 
printf( " tz_dsttime, %dn " ,tz.tz_dsttime); 
}
 perform 
tv_sec: 974857339 
tv_usec:136996 
tz_minuteswest:-540 
tz_dsttime:0

//-------------------------------------------------------------//
Gmtime (gets the current time and date)
Header file
# include < Time. H >
Define a function
* * struct tm gmtime (const time_t timep);
Function description
Gmtime () converts the information in the time_t structure referred to by the parameter timep into the time and date representation method used in the real world, and then returns the result from the structure tm.
The structure tm is defined as
Struct tm
{
Int tm_sec;
Int tm_min;
Int tm_hour;
Int tm_mday;
Int tm_mon;
Int tm_year;
Int tm_wday;
Int tm_yday;
Int tm_isdst;
};
Int tm_sec represents the current number of seconds. The normal range is 0-59, but allows up to 61 seconds
Int tm_min represents the current score and ranges from 0 to 59
Int tm_hour from midnight, ranging from 0 to 23
Int tm_mday the number of days in the current month, ranging from 01 to 31
Int tm_mon represents the current month, from January, ranging from 0 to 11
Int tm_year the number of years since 1900
Int tm_wday the number of days in a week, from Monday, ranging from 0 to 6
Int tm_yday the number of days from January 1 this year up to now, ranging from 0 to 365
Int tm_isdst flag for daylight saving time
The time and date returned by this function is not time zone converted, but UTC time.
The return value
The return structure tm represents the current UTC time
sample

#include <time.h> 
main(){ 
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 
time_t timep; 
struct tm *p; 
time(&timep); 
p=gmtime(&timep); 
printf( " %d%d%d " ,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday); 
printf( " %s%d;%d;%dn " , wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec); 
}
 perform 
2000/10/28 Sat 8:15:38

//-------------------------------------------------------------//
Localtime (gets the local current time and date)
Header file
# include < Time. H >
Define a function
Struct tm *localtime(const time_t * timep);
Function description
Localtime () converts the information in the time_t structure referred to by the parameter timep to the time and date representation method used in the real world, and then returns the result from the structure tm. Please refer to gmtime() for the definition of structure tm. The time and date returned by this function has been converted to the local time zone.
The return value
The return structure tm represents the current local time.
sample

#include<time.h> 
main(){ 
char *wday[]={ " Sun " , " Mon " , " Tue " , " Wed " , " Thu " , " Fri " , " Sat " }; 
time_t timep; 
struct tm *p; 
time(&timep); 
p=localtime(&timep);  
printf ( " %d%d%d  " , (1900+p->tm_year),( l+p->tm_mon), p->tm_mday); 
printf( " %s%d:%d:%dn " , wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec); 
}
 perform 
2000/10/28 Sat 11:12:22

//-------------------------------------------------------------//
Mktime (converts time structure data into elapsed seconds)
Header file
# include < Time. H >
Define a function
Time_t mktime(strcut tm * timeptr);
Function description
Mktime () is used to convert the tm structure data referred to by parameter timeptr to the number of seconds elapsed in UTC from 0:0:0:00 on January 1, 1970 to date.
The return value
Returns the number of seconds elapsed.
sample

 
#include<time.h> 
main() 
{ 
time_t timep; 
strcut tm *p; 
time(&timep); 
printf( " time() : %d n " ,timep); 
p=localtime(&timep); 
timep = mktime(p); 
printf( " time()->localtime()->mktime():%dn " ,timep); 
}
 perform 
time():974943297 
time()->localtime()->mktime():974943297

//-------------------------------------------------------------//
Settimeofday (sets the current time)
Header file
# include < Sys/time. H >
# include < Unistd. H >
Define a function
Int settimeofday (const struct timeval * TV,const struct timezone *tz);
Function description
Settimeofday () sets the current time to the structure referred to by TV, and the local time zone to the structure referred to by tz. Refer to gettimeofday() for detailed instructions. Note that only root permission can use this function to change the time.
The return value
0 is returned on success, -1 is returned on failure, and the error code is stored in errno.
The error code
EPERM is not called settimeofday () by root, not enough.
EINVAL time zone or some data is incorrect and cannot be set correctly.
//-------------------------------------------------------------//
Time (get the current time)
Header file
# include < Time. H >
Define a function
Time_t time (time_t * t);
Function description
This function returns the number of seconds that have elapsed since January 1, 1970, in UTC, from 0:0:0 to the present. If t is not a null pointer, this function also stores the return value in the memory indicated by the t pointer.
The return value
The number of seconds is returned on success, the value ((time_t)-1) is returned on failure, and the reason for the error is in errno.
sample

#include<time.h> 
mian() 
{ 
int seconds= time((time_t*)NULL); 
printf( " %dn " ,seconds); 
}


Related articles: