Originally Posted by
EverydayDiesel
so the data comes to me as
"2019-05-16T09:30:00-04:00" and i can make it
"2019-05-16T09:30:00-0400" but I am still lost on how to convert it to my current timezone
Code:
const char *time_details = "2019-05-16T09:30:00-0400";
struct tm tm2;
strptime(time_details, "%Y-%m-%dT%H:%M:%S%z", &tm2);
time_t tmIncomingTime = mktime(&tm2);
struct tm * tmLocalTime = localtime(&tmIncomingTime);
time_t t3 = mktime(tmLocalTime);
I think you didn't pay attention: strptime() dont change ALL the fields of struct tm. You NEED to initialize it FIRST:
Code:
struct tm tm2 = {0}; // init with all zeros
On how to discover your timezone, take a look:
Code:
/* test.c */
#include <stdio.h>
#include <string.h>
#include <time.h>
int main( void )
{
struct tm *tm;
time_t t;
char *s;
t = time(NULL);
tm = localtime(&t);
s = asctime(tm);
{ char *p = strchr( s, '\n' ); if ( p ) *p = '\0'; }
printf( "%s\n", s );
}
Code:
$ cc -g -o test test.c
$ gdb -q test
Reading symbols from test...done.
(gdb) l
1 /* test.c */
2 #include <stdio.h>
3 #include <string.h>
4 #include <time.h>
5
6 int main( void )
7 {
8 struct tm *tm;
9 time_t t;
10 char *s;
(gdb) l
11
12 t = time(NULL);
13 tm = localtime(&t);
14 s = asctime(tm);
15 { char *p = strchr( s, '\n' ); if ( p ) *p = '\0'; }
16 printf( "%s\n", s );
17 }
(gdb) b 14
Breakpoint 1 at 0x7ff: file test.c, line 14.
(gdb) r
Starting program: /mnt/vol2/Work/tmp/test
Breakpoint 1, main () at test.c:14
14 s = asctime(tm);
(gdb) p *tm
$1 = {tm_sec = 35, tm_min = 56, tm_hour = 19, tm_mday = 16, tm_mon = 4, tm_year = 119, tm_wday = 4, tm_yday = 135,
tm_isdst = 0, tm_gmtoff = -10800, tm_zone = 0x555555756500 "-03"}
(gdb) c
Continuing.
Thu May 16 19:56:35 2019
[Inferior 1 (process 10187) exited normally]
(gdb) q
$
Well... after localtime() call tm.tm_gmtoff is -10800 (I'm brazillian, UTC-3 zone here).
This offset is in seconds.
Beware! This is a GNU extension from glibc (probably not available on MSVC and other compilers).