Analysis of time programming and realization principle in Linux (i) time programming for Linux application layer

Source: Internet
Author: User
Tags current time time zones linux

Introduction

We all live in time, but we can't think about it. What is time? It seems to be a question that can never be answered. As a programmer, however, there are always times when I have to think about what time is. For example, you need to know how long a piece of code is running, to log the timestamp of an event in the log file, or to need a timer to be able to do certain computer operations on a regular basis. I have found that in the computer world, time often has different meanings in different situations, so that people who try to think about it are confused. Thankfully, however, the time in Linux is understandable. So I'm going to talk about time and try to get a deeper understanding of the time problem in C programming in Linux. The main contents are as follows:

The 1th part is the time issue in the application. There are three aspects: program timing need; get current time; timer.

The 2nd part includes introduction of time hardware and principle of GlibC realization time function.

The 3rd and 4 parts are the Linux kernel time support and implementation principle.

Now begin the 1th part, to explore the problem of time programming in application development. In this section, all the example code is compiled and validated by GlibC 2.14, the kernel 2.6.33 Linux system. Readers who use a lower version of the GlibC and Linux kernel may not be able to execute correctly.

Get current time

The acquisition of time

In the program, we often have to output the current time of the system, such as every event in the log file to record its generation time. There are several ways to get the current time in C language, and they have a different time precision from second to nanosecond.

Table 1. C Time function

Gun/linux provides three standard APIs to get the current time ()/gettimeofday ()/clock_gettime (), which differs only in the time precision of the acquisition, and you can select the appropriate call as needed. Ftime () is a time call in some old systems, although many Linux versions support it, but only for forward compatibility, the newly developed software does not recommend using Ftime () to get the current time.

Time Display and Conversion

The time we get now is a number, no matter how precise it is, it represents only a difference. For example, the time () function with a precision of seconds returns an integer of type time_t. Assuming the current time is December 7, 2011 20:29 P.M. 51 seconds, then the time_t value is: 1323318591. We are 1,323,318,591 seconds away from January 1, 1970 0 o'clock. (Here January 1, 1970 Horizon is GMT, not Beijing time.) The time we discuss below, if not specifically, is Greenwich Mean Time, GMT, or UTC time.

The string "1,323,318,591 seconds" does not mean much to most people, and we prefer to see the "December 7, 2011" Display. So when we get the current time of seconds, milliseconds, or even nanoseconds, we often need to convert these numbers to the familiar time representation.

Because of the different countries, customs and time zones, there is no uniform format for the method of timing. In order to meet the needs of various time display, Standard C library provides many functions of time format conversion. The number of these functions is so confusing that it's hard to remember their usage. Here I use the illustrations from Michael Kerrisk in the book "Linux programming Interface" To make a general overview of these standard C functions.

Figure 1. Various time display format conversion function diagram

As you can see from the figure above, time ()/gettimeofday () after the current time from the kernel, the current value can be converted to a more readable display format by two broad classes of functions:

Related Article

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.