Advanced Programming in UNIX environment-time and date

Source: Internet
Author: User
Tags epoch time ranges time and date
The basic time service provided by the Unix kernel is the number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970 AD, The International Standard Time. This number of seconds is represented by the Data Type time_t.

1. The time function returns the current time and date:

Time_t time (time_t * calptr); the time value is always returned as a function. If the parameter is not blank, the time value is also stored in the Unit pointed by calptr.

2. Compared with the time function, gettimeofday provides a higher resolution (maximum bit in microseconds ).

IntGettimeofday ( StructTimeval * TP, Void*
Tzp );

The unique valid value of tzp is null, and other values produce uncertain results.

The gettimeofday function stores the current time in the timeval structure pointed to by TP, and this structure stores seconds and nuances.
StructTimeval {
Long
IntTV _sec; // number of seconds
Long
IntTV _usec; // Number of microseconds
}3. The localtime and gmtime functions convert the calendar time to the time represented by year, month, day, hour, minute, second, and Sunday, and store the time in a TM structure.

StructTM
{
IntTm_sec; // indicates the current number of seconds. The normal range is 0-59, but the value can be 61 seconds.
IntTm_min; // indicates the current score. The value ranges from 0 to 59.
IntTm_hour; // The number of hours counted from midnight, ranging from 0 to 23
IntTm_mday; // the number of days of the current month, range: 01-31
IntTm_mon; // indicates the current month. The value ranges from 0 to 11 from January 1, January.
IntTm_year; // number of years since January 1, 1900
IntTm_wday; // the number of days in a week. The value ranges from 0 to 6 from Monday.
IntTm_yday; // The number of days since January 1, January 1 this year, ranging from 0 to 365.
IntTm_isdst; // whether or not it takes effect during the period
};

StructTM
* Localtime (ConstTime_t
* Clock );
StructTM
* Gmtime (ConstTime_t
* Clock );

The difference between localtime and gmtime is: localtime converts the calendar time to the local time (considering the local time zone and the Daylight Saving Time ), while gmtime converts the day duration to the year, month, day, hour, minute, second, and Sunday of the international standard time.4. The mktime function uses the year, month, and day of the local time as the parameter and converts it to the time_t value.

Time_t mktime ( StructTM * tmptr );

5. The asctime and ctime functions generate a familiar 26-byte string, which is similar to the default output of the date (1) command, for example:

HuangCheng @ Ubuntu :~ $ Date
July 2013
Friday 05 18:25:52 CST

Char* Asctime (Const
StructTM * tmptr );
Char* Ctime (ConstTime_t calptr );

The sctime parameter is a pointer to a string such as year, month, and day, while the ctime parameter is a pointer to a calendar time.

 

Linux time functions

This article aims to provide technical documents for understanding various Linux time types and time functions.

1. Common time types in Linux

There are four common time types in Linux: time_t,StructTM,StructTimeval,StructTimespec

 
1.1 time_t time type

The time_t type is defined in time. h:

#Ifndef_ Time_t

#Define_ Time_t

TypedefLongTime_t;

#Endif

It can be seen that time_t is actually a long integer. The value indicates the number of seconds from the UTC (Coordinated Universal Time) time, January 1, January 1, 1970 (also known as the epoch time of the Linux system) to the current time.

Due to the length limit of the time_t type, the time it represents cannot be later than, January 1, January 19, 2038 (UTC ). To represent a longer period of time, you can use 64-bit or a longer integer to save the calendar time, which is not described here.

Use the time () function to obtain the time_t value of the current time, and use the ctime () function to convert time_t into a local time string.

Note: UTC time is also called GMT time. In fact, UTC and GMT are almost the same concept. They all refer to GMT standard time, But UTC is more formal. The difference between the two lies in that the former is an astronomical concept, while the latter is based on an atomic clock.

 
1.2
StructTM time type

The TM structure is defined in time. h:

#Ifndef_ Tm_defined

StructTM {

IntTm_sec;
/* Second-value range: [0, 59] */

IntTm_min;
/* Minute-value range: [0, 59] */

IntTm_hour;
/* Time-value range: [0, 23] */

IntTm_mday;
/* Day-value range: [1, 31] */

IntTm_mon;
/* Month-value range: [0, 11] */

IntTm_year;
/* Year-the value is from January 1, 1900 to the present */

IntTm_wday;
/* Week-value range [0, 6]. 0 indicates Sunday, 1 indicates week 1, and so on */

IntTm_yday;
/* Number of days starting from January 1, January 1-value range: [0,365]. 0 indicates January 1, January 1 */

IntTm_isdst;
/* Identifier when dimensions is used. If dimensions is not used, tm_isdst is positive. If dimensions is not used, tm_isdst is 0. If dimensions is not used, tm_isdst is negative */

};

#Define_ Tm_defined

#Endif

Ansi c uses the Time Representation of the TM structure as the broken-down time ).

You can use gmtime () and localtime () to convert the time_t time type to the TM struct;

Use mktime () to convert the TM struct to the time_t time type;

Use asctime ()StructConverts TM to a string.

1.3
StructTimeval time type

The timeval struct is defined in time. h:

Struct tmieval {

Time_t TV _sec;/* S */

Suseconds_t TV _usec;/* microsecond us */

};

Settimeofday () and gettimeofday () both use this event type as parameters.

1.4
StructTimespec time type

The timespec struct is defined in time. h:

StructTimespec {

Time_t TV _sec;/* S */

LongTV _nsec;
/* NS */

};

2. Common Linux time functions

References:

Http://blog.csdn.net/c395565746c/article/details/6621153

Http://fanqiang.chinaunix.net/a4/b8/20010527/201001267.html

Http://qgjie456.blog.163.com/blog/static/35451367200844102949365/

Http://hi.baidu.com/wangzhongli/blog/item/e260b3a1f388278746106461.html

Http://www.eefocus.com/xuefu2009/blog/10-03/187348_f456a.html

 

1. The time function returns the current time and date:

Time_t time (time_t * calptr); the time value is always returned as a function. If the parameter is not blank, the time value is also stored in the Unit pointed by calptr.

2. Compared with the time function, gettimeofday provides a higher resolution (maximum bit in microseconds ).

IntGettimeofday ( StructTimeval * TP, Void*
Tzp );

The unique valid value of tzp is null, and other values produce uncertain results.

The gettimeofday function stores the current time in the timeval structure pointed to by TP, and this structure stores seconds and nuances.
StructTimeval {
Long
IntTV _sec; // number of seconds
Long
IntTV _usec; // Number of microseconds
}3. The localtime and gmtime functions convert the calendar time to the time represented by year, month, day, hour, minute, second, and Sunday, and store the time in a TM structure.

StructTM
{
IntTm_sec; // indicates the current number of seconds. The normal range is 0-59, but the value can be 61 seconds.
IntTm_min; // indicates the current score. The value ranges from 0 to 59.
IntTm_hour; // The number of hours counted from midnight, ranging from 0 to 23
IntTm_mday; // the number of days of the current month, range: 01-31
IntTm_mon; // indicates the current month. The value ranges from 0 to 11 from January 1, January.
IntTm_year; // number of years since January 1, 1900
IntTm_wday; // the number of days in a week. The value ranges from 0 to 6 from Monday.
IntTm_yday; // The number of days since January 1, January 1 this year, ranging from 0 to 365.
IntTm_isdst; // whether or not it takes effect during the period
};

StructTM
* Localtime (ConstTime_t
* Clock );
StructTM
* Gmtime (ConstTime_t
* Clock );

The difference between localtime and gmtime is: localtime converts the calendar time to the local time (considering the local time zone and the Daylight Saving Time ), while gmtime converts the day duration to the year, month, day, hour, minute, second, and Sunday of the international standard time.4. The mktime function uses the year, month, and day of the local time as the parameter and converts it to the time_t value.

Time_t mktime ( StructTM * tmptr );

5. The asctime and ctime functions generate a familiar 26-byte string, which is similar to the default output of the date (1) command, for example:

HuangCheng @ Ubuntu :~ $ Date
July 2013
Friday 05 18:25:52 CST

Char* Asctime (Const
StructTM * tmptr );
Char* Ctime (ConstTime_t calptr );

The sctime parameter is a pointer to a string such as year, month, and day, while the ctime parameter is a pointer to a calendar time.

 

Linux time functions

This article aims to provide technical documents for understanding various Linux time types and time functions.

1. Common time types in Linux

There are four common time types in Linux: time_t,StructTM,StructTimeval,StructTimespec

 
1.1 time_t time type

The time_t type is defined in time. h:

#Ifndef_ Time_t

#Define_ Time_t

TypedefLongTime_t;

#Endif

It can be seen that time_t is actually a long integer. The value indicates the number of seconds from the UTC (Coordinated Universal Time) time, January 1, January 1, 1970 (also known as the epoch time of the Linux system) to the current time.

Due to the length limit of the time_t type, the time it represents cannot be later than, January 1, January 19, 2038 (UTC ). To represent a longer period of time, you can use 64-bit or a longer integer to save the calendar time, which is not described here.

Use the time () function to obtain the time_t value of the current time, and use the ctime () function to convert time_t into a local time string.

Note: UTC time is also called GMT time. In fact, UTC and GMT are almost the same concept. They all refer to GMT standard time, But UTC is more formal. The difference between the two lies in that the former is an astronomical concept, while the latter is based on an atomic clock.

 
1.2
StructTM time type

The TM structure is defined in time. h:

#Ifndef_ Tm_defined

StructTM {

IntTm_sec;
/* Second-value range: [0, 59] */

IntTm_min;
/* Minute-value range: [0, 59] */

IntTm_hour;
/* Time-value range: [0, 23] */

IntTm_mday;
/* Day-value range: [1, 31] */

IntTm_mon;
/* Month-value range: [0, 11] */

IntTm_year;
/* Year-the value is from January 1, 1900 to the present */

IntTm_wday;
/* Week-value range [0, 6]. 0 indicates Sunday, 1 indicates week 1, and so on */

IntTm_yday;
/* Number of days starting from January 1, January 1-value range: [0,365]. 0 indicates January 1, January 1 */

IntTm_isdst;
/* Identifier when dimensions is used. If dimensions is not used, tm_isdst is positive. If dimensions is not used, tm_isdst is 0. If dimensions is not used, tm_isdst is negative */

};

#Define_ Tm_defined

#Endif

Ansi c uses the Time Representation of the TM structure as the broken-down time ).

You can use gmtime () and localtime () to convert the time_t time type to the TM struct;

Use mktime () to convert the TM struct to the time_t time type;

Use asctime ()StructConverts TM to a string.

1.3
StructTimeval time type

The timeval struct is defined in time. h:

Struct tmieval {

Time_t TV _sec;/* S */

Suseconds_t TV _usec;/* microsecond us */

};

Settimeofday () and gettimeofday () both use this event type as parameters.

1.4
StructTimespec time type

The timespec struct is defined in time. h:

StructTimespec {

Time_t TV _sec;/* S */

LongTV _nsec;
/* NS */

};

2. Common Linux time functions

References:

Http://blog.csdn.net/c395565746c/article/details/6621153

Http://fanqiang.chinaunix.net/a4/b8/20010527/201001267.html

Http://qgjie456.blog.163.com/blog/static/35451367200844102949365/

Http://hi.baidu.com/wangzhongli/blog/item/e260b3a1f388278746106461.html

Http://www.eefocus.com/xuefu2009/blog/10-03/187348_f456a.html

 

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.