Time and time management in Linux

Source: Internet
Author: User
Tags function prototype month name volatile

Coordinated Universal time (UTC): Coordinated worlds, also known as World standard Times, is known as Greenwich Mean Time (Greenwich Mean time,gmt). For example, the time difference between the Chinese mainland and UTC is +8, which is utc+8. The United States is UTC-5.

Epoch (epoch) is defined as 0:00 midnight of January 1, 1970 of the Coordinated Universal Time (also UTC or Greenwich Mean Time or GMT).
Calendar time is the number of seconds elapsed since the epoch. time_t(i.e. long) #ifndef _time_t_definedtypedef long time_t; /* Time value */#define _TIME_T_DEFINED */Avoid multiple defines of time_t * * #endif header file:: time.h function prototype: time_t Time(time_t *timer) function: Get current system time TM#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; /* Date in one months-the value interval is [1,31] */
int Tm_mon; /* Month (starting from January, 0 for January)-the value range is [0,11] */
int tm_year; /* Year with a value equal to the actual year minus 1900 */
int tm_wday; /* Week – The value interval is [0,6], where 0 stands for Sunday, 1 for Monday, and so on */
int tm_yday; /* Number of days starting January 1 of each year – the value interval is [0,365], where 0 is January 1, 1 is January 2, and so on.
int tm_isdst; /* Daylight Saving time identifier, the TM_ISDST is positive when daylight savings is applied. Without daylight saving time, TM_ISDST is 0 and TM_ISDST () is negative when the situation is not known. */
};
#define _tm_defined
#endif Struct TM * gmtime(const time_t *TIMEP)//Covert time_t to GMT char * Asctime(const struct TM *TM)//Converts the broken-down time value TM into a string with the same format as CTime (). char * CTime(const time_t *TIMEP) struct TM * localtime(const time_t *TIMEP) We can display the time in a fixed format through the Asctime () function and the CTime () function, and the return value of both is a string of type char*. The time format returned is:
Day of the week month Date: minute: Second year \n\0
Example: Wed Jan 02:03:55 1980\n\0 size_tThe stddef.h header file has been tested to show that size_t is 4 bytes in a 32-bit system, and that size_t is 8 bytes in a 64-bit system, so using this type can enhance the portability of the program. Custom Time Format size_t strftime (char *strdest, size_t maxsize, const char *format, const struct TM *TIMEP) we can point to the format of the string according to format The TIMEP command places the time information saved in the strdest in the string pointed to by the strdest, storing up to maxsize characters in a maximum of one. The function returns the number of characters placed in the string pointed to by strdest. %a shorthand for the day of the week
%A full name of the week
%b of the Month
Full name of the%B month
Time string for the date of%c standard
After two digits of the%c year
The day ordinal of a month in%d decimal notation
%d Month/day/year
%e the day ordinal of a month in a two-character field, in decimal notation
%F year-month-day
%g two digits of the year, using week-based
%G years, using week-based years
%h Abbreviated month name
%H 24-Hour Hour
%I 12-Hour Hour
%j decimal indicates the day ordinal of the year
%m the month represented by decimal
%M minutes in a 10 o'clock-hour representation
%n New Line character
%p equivalent display of the local AM or PM
%r 12 hours of time
%R display hours and minutes: hh:mm
%s number of seconds in decimal
%t Horizontal Tab
%T display time seconds: hh:mm:ss
%u days of the week, Monday for the first day (values from 0 to 6, Monday to 0)
%u year of the week, the Sunday as the first day (value from 0 to 53)
%V Week of the year, using week-based
%w Decimal Day of the week (value from 0 to 6, Sunday is 0)
%W Week of the year, Monday as the first day (value from 0 to 53)
Date string for%x standard
Time string for%x standard
%y decimal Year without century (values from 0 to 99)
%Y 10 year with century part
%z,%z the time zone name and returns a null character if the time zone name cannot be obtained.
Percent percent semicolon # include <stdio.h>
#include <time.h>

void Main (void)
{
struct TM *newtime;
Char buf[128];
time_t time;
Time (&time); Time=time (NULL);
Newtime=localtime (&time);
Strftime (BUF, "Today is%A, day%d of%B in the year%y.\n", NewTime);
printf (BUF);
}

Operation Result:
Today is Saturday, Day's July in the year 2005. #include <stdlib.h> int system (const char *command); Calculate Time Difference

NAME
Difftime-calculate Time Difference

Synopsis
#include <time.h>

Double Difftime (time_t time1, time_t TIME0);

Unit is seconds.

Decomposition conversion Time

time_t mktime (struct TM *tm); Linux kernel timekernel/include/linux/time.h#ifndef _struct_timespec
#define _struct_timespec
struct Timespec{
__kernel_time_t tv_sec; /* seconds */
Long tv_nsec; /* nanoseconds */
};
#endif

struct timeval {
__kernel_time_t tv_sec; /* seconds */
__kernel_suseconds_t tv_usec; /* microseconds */
};


Source/arch/x86/include/asm/posix_types_32.h

typedef long __KERNEL_TIME_T;TYPEDEF long __kernel_suseconds_t; When the system starts, the kernel initializes the actual time by reading the RTC (real-time clock), which is stored in the XtimeVariable. Xtime is a global variable in the kernel: extern struct TimespecXtime; beat rate(tick rate): How many clock interrupts the system generates in a second, expressed in Hz by the kernel. #define HZ//i386 beats(tick): two times the clock interrupt interval. Global variables jiffiesUsed to record the total number of beats that have been generated since the system started. Defined by the <linux/jiffies.h>extern unsigned long volatile jiffies;volatile instructs the compiler to access the variable every time it is accessed from the main memory, rather than through the variable alias in the register. The operating time of the system since startup =jiffies/hz, in seconds. Kernel CodeAsmlinkage Long Sys_time (time_t __user *tloc);
Asmlinkage Long Sys_stime (time_t __user *tptr);
Asmlinkage long sys_gettimeofday (struct timeval __user *tv,
struct timezone __user *tz);
Asmlinkage long sys_settimeofday (struct timeval __user *tv,
struct timezone __user *tz); In Source/include/linux/types.h, there are many definitions of kernel types. typedef int __KERNEL_ Timer_t;typedef __kernel_timer_t timer_t; Reference: http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html (with example)

Time and time management in Linux

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.