Muduo Network Programming Example (iii) timer

Source: Internet
Author: User
Tags current time time zones julian day local time time interval linux

Time in the program

The process of time in the program is a big problem, I intend to write a separate article to comprehensively discuss this issue. "Date and time in procedure" chapter II timing and timing of the article, with the "date and time in the program" the first chapter of the date calculation, the series is expected to have four articles.

In this blog I will briefly talk about the content directly related to programming, the more in-depth content to the above mentioned date and time feature articles.

In General Service-side programming, the common tasks associated with time are:

1. Get the current time, calculate the time interval;

2. Time zone conversion and date calculation; convert New York local time to local time in Shanghai; What is the date of the 100th day after 2011-02-05? Wait a minute

3. Timed operations, such as performing a task at a predetermined time, or performing a task after a period of delay.

The 2nd, which looks complicated, is actually the simplest. Date Calculation with Julian day number, time zone conversion with TZ database; the only trouble is daylight savings, but it can also be solved with TZ database. These operations are pure functions, and it is easy to use a set of unit tests to verify the correctness of the code. It is particularly noteworthy that using Tzset/localtime_r to do time zone conversions can be problematic in a multithreaded environment; My solution is to write a TimeZone class to avoid affecting the overall situation, which will be covered in the future date and time topics. The following article does not consider time zones, all UTC time.

The real trouble is the 1th and 3rd items. On the one hand, Linux has a lot of dazzling time-related functions and structures, how to choose in the program? On the other hand, the clock in the computer is not the ideal timer, it may drift or jump, and finally, the civil UTC time and leap seconds of the relationship also makes the timing task become complex and subtle. Of course, operations related to the current time of the system also make unit testing difficult.

Linux Time Functions

The timing function for Linux to get the current time:

* Time (2)/time_t (SEC)

* Ftime (3)/struct TIMEB (ms)

* Gettimeofday (2)/struct Timeval (microseconds)

* Clock_gettime (2)/struct Timespec (nanosecond)

* Gmtime/localtime/timegm/mktime/strftime/struct TM (This has nothing to do with current time)

A timer function that allows a program to wait for a period of time or schedule a task:

* Sleep

* Alarm

* Usleep

* Nanosleep

* Clock_nanosleep

* Getitimer/setitimer

* Timer_create/timer_settime/timer_gettime/timer_delete

* Timerfd_create/timerfd_gettime/timerfd_settime

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.