Converting decimal time representation to the unix era

I have time stored in a 64-bit int of the form 20110103101419 (i.e. representing 2011-01-03 10:14:19). How to convert this to seconds since 1970?

+3
source share
3 answers

My C is a little rusty, but looking at the other two answers, I will write the function as follows, returning the number of seconds from an era or -1 in case of an error.

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

time_t convertDecimalTime(long dt) {
  struct tm time_str;

  time_str.tm_isdst = -1;
  time_str.tm_sec   = dt%100; dt/=100;
  time_str.tm_min   = dt%100; dt/=100;
  time_str.tm_hour  = dt%100; dt/=100;
  time_str.tm_mday  = dt%100; dt/=100;
  time_str.tm_mon   = dt%100-1; dt/=100;
  time_str.tm_year  = dt%10000 - 1900;

  return mktime(&time_str);
}
+6
source

Refer to strftime . You can parse the input and use the% s format to display the number of seconds since Epoch,

+1
source

mktime struct tm, strftime %s, .

0

Source: https://habr.com/ru/post/1783189/


All Articles