Linux Time functions

Source: Internet
Author: User
Tags central time zone epoch time set time string format

System Environment: ubuntu10.04

1. Common time types under Linux
time_t, struct TM, struct timeval, struct TIMESPEC

1.1 time_t Time Type
The time_t type is defined in Time.h:
typedef long time_t;
Visible, time_t is actually a long integer type. Its value is expressed as from UTC (Coordinated Universal Time) January 1, 1970 00:00 00 seconds
(also known as the epoch time of the Linux system) to the current moment of the number of seconds. Because of the time_t type length limit, it does not represent a time later than January 19, 2038 03:14 07 seconds (UTC).
In order to be able to represent a longer time, a 64-bit or longer number of shapes can be used to save the calendar time, which is not detailed here.
Use the time () function to get the time_t value for the current moment, and use the CTime () function to convert the time_t to a local time string.
Note: UTC time is sometimes also called GMT time, in fact UTC and GMT are almost the same concept. They all refer to Greenwich Mean time, except that UTC is more formally called.
The difference is that the former is an astronomical concept, and the latter is based on an atomic clock.

1.2 struct TM Time type
The TM structure is defined in time.h:
#ifndef _tm_defined
struct tm{
int tm_sec; /* seconds-the value interval is [0, 59]*/
int tm_min; /* min-value interval is [0, 59]*/
int tm_hour; /* When-the value interval is [0, 23]*/
int tm_mday; /* Day-value range is [1, 31]*/
int Tm_mon; /* Month-value interval is [0, 11]*/
int tm_year; /* Year-the value is 1900 to this year */
int tm_wday; /* Week-value range [0, 6],0 for Sunday, 1 for Week 1, and so on */
int tm_yday; /* Number of days from January 1 of each year-the value interval is [0, 365],0 represents January 1 */
int tm_isdst; /* Daylight saving time identifier, using daylight saving, TM_ISDST is positive, no daylight saving, TM_ISDST is 0, TM_ISDST is negative when not aware of the situation */
};
#define _tm_defined
#endif
The ANSI C standard says this time using the TM structure is expressed as the decomposition time (broken-down times).
Use Gmtime () and localtime () to convert the time_t time type to a TM structure;
Use Mktime () to convert the TM structure to the time_t time type;
Use Asctime () to convert a struct TM to a string form.

1.3 struct Timeval Time type
The TIMEVAL structure is defined in the time.h:
Struct tmieval{
time_t tv_sec; /* Seconds s*/
suseconds_t tv_usec; /* Microsecond us*/
};
The Set Time function Settimeofday () and the Get Time function gettimeofday () Use the event type as the argument.

1.4 struct Timespec Time type
The TIMESPEC structure is defined in time.h:
struct timespec{
time_t tv_sec; /* Seconds s*/
Long tv_nsec; /* nanosecond ns*/
};

2. Common time functions under Linux
Time functions commonly used under Linux are: Times (), CTime (), Gmtime (), localtime (), Mktime (), Asctime (), Difftime (), Gettimeofday (), Settimeofday ()

2.1 Time () function
Header files: #include <time.h>
function definition: time_t time (time_t *timer)
Function Description: This function returns the number of seconds that have elapsed since January 1, 1970 00:00 00 seconds. If time_t *timer a non-null pointer, the function also saves the return value to the memory pointed to by the timer pointer.
Return value: Success returns the number of seconds, Failure returns ((time_t)-1) value, and the reason for the error is stored in errno.
Cases:
time_t seconds;
seconds = Time ((time_t *) NULL);

2.2 CTime () function
Header files: #include <time.h>
function definition: char *ctime (const time_t *TIMEP);
Function Description: CTime () Converts the time_t time information pointed to by the parameter timep to the actual time date representation used and is returned as a string. The string format is: "Wed June 21:00:00 2012\n".
Cases:
time_t TIMEP;
TMEP = time (NULL);
printf ("%s\n", CTime (&TIMEP));

2.3 gmtime () function
Header file: #include <time.h>
function definition: struct TM *gmtime (const time_t *TIMEP)
Function Description: Gmtime () Converts the time_t time information pointed to by the parameter TIMEP to GMT time information expressed as a TM struct, and returns it as a struct tm* pointer.
Gmt:gmt is the central Time zone, Beijing in the East 8 district, the difference 8 hours, so Beijing time =gmt time + 8 hours.
Example:
int main (void)
{
Char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t TIMEP;
Struct TM *p_tm;
TIMEP = time (NULL);
P_tm = gmtime (&TIMEP);/* Get GMT Time */
printf ("%d-%d-%d", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm- >tm_mday);
printf ("%s%d:%d:%d\n", Wday[p_tm->tm_wday], P_tm->tm_hour, P_tm->tm_min, p_tm->tm_sec);
}

2.4 localtime () function
Header file: #include <time.h>
function definition: struct TM *localtime (const time_t *TIMEP);
Function Description: localtime () Converts the time_t time information pointed to by the parameter TIMEP to the local time zone (such as Beijing time = gmt+ hour) as represented by the TM structure.
Example:
int main (void)
{
Char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t TIMEP;
Struct TM *p_tm;
TIMEP = time (NULL);
P_tm = localtime (&TIMEP);/* Get local time zone times */
printf ("%d-%d-%d", (p_tm->tm_year+1900), (p_tm->mon+1), P_ Tm->tm_mday);
printf ("%s%d:%d:%d\n", Wday[p_tm->tm_wday], P_tm->tm_hour, P_tm->tm_min, p_tm->tm_sec);
return 0;
}

2.5 mktime () function
Header files: #include <time.h>
function definition: time_t mktime (struct TM *p_tm);
Function Description: Mktime () Converts the TM structure data pointed to by the parameter P_tm to the number of seconds elapsed GMT time from January 1, 1970 00:00 to 00 seconds.
Cases:
int main (void)
{
time_t TIMEP:
struct TM *p_tm;
TIMEP = time (NULL);
Pintf ("Time ():%d\n", TIMEP);
P_tm = local (&TIMEP);
TIMEP = Mktime (P_TM);
printf ("Time ()->localtime ()->mktime ():%d\n", TIMEP);
return 0;
}

2.6 Asctime () function
Header files: #include <time.h>
function definition: char *asctime (const struct TM *P_TM);
Function Description: Asctime () Converts the TM structure data that the parameter P_tm points to the actual used time date representation method and returns it as a string (same as the CTime function). The string format is: "Wed June 21:00:00 2012\n".
Cases:
int main (void)
{
time_t TIMEP;
TIMEP = time (NULL);
printf ("%s\n", Asctime (Gmtime (&TIMEP)));
return 0;
}

2.7 Difftime () function
Header files: #include <time.h>
function definition: Double difftime (time_t timep1, time_t timep2);
Function Description: Difftime () compares the parameters Timep1 and TIMEP2 for the same time, and returns the number of seconds between differences.
Cases:
int main (void)
{
time_t Timep1, TIMEP2;
TIMEP1 = time (NULL);
Sleep (2);
TIMEP2 = time (NULL);
printf ("The difference is%f seconds\n", Difftime (TIMEP1, TIMEP2));
return 0;
}

2.8 Gettimeofday () function
Header files: #include <sys/time.h>
#include <unistd.h>
function definition: int gettimeofday (struct timeval *tv, struct timezone *tz);
Function Description: Gettimeofday () The current time information into the structure of the TV point, the local time zone information is placed in the TZ point of the structure.
struct TimeZone prototypes:
struct timezone{
int tz_minuteswest; /*miniutes West of greenwich*/
int tz_dsttime; /*type of DST correction*/
};
Cases:
struct Timeval TV;
struct Timeval tz;
Gettimeofday (&TV, &tz);

Attached:
Use the time function family to get the times and output the specified format string example (Strftime () function):
int main (void)
{
Char strtime[20] = {0};
time_t TIMEP;
Struct TM *p_tm;
TIMEP = time (NULL);
P_tm = localtime (&TIMEP);
Strftime (strtime, sizeof (Strtime), "%y-%m-%d%h:%m:%s", P_TM);
return 0;
}

2.9 settimeofday () function
Header file: #include <sys/time.h>
#include <unistd.h>
function definition: int settimeofday ( const struct Timeval *tv, const struct timezone *gz);
Function Description: Settimeofday () sets the current time to the structure data that is directed by the TV. The current region information is set to the structure data that TZ points to.
Example:
int main (void)
{
Char t_string[] = "2012-04-28 22:30:00";
Struct TM Time_tm;
struct Timeval time_tv;
time_t TIMEP;
int ret = 0;

sscanf (t_string, "%d-%d-%d%d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, & Time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec);
Time_tm.tm_year-= 1900;
Time_tm.tm_mon-= 1;
Time_tm.tm_wday = 0;
Time_tm.tm_yday = 0;
TIME_TM.TM_ISDST = 0;

TIMEP = mktime (&TIME_TM);
Time_tv.tv_sec = TIMEP;
Time_tv.tv_usec = 0;

ret = Settimeofday (&TIME_TV, NULL);
if (ret! = 0)
{
fprintf (stderr, "Settimeofday failed\n");
Return-1;
}
return 0;
}

Linux Time 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.