Linux System Time Acquisition method

Source: Internet
Author: User
Tags epoch time local time month name set time

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


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. 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:


[OBJC]View PlainCopy
    1. #include <stdio.h>
    2. #include <time.h>
    3. int main ()
    4. {
    5. time_t T;
    6. T=time (NULL);
    7. printf ("The number of seconds since 1970-01-01 00:00 is:%d\n", t);
    8. return 0;
    9. }

[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
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main (int argc, const Char char*argv[])
  5. {
  6. struct TMTM *gmt, *local;
  7. time_t tt;
  8. Tzset (); //void tzset (void); Set time environment variable-time zone
  9. Tt=time (NULL);   Equivalent to time (&TT);
  10. Charchar *str=ctime (&TT);
  11. printf ("CTime is:%s", str);
  12. Local=localtime (&TT);
  13. 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);
  14. printf ("Lcoaltime is:%s", asctime (local));
  15. Gmt=gmtime (&TT);
  16. printf ("Gmtime is:%s", Asctime (GMT));
  17. return 0;
  18. }

[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


[OBJC]View PlainCopy
  1. #include <stdio.h>
  2. #include <time.h>
  3. int main () {
  4. time_t T_start, t_end;
  5. T_start = time (NULL);
  6. Sleep (5);
  7. T_end = time (NULL);
  8. printf ("Time:%.0f s\n", Difftime (T_end,t_start));
  9. return 0;
  10. }

[Email protected] time]#./a.out
Time:5 s


[OBJC]View PlainCopy
  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. int main () {
  4. struct timeval start, end;
  5. Gettimeofday (&start, NULL);
  6. Sleep (3);
  7. Gettimeofday (&end, NULL);
  8. int timeuse = 11000000 * (end. Tv_sec-start. tv_sec) + EndTv_usec-start. tv_usec;
  9. printf ("Time:%d us\n", timeuse);
  10. return 0;
  11. }

[Email protected] time]#./a.out
time:3000205 US


[OBJC]View PlainCopy
  1. #include <stdio.h>
  2. #include <sys/timeb.h>
  3. Long long GetSystemTime () {
  4. struct TIMEB t;
  5. Ftime (&t);
  6. return 1* t. Time + t. millitm;
  7. }
  8. int main () {
  9. Long long start=getsystemtime ();
  10. Sleep (3);
  11. Long long end=getsystemtime ();
  12. printf ("Time:%lld ms\n", End-start);
  13. return 0;
  14. }

[Email protected] time]#./a.out
TIME:3001 ms

Linux System Time Acquisition method

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: 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.