Common computing ideas based on Linux integer time

Source: Internet
Author: User
Tags time zones time and seconds

Common computing ideas based on Linux integer time

The last time I shared the Linux time zone details and common time functions, I believe you have some knowledge about the use of common Linux time functions, when you encounter requirements such as getting time at work, you must be able to handle it well. This article provides some simplified and commonly used computing ideas based on Linux integer time, and tries to enhance your understanding of time processing from another perspective, hoping to help you.

Overview

In the development of backend servers, it is often necessary to compare and calculate data based on date and time. Similar functional requirements may be: Determine the day of the week, determine whether two times are in the same day, whether they are in the same week, and whether the current time is in a specific time period of the day. Although the system function localtime () can obtain detailed information about the date, the obtained information is so detailed that localtime () is used in some specific simple functions () there is actually excessive overhead. For some simple judgments, we recommend that you use simpler, more primitive, and more understandable methods.

Computing ideas

In Unix/Linux, the system time is expressed as time_t, which is essentially an integer value. The value indicates that it starts from a historical benchmark (January 1, January 1, 1970 at GMT ), the number of seconds that will last until the current time point. In Linux, time_t is defined as a long type, that is, a signed integer.

Considering that China and Greenwich Mean Time zones are different, for China, the benchmark start point of time is January 1, 1970 eight o'clock. For any time zone, the time_t representation rules can be expressed.

As shown above, T0 = 0 indicates the start time; T1 indicates the zero point time on the first day after T0; T2 indicates the zero point time on the second day; it can be seen that for different time zones, the regular difference is that T1 has different values. Starting from the time T1, T1, T2, T3. .. Tn is an equidifference sequence. The tolerances are the time and seconds of a day, and are recorded as D = 86400 (60*60*24 ).

For any time, it can be expressed:

T = T1 + k x D + m .... Formula 1

T1 is a constant related to the time zone. m is the number of seconds in the current day. k can be interpreted as the number of days in history.

After deformation, k = (t-T1-m)/D can be obtained.

M <D can be further simplified:

K = (t-T1)/D .... Formula 2

K is the day of the t time, the number of days since T0.

For time t, the time at the zero point of the current day:

Tz = T1 + (t-T1)/D × D .... Formula 3

Tz Is the zero time of the day where t time is located.

 

Based on formula 2, we can determine whether t1 and t2 are the same day at any two times. Based on Formula 3, we can find the time period where t1 is located on the current day. Based on these two formulas, we can expand more date calculations related to the day. It is easy to see that the formula is only used for integer calculation.

 

We can simulate the above idea for calculating the week. The value of T1 is the start time of the first week, for example, on Monday. The value of D is 604800 (86400*7) of the second of a week ).

Through any time t, We can find its current zero point time, find the start time of the week, and then through a simple comparison, it is also easy to calculate the day of the Day and other related extensions. I will not go into detail here.

Common function implementation

// Obtain the value of tNow time at on the current day. The value at is used as the first second of the day.

Time_t GetTodayZeroTime (time_t tNow)

{

Return (tNow-57600)/86400) * 86400 + 57600 );

}

 

// Determine whether two times are on the same day. The concept of one day is 00:00:00 to 23:59:59.

Bool IsInSameDay (time_t tTm1, time_t tTm2)

{

Return (tTm1-57600)/86400 = (tTm2-57600)/86400 );

}

 

// Obtain the start time of the week of the tNow time, that is, 00:00:00 on Monday of this week.

// Calculation method. The integer time at on January 7, 316022400 is Monday, and is (by China time zone)

Time_t GetWeekBeginTime (time_t tNow)

{

Return (tNow-316022400)/604800*604800 + 316022400 );

}

 

// Obtain the end time of the week of the tNow time, that is, on Sunday of this week.

Time_t GetWeekEndTime (time_t tNow)

{

Return (tNow-316022400)/604800*604800 + 316627199); // 316022400 + 604800-1 );

}

 

// Determine whether two hours are in the same week. The concept of a week is 00:00:00 from Monday to 23:59:59 on Sunday.

Bool IsInSameWeek (time_t tTm1, time_t tTm2)

{

Return (tTm1-316022400)/604800 = (tTm2-316022400)/604800 );

}

Code Description
# Include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <time. h> time_t GetTodayZeroTime (time_t tNow) {return (tNow-57600)/86400) * 86400 + 57600);} bool IsInSameDay (time_t tTm1, time_t tTm2) {return (tTm1-57600)/86400 = (tTm2-57600)/86400);} bool IsInSameWeek (time_t tTm1, time_t tTm2) {return (tTm1-316022400) /604800 = (tTm2-316022400)/604800);} time_t GetWeekBeginTime (time_t tNow) {return (tNow-316022400)/604800*604800 + 316022400 );} time_t GetWeekEndTime (time_t tNow) {return (tNow-316022400)/604800*604800 + 316627199); // 316022400 + 604800-1);} int main (int argc, char ** argv) {time_t currtime, one_hour_after, one_day_after, one_week_after; time (& currtime); hour = currtime + 3600; // one hour later, one_day_after = currtime + 86400; // one day later one_week_after = currtime + 604800; // printf ("Today zero time => % d \ n", GetTodayZeroTime (currtime) after one week )); printf ("Week begin time ==> % d \ n", GetWeekBeginTime (currtime); printf ("Week end time ==> % d \ n ", getWeekEndTime (currtime); printf ("Is in same day ==>( currtime | one_hour_after = % d), (currtime | one_day_after = % d) \ n ", isInSameDay (currtime, one_hour_after), IsInSameDay (currtime, one_day_after); printf ("Is in same week ==> (currtime | one_week_after = % d ), (one_day_after | one_week_after = % d) \ n ", IsInSameWeek (currtime, one_week_after), IsInSameWeek (one_day_after, one_week_after); return 0 ;}
 

Result description

[Root @ VM_174_171_CentOS unixtime] # g ++-g-o unixtime_simplify unixtime_simplify.cpp

[Root @ VM_174_171_centos unixtime] #./unixtime_simplify

Today zero time ==> 1445097600

Week begin time => 1444579200

Week end time => 1445183999

Is in same day ==> (currtime | one_hour_after = 1), (currtime | one_day_after = 0)

Is in same week ==> (currtime | one_week_after = 0), (one_day_after | one_week_after = 1)

[Root @ VM_174_171_centos unixtime] # date

Sun Oct 18 13:17:37 CST 2015

[Root @ VM_174_171_centos unixtime] # date-d @ 1445097600

Sun Oct 18 00:00:00 CST 2015

[Root @ VM_174_171_centos unixtime] # date-d @ 1444579200

Mon Oct 12 00:00:00 CST 2015

[Root @ VM_174_171_centos unixtime] # date-d @ 1445183999

Sun Oct 18 23:59:59 CST 2015

Application Example

In some activities and task logic, a value similar to a natural day statistical value is often required. After a day, the value is cleared.

For this requirement, we usually use [value, Update time] to compare the access time. If the access time exceeds the validity period, it is cleared. For example, when GetValue () and AddValue () are used to determine the last update time of a value, t_upd. If IsInSameDay (t_upd, t_now) is used, the current value is still valid, otherwise, the operation is performed after the value is cleared. Every time you modify a value, t_upd is updated to the current time.

Internationalization considerations

For different time zones, the formula differs only from the value T1, and the form and use of the formula do not need to be changed.

One way is to define T1 as a macro and use different T1 values for versions in different time zones during internationalization.

Another way is to define T1 as a global variable and use the localtime () function of the system when the server starts, so that T1 can be initialized according to the local time zone.

Rules not applicable to year and month

Because the number of days per year and the number of days per month are not fixed, the calculation method in this article is not applicable to the determination of the number of days per month. Based on previous experience, the function requirements for specific dates in a specific month are not very common. For these functions, it is easier to use the localtime () function.

This article permanently updates the link address:

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.