question  mktime issue

afrogonabike (3)   Link to this post
Hi,

I'm trying to use the mktime function. In order to test I thought I'd try and get it to return time 0 i.e. 01/01/1970 00:00:00. The code I'm using is as follows:

1
2
3
4
5
6
7
8
9
10
11
struct tm timeinfo;
memset(&timeinfo,0,sizeof(timeinfo));
timeinfo.tm_hour = 0;
timeinfo.tm_min = 0;
timeinfo.tm_sec = 0;
timeinfo.tm_year = 70;
timeinfo.tm_mday = 1;
timeinfo.tm_mon = 0;
timeinfo.tm_isdst = 0;

std::cerr <<std::endl << mktime(&timeinfo) << std::endl;


The output i get is:


-3600


This is precisely one hour out. I have tried setting tm_isdst to -1,0 and 1 and get the same result!

Anyone any ideas?

Thanks,

Tim
kbw (1511)   Link to this post
I get zero. It could be a DST issue.
afrogonabike (3)   Link to this post
I've tried setting tm_isdst to -1,0 and 1 which doesn't make any difference.
kbw (1511)   Link to this post
What platform and timezone are you?

I've used gcc under cygwin in GMT.
Last edited on
afrogonabike (3)   Link to this post
Using gcc on a linux pc in GMT.
kbw (1511)   Link to this post
-3600 on gcc on FreeBSD amd64.

Interestingly enough, going the other way with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <time.h>
#include <memory.h>

int main()
{
        struct tm timeinfo;
        memset(&timeinfo, 0, sizeof(timeinfo));

        timeinfo.tm_year = 70;
        timeinfo.tm_mday = 1;
        std::cout << mktime(&timeinfo) << std::endl;

        time_t t = 0;
        struct tm *tm2 = localtime(&t);
        std::cout << mktime(tm2) << std::endl;

        std::cout
                << "19" << tm2->tm_year << '/' << tm2->tm_mon << '/' << tm2->tm_mday << '\t'
                << tm2->tm_hour << ':' << tm2->tm_min << ':' << tm2->tm_sec << '\t'
                << tm2->tm_zone << ':' << tm2->tm_gmtoff
                << std::endl;
        return 0;
}


gives:
1
2
3
-3600
0
1970/0/1        1:0:0   BST:3600


Notably, time zone BST and offset to UTC 3600. That I did not expect.

This topic is archived - New replies not allowed.