Time Programming __ Programming

Source: Internet
Author: User
Tags local time string format

Note: Only for learning Exchange, reprint please specify the source

Calendar Time: The number of seconds from one point of time (usually January 1, 1970 0:0 0 seconds) to the time_t data type.

GMT (Greenwich Mean time GMT): Standard period provided by the Royal Greenwich Observatory.

local time: The time calculated based on the GMT and local timezone.

TM Structure Body:

The TM structure body (time Structure) is a structural body used in C language to represent dates and times, and several important temporal functions are related to it. It contains a total of 8 members, respectively:

struct TM

{

int tm_sec; Represents the number of seconds, between [0,61], the extra two seconds is used to handle the skip-second problem

int tm_min; Indicates a score, between [0,59]

int tm_hour; Represents the number of hours between [0,23]

int tm_mday; In the first few days of this month, between [1,31]

int Tm_mon; In the first few months of this year, between [0,11]

int tm_year; Indicates the number of years from 1900 now

int tm_wday; In the first few days of this week, between [0,6]

int tm_yday; In the first few days of this year, between [0,365], run year has 366 days

int tm_isdst; Indicates whether to save time for daylight

};

Time function

Header file

#include <time.h>

Function prototypes

time_t time (time_t timer);

return value

Return Calendar Time

Function: Get the current system time

Parameter description: Timer=null when the system's calendar time, timer= time value, to set the calendar time.

localtime function

Header file

#include <time.h>

Function prototypes

struct tm* localtime (const time_t *clock);

return value

Returns a pointer to a TM structure body

Function: Gets the local time through the calendar time, and saves it to the TM structure body.

Parameter description: Clock pointer variable pointing to the calendar time.

gmtime function

Header file

#include <time.h>

Function prototypes

struct TM *gmtime (long *clock);

return value

Returns a pointer to a TM structure body

function function: Get GMT by calendar time, and save to TM structure body.

Parameter description: Clock is a pointer variable that points to the calendar time.

Asctime function

Header file

#include <time.h>

Function prototypes

Char *asctime (const struct TM *tblock);

return value

Return string format: Weeks, months, days, hours: minutes: seconds, years

function function: Converts the time of the TM format to a string, such as Sat June 30 08:43:03 2005

Parameter description: Tblock The TM structure that records the time.

CTime function

Header file

#include <time.h>

Function prototypes

Char *ctime (const time_t *time);

return value

Return string format: Weeks, months, days, hours: minutes: seconds, years

function function: Converts a date and time to a string.

Parameter description: Time is a pointer variable that points to the calendar times, which is obtained by the function of the parameter.

Gettimeofday function

Header file

#include <time.h>

Function prototypes

int gettimeofday (struct timeval *tv, struct timezone);

return value

Successfully returned 0, failure returned-1

function function: Gets the time difference from today's early morning to the present, which is often used for calculation. Or to get the current time.

Parameter description: TV is the current timeval structure, TZ is the time zone structure.

struct Timeval

{

int tv_sec;//number of seconds

int tv_usec; Microsecond number

}

Settimeofday function

Header file

#include <time.h>

Function prototypes

int settimeofday (const struct Timeval *tv, const struct timezone);

return value

Successfully returned 0, failure returned-1

function function: Convert date and time to string

Parameter description: TV is the current timeval structure, TZ is the time zone structure

Instance:

#include <time.h>

#include <stdio.h>

int main (void)

{

struct TM *ptr = NULL;

struct TM *str = NULL;

time_t lt;

lt = Time (NULL);

ptr = Gmtime (&LT);

str = localtime (&LT);

printf (asctime (str));

printf (Asctime (PTR));

printf (CTime (&lt));

return (0);

}

Run Result:

[Root@localhost test]#./time

Mon May 6 18:55:08 2013

Mon May 6 18:55:08 2013

Mon May 6 18:55:08 2013

Summarize:

To get a local time string, you typically need three steps:

(1): The time function to obtain calendar times.

(2): The calendar time is converted to local time by the localtime function and stored in the TM structure body.

(3): Converts local time into strings via the Asctime function.

Of course, you can get the time string from the calendar time without step (2) directly using CTime. But in fact they are the same inside implementations.

In practice, there are many times when you do not need a fixed-format string for asctime function conversions. Typically, a TM structure representing the local time is obtained through the localtime function, and the custom time string format is exported through this member of the struct body. Therefore, the time string can be obtained more flexibly through the TM structure.

Instance:

#include <stdio.h>

#include <sys/time.h>

#include <time.h>

#include <string.h>

Char *weekday[] = {

"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"

};

Char *asctime (const struct TM *timeptr)

{

static Char ts[256];

Bzero (TS, 256);

sprintf (TS, "%s%d%d%02d:%02d:%02d%d",

Weekday[timeptr->tm_wday],

Timeptr->tm_mon+1,timeptr->tm_mday,

Timeptr->tm_hour,timeptr->tm_min,

Timeptr->tm_sec,timeptr->tm_year + 1900);

return (TS);

}

int main (int argc, char *argv[])

{

struct TM *ptm;

time_t t = time (NULL);

PTM = LocalTime (&t);

printf ("%s\n", Asctime (PTM));

return (0);

}

Run Result:

[Root@localhost test]#./asctime

Mon 5 6 20:03:46 2013

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.