mktime issue
Nov 18, 2009 at 2:22pm UTC
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:
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
Nov 18, 2009 at 2:32pm UTC
I get zero. It could be a DST issue.
Nov 18, 2009 at 2:41pm UTC
I've tried setting tm_isdst to -1,0 and 1 which doesn't make any difference.
Nov 18, 2009 at 2:58pm UTC
What platform and timezone are you?
I've used gcc under cygwin in GMT.
Last edited on Nov 18, 2009 at 3:01pm UTC
Nov 18, 2009 at 3:14pm UTC
Using gcc on a linux pc in GMT.
Nov 18, 2009 at 10:29pm UTC
-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.
Topic archived. No new replies allowed.