Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Skillful use of Unix timestamp

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Unix timestamps contain rich time information. Many platforms have the concept of timestamps and provide a lot of system calls to operate timestamps.

These system calls are powerful, but I prefer to use convenient methods rather than call system functions. Here are two small examples I encountered when typing code. I used an unconventional approach to improve my efficiency a little bit.

Contemporary code becomes simple and efficient, even if only a little bit, the mood is very comfortable.

Definition: the number of seconds that have elapsed since January 1, 1970 (midnight in UTC/GMT), regardless of leap seconds.

Example1

Requirements, a structure stored in Memcached (which can be understood as a database). The main structure is as follows. Each player can get MAX experience every day. The exp field represents the experience gained today. This field is used to determine whether the highest experience of the day has been reached.

Suppose the player wants to add 100th experience, and exp+100 exceeds MAX, then he wants to add experience to MAX-exp. Before that, make a judgment.

First determine whether to gain experience for the first time today, if so, set the current time to time, and then set exp to 100

At this time, the general practice may be to extract the time and the current time, get their year, month and day, to determine whether they are on the same day, and if not, set the current time to time

Struct xx {

Int exp; / / experience

Unsigned int time; / / time

}

What I do is to get the current time and time, but I won't put the timestamp in this time, but a value after calculation

The time of a day is 60,60,24,86400.

# define DAYTIME 86400

Judge the current time / DAYTIME and time. If it is equal, it means the same day. If not, set the current time / DAYTIME to time.

Compared with the two methods, the first one needs to call two system functions to get tm, and one time system call, and then make three judgments, year, month and day.

Second, only one time system call is called, one division and one judgment are done.

Example2

Requirements, save a data in Memcached, need to set a timeout, expire in the early hours of every day, this kind of demand is very extensive, daily tasks and so on can have this kind of demand.

Then when you set this key, you need to specify the timeout ~ and the number of seconds remaining from this morning. PS (when the number of seconds is 0, it needs to be set to 86400, otherwise 0 in memcached means never timeout)

As a general practice, the hours, minutes and seconds of the day are obtained through the system function, and then the number of seconds elapsed on the day is calculated, and the last 86400-seconds is set to the timeout period.

What I did.

# define DAYTIME 86400

The number of seconds elapsed of the day can be obtained by the current time% DAYTIME, then 86400-the number of seconds elapsed of the day, set to timeout

Compared with the two methods, the first one requires an additional call to the system function, and the elapsed seconds of the day can be obtained by * 3600 + minutes * 60 + seconds when multiplying twice and adding twice.

Second, only one module operation is done, and the results obtained from the above steps are obtained.

If you have other efficient practices, you are welcome to share and learn from each other.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report