Recently engaged in video retrieval, involving a lot of time calculation. Note down some basic usage as well.

Gmtime usage

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PNULL NULL
int p_time(time_t t)
{
    struct   tm  *timenow;
    if (t == -1) {
        printf("p_time input error. time = -1\n");
        return;
    }
    timenow = gmtime(&(t));
    if (timenow == PNULL) {
        return;
    }
    printf("%4d.%02d.%02d-%02d:%02d:%02d\n", \
        timenow->tm_year+1900,timenow->tm_mon+1,timenow->tm_mday,\
        timenow->tm_hour,timenow->tm_min,timenow->tm_sec);
}
void main()
{
    time_t test_time;

    time(&test_time);
    test_time = test_time + (8*60*60);

    printf("time = %lu\n", test_time);
    p_time(test_time);
}
Copy the code

Running results:

root@chenwr-pc:/home/workspace/test/tmp/FAT# date
Thu Mar 28 14:52:03 CST 2019
root@chenwr-pc:/home/workspace/test/tmp/FAT# gcc time.c -o run && ./runTime = 1553784724 2019.03.28-14:52:04Copy the code

Function to print out the current time.

  1. Gmtime () converts the information in the time_t structure indicated by the test_time parameter into the real world time-date representation, and then returns the result from the structure TM.
  2. Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT). For example, the time difference between mainland China and UTC is +8, which is UTC+8. The United States is UTC-5.
  3. The time function is used to get machine time (UTC) in seconds, all 8 time zones away from Beijing time, so add 86060. Finally, gmTime is used to convert.

2, the time function usage

The time() function returns a value, ** the time from 00:00:00, January 1, 1970 to the current time **, in seconds.Copy the code
#include <stdio.h>
#include <stdlib.h>
void main()
{
    time_t t;

    time(&t);
    printf("time = %lu\n", t);
}
Copy the code

Running results:

time = 1555576546
Copy the code

Tm structure

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;
     }; 
Copy the code

Ctime

Char *ctime(long time) char *ctime(long time) Char *ctime(long time) Char *ctime(long time) Char *ctime(long time) Char *ctime(long time) Char *ctime(long time)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
    time_t t;

    time(&t);
    printf("date and time: %s\n", ctime(&t));
}
Copy the code

Running results:

date and time: Thu Apr 18 16:43:32 2019
Copy the code

4, mktime usage

Converts the time to the number of seconds that have elapsed since January 1, 1970, returning -1 in case of an error

Time_t mktime(struct tm * timeptr); #include <time.h>

How do dates turn into seconds instances

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PNULL NULL
typedef signed long         INT32S;
#include <math.h>
#include <string.h>
int p_time(time_t t)
{
    struct   tm  *timenow;
    if (t == -1) {
        printf("p_time input error. time = -1\n");
        return;
    }
    timenow = gmtime(&(t));
    if (timenow == PNULL) {
        return;
    }
    printf("%4d.%02d.%02d-%02d:%02d:%02d\n", \
        timenow->tm_year+1900,timenow->tm_mon+1,timenow->tm_mday,\
        timenow->tm_hour,timenow->tm_min,timenow->tm_sec);
}
time_t date_to_sec(int year, int mon, int day, int hour, int min, int sec)
{
    struct tm date ;
    int time_zone = 8;

    memset(&date, 0, sizeof(date));
    date.tm_year = year-1900;
    date.tm_mon = mon-1;
    date.tm_mday = day;
    date.tm_hour = hour+time_zone;
    date.tm_min = min;
    date.tm_sec = sec;

    return mktime(&date);
}
void main()
{
    int my_time;

    my_time = date_to_sec(2019, 3, 20, 18, 10, 6);
    printf("date sec; %d\n", my_time);
    p_time(my_ime);
}
Copy the code

Running results:

date sec; 1553105406
2019.03.20-18:10:06
Copy the code

5. Reference materials

This article put the common function of time usage of the clear time functions in c language usage – – CSDN blog blog.csdn.net/wangluojisu wangluojisuan column…

Struct tm and time_t method (turn) the use of the time and date teaching blog – garden www.cnblogs.com/hhpjxbk/arc…