liunx time function and conversion method between time format and string

  • 2020-06-19 12:34:39
  • OfStack

We can think of Greenwich time as time coordinated time (GMT=UTC)

GMT: Greenwich Time

UTC: Time coordinated time

1, time_t


time_t time(time_t *t);

Get seconds from January 1, 1970 to date.

Type time_t, which is essentially a long integer (long) representing the number of seconds from 1970-01-01 00:00:00 to the current timing time, timeval is accurate to milliseconds

2, timeval

Type timeval, which is a structure type, and the struct timeval header file is time.h


struct timeval
{
time_t tv_sec;    /* Seconds. */
// seconds 
suseconds_t tv_usec; /* Microseconds. */
// microseconds 
};

Use:


struct timeval tv;
gettimeofday(&tv, NULL);
printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);

3, timezone


struct timezone
{ 
  int tz_minuteswest;   /* minutes west of Greenwich */ 
  int tz_dsttime;     /* type of DST correction */ 
};

4, struct tm

The tm structure, which is essentially a structure that contains the time fields


struct tm { 
    int tm_sec;   /* seconds after the minute - [0,59] */ 
    int tm_min;   /* minutes after the hour - [0,59] */ 
    int tm_hour;  /* hours since midnight - [0,23] */ 
    int tm_mday;  /* day of the month - [1,31] */ 
    int tm_mon;   /* months since January - [0,11] */ 
    int tm_year;  /* years since 1900 */ 
    int tm_wday;  /* days since Sunday - [0,6] */ 
    int tm_yday;  /* days since January 1 - [0,365] */ 
    int tm_isdst;  /* daylight savings time flag */ 
    };

Where tm_year represents the number of years between 1900 and the present, and tm_isdst is usually -1 if the value is set manually.

Function:

The tm structure contains, year, month, day, hour, minute and second

Use:


time_t t_time;
  struct tm *tmp_ptr = NULL;
  time(&t_time);
  printf("t_time = %d.\n", t_time);  
  tmp_ptr = gmtime(&t_time);

  printf("after gmtime, the time is: \
      \n yday = %d \
      \n wday = %d \
      \n year = %d \
      \n mon = %d \
      \n mday = %d \
      \n hour = %d \
      \n min = %d \
      \n sec = %d \
      \n isdst =%d.\n", 
      tmp_ptr->tm_yday,
      tmp_ptr->tm_wday,
      tmp_ptr->tm_year,
      tmp_ptr->tm_mon,
      tmp_ptr->tm_mday,
      tmp_ptr->tm_hour, 
      tmp_ptr->tm_min, 
      tmp_ptr->tm_sec,
      tmp_ptr->tm_isdst
      );

Print:


t_time = 1513841065.
after gmtime, the time is:       
yday = 354       
wday = 4       
year = 117       
mon = 11       
mday = 21       
hour = 7       
min = 24       
sec = 25       
isdst =0.

5, ctime/asctime


char *ctime(const time_t *timep);

To convert timep to real world time as a string, it differs from asctime in that the parameter form is not the same.


char *asctime(const struct tm* timeptr);

The time to convert the information in the structure to the real world is shown as a string.

char *ctime(const time_t *timer) returns a string representing the local time, based on the parameter timer.

Return string format: Thu Dec 21 13:59:57 2017

Use:


time_t curtime;
struct tm *tm_ptr = NULL;
time(&curtime);
tm_ptr = localtime(&curtime);
printf("ctime The current time of the transformation  = %s", ctime(&curtime));
printf("asctime The current time of the transformation  = %s", asctime(tm_ptr));

Print:

Current time of ctime conversion = Thu Dec 21 13:59:57 2017

Current time of asctime conversion = Thu Dec 21 13:59:57 2017

6, gmtime/localtime

struct tm* gmtime(const time_t *timep);

Converts the time represented by time_t to UTC time without time zone conversion, which is 1 pointer to the struct tm structure.

stuct tm* localtime(const time_t *timep);

It's similar to gmtime, but it's time zone conversion.

time_t curtime;

The gmtime function converts curtime to THE Greenwich mean time structure of struct tm, which basically means that gmtime turns out to be standard time in time zone 0

The localtime function converts curtime to the local time of the struct tm structure, which is the time of the current time zone rolled out with the time zone taken into account.

Here's an example:


struct timeval
{
time_t tv_sec;    /* Seconds. */
// seconds 
suseconds_t tv_usec; /* Microseconds. */
// microseconds 
};
0

Print:


t_time = 1513841065.
after gmtime, the time is:       
yday = 354       
wday = 4       
year = 117       
mon = 11       
mday = 21       
hour = 7       
min = 24       
sec = 25       
isdst =0.
after localtime, the time is:       
yday = 354       
wday = 4       
year = 117       
mon = 11       
mday = 21       
hour = 15       
min = 24       
sec = 25       
isdst =0.

7, mktime


time_t mktime(struct tm* timeptr);

Convert the time of struct tm structure to seconds from 1970 to the present.

mktime as opposed to gmtime/localtime, gmtime/localtime converts time_t to struct structure data, and mktime converts struct tm back to UTC time of type time_t

Use examples:


struct timeval
{
time_t tv_sec;    /* Seconds. */
// seconds 
suseconds_t tv_usec; /* Microseconds. */
// microseconds 
};
3

Print:


struct timeval
{
time_t tv_sec;    /* Seconds. */
// seconds 
suseconds_t tv_usec; /* Microseconds. */
// microseconds 
};
4

8 gettimeofday.


struct timeval
{
time_t tv_sec;    /* Seconds. */
// seconds 
suseconds_t tv_usec; /* Microseconds. */
// microseconds 
};
5

Returns the current distance from 1970 in seconds and the number of subtleties. tz is the time zone.

Use examples:


struct timeval
{
time_t tv_sec;    /* Seconds. */
// seconds 
suseconds_t tv_usec; /* Microseconds. */
// microseconds 
};
6

Print:


 A second time .tv_sec:1513843633
 Time microseconds .tv_usec:689374

9 difftime.


double difftime(time_t time1, time_t time2);

Returns the number of seconds between the two times

Use examples:


struct timeval
{
time_t tv_sec;    /* Seconds. */
// seconds 
suseconds_t tv_usec; /* Microseconds. */
// microseconds 
};
9

Print:


 Time interval between = 5.000000.

10, strftime


size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);

Use the strftime () function to format the time to the format we want.


(
%a  Short for day of the week 
%A  Full name of the day of the week 
%b  Short for month minute 
%B  Full name of the month 
%c  A time string of standard dates 
%C  The last two digits of the year 
%d 10 The day of the month in base 
%D  month / day / years 
%e  In the two-character field, 10 The day of the month in base 
%F  years - month - day 
%g  The last two digits of the year, using years based on weeks 
%G  Years are divided, using years based on weeks 
%h  Short for month name 
%H 24 An hour is an hour 
%I 12 An hour is an hour 
%j 10 The day of the year in base 
%m 10 The radix represents the month 
%M 10 The number of minutes in time 
%n  New line character 
%p  The local AM or PM Equivalent display of 
%r 12 Hour time 
%R  Display hours and minutes: hh:mm
%S 10 The number of seconds in base 
%t  Horizontal TAB 
%T  Display time: hh:mm:ss
%u  Day of the week 1 For the first 1 day   (the value from 0 to 6 That week 1 for 0 ) 
%U  The week of the year, make Sunday the day of the year 1 From the day (value 0 to 53 ) 
%V  The week of the year, using the year based on the week 
%w 10 The radix represents the day of the week (value from 0 to 6 On Sunday 0 ) 
%W  The week of the year, put the week 1 As the first 1 From the day (value 0 to 53 ) 
%x  Standard date string 
%X  Standard time string 
%y  No century 10 Base year (value from 0 to 99 ) 
%Y  With the century part 10 Base year 
%z . %Z  Time zone name. Null character is returned if the time zone name cannot be obtained. 
%%  percent 
)

Use examples:


time_t t_time;
  char buf[128];
  struct tm* tm_ptr = NULL;
  time(&t_time);
  tm_ptr = localtime(&t_time);
  //2017-12-21 18:53:58
  strftime(buf, 64, "%Y-%m-%d %H:%M:%S", tm_ptr);
  strftime(buf, 64, "%Y-%m-%d --- %H:%M:%S", tm_ptr);
  printf("formatTimeString = %s.\n", buf);

Print:


formatTimeString = 2017-12-21 18:53:58.
formatTimeString = 2017-12-21 --- 18:54:46.

11, strptime

The strftime function is the opposite of the strftime function, which formats a string into an tm structure.


size_t strftime(char *s,size_t maxsize,char *format,const struct tm *timeptr);

Use examples:


char buf[] = "2017-12-21 --- 18:54:46";
  struct tm tm_ptr;
  //2017-12-21 18:53:58
  strptime(buf, "%Y-%m-%d --- %H:%M:%S", &tm_ptr);
  printf("----strptime-----, the time is: \
      \n yday = %d \
      \n wday = %d \
      \n year = %d \
      \n mon = %d \
      \n mday = %d \
      \n hour = %d \
      \n min = %d \
      \n sec = %d.\n", 
      tm_ptr.tm_yday,
      tm_ptr.tm_wday,
      tm_ptr.tm_year,
      tm_ptr.tm_mon,
      tm_ptr.tm_mday,
      tm_ptr.tm_hour, 
      tm_ptr.tm_min, 
      tm_ptr.tm_sec
     );

Print:


----strptime-----, the time is:       
yday = 354       
wday = 4       
year = 117       
mon = 11       
mday = 21       
hour = 18       
min = 54       
sec = 46.

Related articles: