Linux Operating System Computing time:
Main function: Time localtime gmtime asctime ctime mktime
Difftime strftime Gmstrftime
1.time () function
Prototype: time_t time (time_t * timer)
Function: Returns a time_t type of data representing the number of seconds from the cut time of January 1, 1970 00:00:00 (called The epoch Time of the UNIX system) to the current moment.
Then call localtime to convert the cut time represented by time_t to local time (we are +8 zones, 8 hours more than cut) and turn into a struct TM type,
Each data member of the type represents the date and time of the month and seconds.
2.localtime () function
Prototype: struct TM *localtime (const time_t *clock);
Return value: Returns a pointer to the TM structure
Function: Converts the number of seconds from 1970-1-1:00 to the current time system to calendar time. Convert obsolete zones! (Fri Oct 13 17:36:29 2017)
3.gmtime () function
Prototype: struct TM *gmtime (long *clock);
Return value: Returns a pointer to the TM structure
Function: Information in the time_t structure (from 1970-1-1:00 to the number of seconds offset by the current time system) is converted into the real world using the time date representation
method, for time zone conversion! 8 hours smaller than localtime! (Fri Oct 13 09:36:29 2017)
4.asctime () function
Prototype: char *asctime (const struct TM *tblock);
Function: convert date and time to corresponding string (for example: Fri Oct 13 17:36:29 2017)
5.ctime () function
Prototype: Char *ctime (const time_t *time);
Function: Converts the date and time to a string. (Example: Fri Oct 13 17:36:29 2017)
6.mktime () function
Prototype: time_t mktime (Strcut TM * timeptr);
Function: Converts the TM structure data referred to the number of seconds elapsed from the UTC time of January 1, 1970 0:0 0 seconds to the present.
7.difftime () function
Prototype: Double Difftime (time_t time1, time_t TIME0);
Function: Calculates the time interval, in seconds, and can only be accurate to seconds.
8.strftime () function
Prototype: size_t strftime (char *strdest,size_t maxsize,const char *format,const struct TM *timeptr);
Function: Format a time string
Return value: This function returns the number of characters placed in the string pointed to by strdest.
Description: Similar to sprintf (): Identifies a collection of formatting commands that begin with a percent sign (%), and the formatted output is placed in a string.
%a shorthand for the day of the week
%A full name of the week
Abbreviated%B 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 year, using week-based
%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, put 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 hundred percent semicolon
Tip: The same behavior as Gmstrftime (), the difference is that the return time is local time.
9.gettimeofday () function accurate to microseconds
Prototype: int gettimeofday (struct timeval*tv,struct timezone *tz)
Function: When using the Gettimeofday () function, the second parameter is generally empty, we generally just want to get the current time without having to get the timezone value
10.getSystemTime
Related types:
1.time_t is actually a long integer type, defined as: typedef long time_t;
2.timeval is a struct, defined in time.h as:
struct Timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* microseconds. */
};
Where Tv_sec is epoch (1970-1-1-:00) and the number of seconds to create the struct timeval, Tv_usec is the number of microseconds, which is a fraction of the seconds behind.
3.tm is a struct, defined as:
struct TM
{
int tm_sec; /* Represents the current number of seconds, the normal range is 0-59, but allowed to 61 seconds */
int tm_min; /* represents current score, range 0-59*/
int tm_hour; /* Number of hours from midnight, range 0-23 */
int tm_mday; /* Number of days in the current month, range 01-31 */
int Tm_mon; /* Represents the current month, starting from January, ranging from 0-11 */
int tm_year; /* Number of years since 1900 * *
int tm_wday; /* The number of days in a week, starting from Monday, with a range of 0-6. */
int tm_yday; /* days in year. [0-365] */
int tm_isdst; /* Daylight saving time flag DST. [ -1/0/1]*/
};
Test code:
Time
[OBJC]View PlainCopy
- #include <stdio.h>
- #include <time.h>
- int main ()
- {
- time_t T;
- T=time (NULL);
- printf ("The number of seconds since 1970-01-01 00:00 is:%d\n", t);
- return 0;
- }
Results:
[Email protected] time]#./a.out
The number of seconds since 1970-01-01 00:00 is:1507889358
LocalTime gmtime CTime asctime tzset
[OBJC]View PlainCopy
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main (int argc, const Char char*argv[])
- {
- struct TMTM *gmt, *local;
- time_t tt;
- Tzset (); //void tzset (void); Set time environment variable-time zone
- Tt=time (NULL); Equivalent to time (&TT);
- Charchar *str=ctime (&TT);
- printf ("CTime is:%s", str);
- Local=localtime (&TT);
- printf ("%4d%02d month%02d day%2d:%2d:%2d\n", local->tm_year+1900,local->tm_mon+1,local- >TM_MDAY,LOCAL->TM_HOUR,LOCAL->TM_MIN,LOCAL->TM_SEC);
- printf ("Lcoaltime is:%s", asctime (local));
- Gmt=gmtime (&TT);
- printf ("Gmtime is:%s", Asctime (GMT));
- return 0;
- }
Results:
[Email protected] time]#./a.out
CTime Is:mon Oct 16 10:40:39 2017
October 16, 2017 10:40:39
Lcoaltime Is:mon Oct 16 10:40:39 2017
Gmtime Is:mon Oct 16 02:40:39 2017
Difftime
[OBJC]View PlainCopy
- #include <stdio.h>
- #include <time.h>
- int main () {
- time_t T_start, t_end;
- T_start = time (NULL);
- Sleep (5);
- T_end = time (NULL);
- printf ("Time:%.0f s\n", Difftime (T_end,t_start));
- return 0;
- }
Results:
[Email protected] time]#./a.out
Time:5 s
Gettimeofday
[OBJC]View PlainCopy
- #include <stdio.h>
- #include <sys/time.h>
- int main () {
- struct timeval start, end;
- Gettimeofday (&start, NULL);
- Sleep (3);
- Gettimeofday (&end, NULL);
- int timeuse = 11000000 * (end. Tv_sec-start. tv_sec) + EndTv_usec-start. tv_usec;
- printf ("Time:%d us\n", timeuse);
- return 0;
- }
Results:
[Email protected] time]#./a.out
time:3000205 US
GetSystemTime
[OBJC]View PlainCopy
- #include <stdio.h>
- #include <sys/timeb.h>
- Long long GetSystemTime () {
- struct TIMEB t;
- Ftime (&t);
- return 1* t. Time + t. millitm;
- }
- int main () {
- Long long start=getsystemtime ();
- Sleep (3);
- Long long end=getsystemtime ();
- printf ("Time:%lld ms\n", End-start);
- return 0;
- }
Results:
[Email protected] time]#./a.out
TIME:3001 ms
Linux System Time Acquisition method