[c/c++][Linux] Convert timestamp to tm,time_t,timeval

tags: Linux  c/c++

ref

[C / c ++] [Linux] type of operations Summary notes

Description

The timestamp is 2020/03/25 11:09:42 .761197, where .761197 is microseconds.

code

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main()
{
    char time[100] = "2020/03/25 11:09:42 .761197";
    char time_format[100] = "%Y/%m/%d %H:%M:%S";
    printf("origin  is %s\n", time);

    /* to tm */
    struct tm tm;
    strptime(time, time_format, &tm);
    printf("tm      is %04d-%02d-%02d %02d:%02d:%02d\n", 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    /* to time_t */
    time_t tt;
    tt = mktime(&tm);
    printf("time_t  is %lu\n", tt);

    /* to timeval */
    struct timeval tv;
    tv.tv_sec = tt;
    tv.tv_usec = atoi(time + 21);		// 21 = strlen("2020/03/25 11:09:42 .")
    printf("timeval is %ld.%06ld\n", tv.tv_sec, tv.tv_usec);

    return 0;
}

output

origin  is 2020/03/25 11:09:42 .761197
tm      is 2020-03-25 11:09:42
time_t  is 1585105782
timeval is 1585105782.761197

Intelligent Recommendation

C # and Linux timestamp conversion

Original address: http://www.cnblogs.com/codemo/archive/2012/05/18/2507251.html Author:Code example Source:  The copyright belongs to the copyright belong to the blog, welcome to reprint, but thi...

Conversion between time_t, tm, and string in C/C++

1, the conversion of string to time_t The time_t here can pass an unsigned int type parameter, which means the number of seconds that 1970.1.1 0:0:0 starts counting, and returns the string structure s...

time_t tm timeval time string and conversion methods

time_t tm timeval time string and conversion methods 1, the conventional storage time 1) time_t type, this is essentially a long integer representing the number of seconds from 1970-01-01 00:00:00 to ...

Linux C gets timestamp print timestamp

The following is a timestamp interface that is commonly used in project development, which can be used directly. First, related interfaces Second, the code implementation Third, test It is relatively ...

C #, DateTime.Ticks property and convert a Unix timestamp

1. Related concepts DateTime.Ticks: 0001 represents 100 nanoseconds since midnight on January 1 12:00:00 experienced that Ticks property is 100 nanoseconds (1Ticks = 0.0001 ms). Unix timestamp: is the...

More Recommendation

C# timestamp and dateTime convert each other

Timestamp The concept of no time zone Unix timestamp: refers to the total seconds of seconds from January 01, January 01, 1970 (00:00 on January 01, 1970 (08:00 00 seconds on January 01, 1970). DateTi...

c++ time type details (time_t and tm)

  There are two common storage methods for storage time under linux. One is how many seconds have elapsed since 1970. One is to use a structure to store the year, month, day, minute, minute,...

How C/C++ converts the timestamp time_t into text char* string

Just use this _ltoa_s function, where 13 represents the length of time_stamp, and 10 represents decimal. The timestamp is 12 numbers, so create a char with a length of 13 to prevent memory overflow...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top