Linux get system Current time __linux

Source: Internet
Author: User
Tags local time month name

Recently, I'm looking at the problem of time programming in Linux:

1. Time-related structures under Linux
struct Timeval
{
int tv_sec;
int tv_usec;
};
The tv_sec is the number of seconds from the beginning of the morning, and the Tv_usec is microsecond (10E-6 second).

struct TIMEZONE
{
int tv_minuteswest;
int tv_dsttime;
};
Tv_minuteswest is the time difference between GMT and the west, and Tv_dsttime is the way to fix it.

struct TIMESPEC
{
long int tv_sec;
long int tv_nsec;
};
Tv_nsec is the nano-second (10E-9 second).

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_isdst;
};
Tm_sec table "Seconds", between [0,61], the extra two seconds is used to handle the skip-second problem.
Tm_min the number of "minutes" in the table, between [0,59].
Tm_hour the "time" number of the table, between [0,23].
Tm_mday table "Day of the Month", between [1,31].
Tm_mon table "The first few months of the year", between [0,11].
Tm_year to add 1900 to that year.
Tm_wday table "in the first few days", between [0,6].
Tm_yday table "The first few days of the year", between [0,365], leap years have 366 days.
Tm_isdst Whether the table is "Daylight saving Time".

struct Itimerval
{
struct Timeval it_interval;
struct Timeval it_value;
};
The It_interval member represents the initial value of the interval counter, while the It_value member represents the current value of the interval counter.

2. Get current time
In all UNIX, there is a function of time ()
time_t time (time_t *t);
This function returns the number of seconds from the start of the epoch, and if T is non-null, it fills the time value into T.

Linux provides gettimeofday () for certain requirements that require higher accuracy.
int gettimeofday (struct timeval * tv,struct timezone *tz);
int settimeofday (const struct Timeval * tv,const struct timezone);

struct TM format time function

struct TM * GMTIME (const time_t * t);
Converted to Greenwich. Sometimes called GMT or UTC.

struct TM * localtime (const time_t *t);
Convert cost to time. It can be used to modify TZ environment variables in a machine, different users to represent different times.

time_t mktime (struct TM *TP);
Convert TM into time_t format, using local time.

tme_t TIMEGM (Strut TM *TP);
Convert TM becomes time_t format, using UTC time.

Double Difftime (time_t t2,time_t t1);
Calculates the second difference.


3. Text Time Format function

char * asctime (struct TM *TP);
char * CTime (struct TM *TP);
Both functions convert the time format to the standard UNIX time format.
Mon May 3 08:23:35 1999

The CTime rate uses local time, and Asctime is represented by timezone information in the TM structure.

size_t strftime (char *str,size_t Max,char *fmt,struct TM *TP);
Strftime is a bit like sprintf, whose format is specified by FMT.

%a: Name of the day, abbreviation.
%A: Name of this day.
%b: Month name, abbreviation.
%B: Month names, full name.
%c: Is the same as the ctime/asctime format.
%d: The name of the day of the month, from 0.
%H: The day of the first few hours, 24-hour system, from 0.
%I: The day of the first few hours, 12-hour system, from 0.
%j: The first days of the year, from 0.
%m: The first few months of the year, from 0.
%m: The first minute of the hour, calculated from 0.
%p:am or PM.
%s: The first few seconds of the minute, calculated from 0.
%u: At the beginning of the year, the first day is calculated.
%w: At the beginning of the year, the first one is calculated.
%w: When the first few days, from 0.
%x: local date.
%x: local time.
%y: two-digit years.
%Y: four-digit years.
%Z: Abbreviation for time zone name.
%%:% symbol.

char * strptime (char *s,char *fmt,struct TM *TP);
Like scanf, the decoded string becomes the TM format.

%h: Same with%b and%b.
%c: Reads the%x and%x formats.
%c: Read%c format.
%e: Same as%d.
%d: Read%m/%d/%y format.
%k: Same with%h.
%l: Same with%i.
%r: Read the "%i:%m:%s%p" format.
%R: Read the "%h:%m" format.
%T: Read the "%h:%m:%s" format.
%y: Reads two-digit years.
%Y: Reads four-digit years.

Here's a small example of how to get the system's current time:
C codetime_tNowstructTM*timenow;CharSTRTEMP[255];    Time (&now);    TimeNow = LocalTime (&now); printf ("Recent time is:%s/n", Asctime (TimeNow));

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.