Linux C language Time Functions

Source: Internet
Author: User

In the manual, there are time ctime gmtime localtime asctime mktime settimeofday, gettimeofday, and other time functions.

There is also the Difference Between UTC and local time and these functions. Understand that these are essential for Linux time programming!

First, the basic struct, typedef, and define:

Time_t <time. h>

 
# Ifndef _ time_t
# Define_ Time_t/* avoid repeated definitions of time_t */
TypedefLongTime_t;/*The Time Value time_t is the alias of a long integer.*/
# Endif

The usage is time (& time_tvar ); in this way, the time function will return and assign the time_t type variable of this address to a value in seconds (from to the current number of seconds, its maximum value is 32-bit (4294967296), but it has to be half done without unsigned; 2011.12.22 is about 1324544508, and it is about several decades away from its fullness. Even if it is unsigned, it has only 1970 years since 132.95, by 2103 (4194288000), this value will overflow .. khan, I hope I don't want to take several bytes into consideration when I design it again. it should be as big as possible ...)

For the seconds from January 1, 1970 to the present, I also wroteAlgorithm:

Int Getdayssinceyear (){ //  0-365 not including this year!  
Time_t t; time (& T );
Struct TM * P;
P = localtime (& T );
Return P-> tm_yday;
}


Int Getdayssinceyear2 ( Int Year, Int Mon, Int Day) {// This is another algorithm in the range of 1-. Therefore, we should use-1 to achieve the above goal.
Int Myyday = 0 ;
Mon --; // Last month and before
While (Mon> 0 ){ // From 1 to last month !!!!!!!!!!
Switch (Mon ){
Case 1 :
Case 3 :
Case 5 :
Case 7 :
Case 8 :
Case 10 :
Case 12 : Myyday + = 31 ; Break ;
Case 4 :
Case 6 :
Case 9 :
Case 11 : Myyday + = 30 ; Break ;
Case 2 : Myyday + = 28 ;Break ;
Default : Break ;
}
Mon --;
}
Myyday + = Day; // And plus this month's days
If (Year % 4 = 0 ) Myyday + = 1 ; // 2 Yue if it is 29 days !!!
Return Myyday;
}

Int Getsecondsbytime ( Int Year, Int Month, Int Days, Int Hour, Int Minute, Int Second ){
// Input Format: 2011 11 11 11 11 11

Printf ( " % D-% d: % d = input time \ n " , Year, month, days, hour, minute, second );

Int Dayseconds = 24 * 60 * 60 ;
Seconds = hour * 60 * 60 + Minute * 60 + Second; // This year!
Seconds + = (getdayssinceyear () * dayseconds;
Year --; // So not including this year!
For (; Year> = 1970 ; Year --){ // 1970-last year!
Seconds + = 365 * Dayseconds;
If (Year % 4 = 0 ) Seconds + = dayseconds;
}
Seconds-= 8 * 60 * 60 ; // UTC time. Current + 8 hours Beijing!
Printf ( " % D = seconds (1970) in UTC \ n " , Seconds );
Return Seconds;
}

In the last second I got an operation: seconds-= 8*60*60; why? Because my computer sets the display local time (that is, not the UTC time)

If the UTC time is used, it will take more than 8 hours. (In Windows, the default time is local, not UTC, so there are often two non-synchronization issues, as long as you cancel the Linux UTC, in/etc/rc. set archlinux in files similar to Conf)

This time is obtained from the BIOS. The operating system must process this time. If the time is in UTC time, the BIOS time must be UTC + 0, we set the UTC timezone to Beijing UTC + 8, so Linux will time out the system time + 8 hours... (Later I changed to the local time, restarted the system, and recovered the time. Compared with the previous-8 hours, the time for some files was miserable .. the prompt is 8 hours later than now ...), so time () is the UTC + 0 time. If the system sets the UTC timezone to + 0, I think it's okay... khan .. who calls our time eight hours earlier than their UTC + 0 time zone.

Even if the system time is changed to the BIOS local time, it still gets the UTC time during programming. If we want to get the current time, it will take 8 hours. That's disgusting.

From time to second, there is no direct function in Linux, but the structure from second to time is ready !! Gmtime (& time_tvar) and localtime (& time_tvar );

The difference between the two is UTC, which is also disgusting... second is the second of the current time, UTC + 0, use gmtime to get the UTC + 0 time, localtime to get the local time UTC + 8, localtime is correct.

The current time is the following... or the time t is UTC + 0.

In short, UTC is annoying in this time programming .. another question is: in Java, there can be a subtle result. After a second, there will be a 1000*1000 carry. What if I use C to convert the Java time to the actual time ???

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.