UNIX System Management: System call-time system call

Source: Internet
Author: User
Keywords UNIX System Management time system call
Tags computer computer hardware counter data type files functions guided hardware

In the computer hardware contains a system clock, the system is guided to read the clock, and then maintain its own clock counter, using the clock counter to the system events, such as the creation of files or user login to give time records. The clock value can also be used by the time () system call for your own program:

#include <sys/types/h>

#include (time.h>

time_t time (time_t *loc);

This system call returns a time_t value, and if the parameter loc is a pointer to a variable of this type, the return value is also copied into the variable that the pointer refers to.

The data type time_t is actually defined by the TypeDef and can be accessed through <time.h>, and its basic http://www.aliyun.com/zixun/aggregation/18278.html "> data type is long. Therefore the time () system call returns a long integer as the value of the current clock, but this is a strange format, that is, the number of seconds elapsed since midnight of January 1, 1970 GMT (I assume that this clock must begin at some point ...).

One side question is: how long does it take a long integer to record? If it is limited to a 32-bit long positive integer range (negative values have no meaning for time!) ), it is just over 68 years, or until early 2038 (there is plenty of time to think of an alternative).

This time format is actually quite useful, and if you want to resolve the number of times between two events (such as your login time and the time of your exit system), you can get the seconds between two events by subtracting the later time from the earlier time.

However, this time format is inconvenient when you want to display the day, month, or day. To simplify these operations, the standard library contains some useful functions for you to convert. Two such functions are:

#include <time.h>

struct TM *gmtime (time_t *loc);

struct TM *localtime (time_t *loc);

Both functions take a parameter loc, which is a pointer to a variable that contains the value of the number of seconds since 1970. Here both functions return a pointer to a struct that contains the required information:

struct TM {

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int Tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_lsdst;

}

Domain Tm_hour,tm_min, and tm_sec in a 24-hour clock format to give the clock time, Tm_mday is in the range of 1 to 31 days of the month, Tm_mon is the range of 0月-November, January (whose) as 0,tm_ Year is the number of years since 1900, Tm_wday is a week of 0 to 6, Sunday (Sunday) for 0,tm_yday is the range of 0 to 365 days of the Year, January 1 (1 whose) as 0,tm_ ISDST is a flag that stipulates whether daylight saving time is valid (if the information is available).

The main difference between the two functions is that gmtime () gives its time information relative to GMT (GMT), while LocalTime () gives its temporal information relative to your local time zone (setting the local time zone during system setup and configuration).

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.