473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem about time_t, localtime in Vc2005, possible bug ?

Hi There,

I just found that there is a problem in vc2005 regarding to time_t and
localtime. See code snippets belows. Using this code segment, I found that
when ut is 86200, the corresponding output is 1970.1.1 7:56:40, when ut is 0,
the output is 1970.1.1 8:00:00, when ut is 86400, the output is 1970.1.2
8:00:00. While output will be 1970 1.2 7:56:40 in VC8. Please note that I
am aware that time_t is 64bit in VC2005 and using _USE_32BIT_TIME_T can force
time_t behave like 32bit, but don't plan to use this workaround.

time_t ut;
ut = (time_t) 86200;
struct tm tm = *localtime(&ut);
cout << tm.tm_year << endl;
cout << tm.tm_mon << endl;
cout << tm.tm_mday << endl;
cout << tm.tm_hour << endl;
cout << tm.tm_min << endl;
cout << tm.tm_sec << endl;

Any comments/thoughts/suggestions is highly appreciated.

Thanks,
-Yuanfei
May 24 '06 #1
7 4239
On Wed, 24 May 2006 02:10:02 -0700, Yuanfei
<Yu*****@discussions.microsoft.com> wrote:
Hi There,

I just found that there is a problem in vc2005 regarding to time_t and
localtime. See code snippets belows. Using this code segment, I found that
when ut is 86200, the corresponding output is 1970.1.1 7:56:40, when ut is 0,
the output is 1970.1.1 8:00:00, when ut is 86400, the output is 1970.1.2
8:00:00. While output will be 1970 1.2 7:56:40 in VC8. Please note that I
am aware that time_t is 64bit in VC2005 and using _USE_32BIT_TIME_T can force
time_t behave like 32bit, but don't plan to use this workaround.

time_t ut;
ut = (time_t) 86200;
struct tm tm = *localtime(&ut);
cout << tm.tm_year << endl;
cout << tm.tm_mon << endl;
cout << tm.tm_mday << endl;
cout << tm.tm_hour << endl;
cout << tm.tm_min << endl;
cout << tm.tm_sec << endl;

Any comments/thoughts/suggestions is highly appreciated.


What time zone are you in? Also, go ahead and finish writing this up as a
console program to make it easier for others to try, and talk about its
actual output. Finally, it's not clear what the problem is; you didn't say
which compiler gave you the good(?) results, and it's not clear what input
applies to the sentence fragment "While output will be 1970 1.2 7:56:40 in
VC8".

--
Doug Harrison
Visual C++ MVP
May 24 '06 #2
Thanks Doug for reminding me.

1) My time zone is GMT+8:00

2) The OS of my bos is XP Professional, SP2

3) The repro code is belows:
#include <iostream>
#include <time.h>
void main()
{
using namespace std;
time_t ut;
ut = 86200;
struct tm tm = *localtime(&ut);
cout << "The time is" << tm.tm_year+1900 <<" " << tm.tm_mon+1 <<" " <<
tm.tm_mday << " ";
cout << tm.tm_hour << ":" << tm.tm_min << ":" << tm.tm_sec << endl;

ut = 86400;
tm = *localtime(&ut);
cout << "The time is" << tm.tm_year+1900 <<" " << tm.tm_mon+1 <<" " <<
tm.tm_mday << " ";
cout << tm.tm_hour << ":" << tm.tm_min << ":" << tm.tm_sec << endl;
}
4) And the result of the program above is:
On VC8:
The time is: 1970 1 1 7:56:40
The time is: 1970 1 2 8:0:0

On VC6:
The time is: 1970 1 2 7:56:40
The time is: 1970 1 2 8:0:0

5) Of course, adding _USE_32BIT_TIME_T to VC8 project will make the result
of the program in VC8 being same as that in VC6.

6) Note that when ut is 86200, the value of tm.tm_mday should be 2 instead
of 1. Because 86400 's corresponding tm.tm_mday's value is 1.

7) My question is:

* If the behavior of the code above in VC8 is expected and corrected ?
* If yes, I wonder if there are any patch availalbe for this issue ?
* If it's not bug, how can I change my code to get the expected value "The
time is: 1970 1 2 7:56:40" when ut is 86200 ?
Thanks,
-Yuanfei

"Doug Harrison [MVP]" wrote:
On Wed, 24 May 2006 02:10:02 -0700, Yuanfei
<Yu*****@discussions.microsoft.com> wrote:
Hi There,

I just found that there is a problem in vc2005 regarding to time_t and
localtime. See code snippets belows. Using this code segment, I found that
when ut is 86200, the corresponding output is 1970.1.1 7:56:40, when ut is 0,
the output is 1970.1.1 8:00:00, when ut is 86400, the output is 1970.1.2
8:00:00. While output will be 1970 1.2 7:56:40 in VC8. Please note that I
am aware that time_t is 64bit in VC2005 and using _USE_32BIT_TIME_T can force
time_t behave like 32bit, but don't plan to use this workaround.

time_t ut;
ut = (time_t) 86200;
struct tm tm = *localtime(&ut);
cout << tm.tm_year << endl;
cout << tm.tm_mon << endl;
cout << tm.tm_mday << endl;
cout << tm.tm_hour << endl;
cout << tm.tm_min << endl;
cout << tm.tm_sec << endl;

Any comments/thoughts/suggestions is highly appreciated.


What time zone are you in? Also, go ahead and finish writing this up as a
console program to make it easier for others to try, and talk about its
actual output. Finally, it's not clear what the problem is; you didn't say
which compiler gave you the good(?) results, and it's not clear what input
applies to the sentence fragment "While output will be 1970 1.2 7:56:40 in
VC8".

--
Doug Harrison
Visual C++ MVP

May 25 '06 #3
On Wed, 24 May 2006 19:41:01 -0700, Yuanfei
<Yu*****@discussions.microsoft.com> wrote:
Thanks Doug for reminding me.

1) My time zone is GMT+8:00

2) The OS of my bos is XP Professional, SP2

3) The repro code is belows:
#include <iostream>
#include <time.h>
void main()
{
using namespace std;
time_t ut;
ut = 86200;
struct tm tm = *localtime(&ut);
cout << "The time is" << tm.tm_year+1900 <<" " << tm.tm_mon+1 <<" " <<
tm.tm_mday << " ";
cout << tm.tm_hour << ":" << tm.tm_min << ":" << tm.tm_sec << endl;

ut = 86400;
tm = *localtime(&ut);
cout << "The time is" << tm.tm_year+1900 <<" " << tm.tm_mon+1 <<" " <<
tm.tm_mday << " ";
cout << tm.tm_hour << ":" << tm.tm_min << ":" << tm.tm_sec << endl;
}
4) And the result of the program above is:
On VC8:
The time is: 1970 1 1 7:56:40
The time is: 1970 1 2 8:0:0

On VC6:
The time is: 1970 1 2 7:56:40
The time is: 1970 1 2 8:0:0

5) Of course, adding _USE_32BIT_TIME_T to VC8 project will make the result
of the program in VC8 being same as that in VC6.

6) Note that when ut is 86200, the value of tm.tm_mday should be 2 instead
of 1. Because 86400 's corresponding tm.tm_mday's value is 1.
You mean 2, right?
7) My question is:

* If the behavior of the code above in VC8 is expected and corrected ?
* If yes, I wonder if there are any patch availalbe for this issue ?
* If it's not bug, how can I change my code to get the expected value "The
time is: 1970 1 2 7:56:40" when ut is 86200 ?


Yeah, I'd expect the day of the month to change if the times were 23:56:40
and 0:00:00. I can repro this by adding the following to your code:

_putenv("TZ=XXX-8");
_tzset();

MS now has an excellent bug reporting facility:

http://lab.msdn.microsoft.com/produc...k/Default.aspx

I suggest you enter a bug report there and post the link you receive so
others can follow it.

--
Doug Harrison
Visual C++ MVP
May 25 '06 #4
Hi Doug,

Thanks for your suggestion.

I have filed a bug, and its link is
http://lab.msdn.microsoft.com/Produc...e-f0e798a99f5a.

Cheers,
-Yuanfei

"Doug Harrison [MVP]" wrote:
On Wed, 24 May 2006 19:41:01 -0700, Yuanfei
<Yu*****@discussions.microsoft.com> wrote:
Thanks Doug for reminding me.

1) My time zone is GMT+8:00

2) The OS of my bos is XP Professional, SP2

3) The repro code is belows:
#include <iostream>
#include <time.h>
void main()
{
using namespace std;
time_t ut;
ut = 86200;
struct tm tm = *localtime(&ut);
cout << "The time is" << tm.tm_year+1900 <<" " << tm.tm_mon+1 <<" " <<
tm.tm_mday << " ";
cout << tm.tm_hour << ":" << tm.tm_min << ":" << tm.tm_sec << endl;

ut = 86400;
tm = *localtime(&ut);
cout << "The time is" << tm.tm_year+1900 <<" " << tm.tm_mon+1 <<" " <<
tm.tm_mday << " ";
cout << tm.tm_hour << ":" << tm.tm_min << ":" << tm.tm_sec << endl;
}
4) And the result of the program above is:
On VC8:
The time is: 1970 1 1 7:56:40
The time is: 1970 1 2 8:0:0

On VC6:
The time is: 1970 1 2 7:56:40
The time is: 1970 1 2 8:0:0

5) Of course, adding _USE_32BIT_TIME_T to VC8 project will make the result
of the program in VC8 being same as that in VC6.

6) Note that when ut is 86200, the value of tm.tm_mday should be 2 instead
of 1. Because 86400 's corresponding tm.tm_mday's value is 1.


You mean 2, right?
7) My question is:

* If the behavior of the code above in VC8 is expected and corrected ?
* If yes, I wonder if there are any patch availalbe for this issue ?
* If it's not bug, how can I change my code to get the expected value "The
time is: 1970 1 2 7:56:40" when ut is 86200 ?


Yeah, I'd expect the day of the month to change if the times were 23:56:40
and 0:00:00. I can repro this by adding the following to your code:

_putenv("TZ=XXX-8");
_tzset();

MS now has an excellent bug reporting facility:

http://lab.msdn.microsoft.com/produc...k/Default.aspx

I suggest you enter a bug report there and post the link you receive so
others can follow it.

--
Doug Harrison
Visual C++ MVP

May 25 '06 #5

Yuanfei wrote:
Hi Doug,

Thanks for your suggestion.

I have filed a bug, and its link is
http://lab.msdn.microsoft.com/Produc...e-f0e798a99f5a.

Cheers,
-Yuanfei

"Doug Harrison [MVP]" wrote:
On Wed, 24 May 2006 19:41:01 -0700, Yuanfei
<Yu*****@discussions.microsoft.com> wrote:
Thanks Doug for reminding me.

1) My time zone is GMT+8:00

2) The OS of my bos is XP Professional, SP2

3) The repro code is belows:
#include <iostream>
#include <time.h>
void main()
{
using namespace std;
time_t ut;
ut = 86200;
struct tm tm = *localtime(&ut);
cout << "The time is" << tm.tm_year+1900 <<" " << tm.tm_mon+1 <<" " <<
tm.tm_mday << " ";
cout << tm.tm_hour << ":" << tm.tm_min << ":" << tm.tm_sec << endl;

ut = 86400;
tm = *localtime(&ut);
cout << "The time is" << tm.tm_year+1900 <<" " << tm.tm_mon+1 <<" " <<
tm.tm_mday << " ";
cout << tm.tm_hour << ":" << tm.tm_min << ":" << tm.tm_sec << endl;
}
4) And the result of the program above is:
On VC8:
The time is: 1970 1 1 7:56:40
The time is: 1970 1 2 8:0:0

On VC6:
The time is: 1970 1 2 7:56:40
The time is: 1970 1 2 8:0:0

5) Of course, adding _USE_32BIT_TIME_T to VC8 project will make the result
of the program in VC8 being same as that in VC6.

6) Note that when ut is 86200, the value of tm.tm_mday should be 2 instead
of 1. Because 86400 's corresponding tm.tm_mday's value is 1.


You mean 2, right?
7) My question is:

* If the behavior of the code above in VC8 is expected and corrected ?
* If yes, I wonder if there are any patch availalbe for this issue ?
* If it's not bug, how can I change my code to get the expected value "The
time is: 1970 1 2 7:56:40" when ut is 86200 ?


Yeah, I'd expect the day of the month to change if the times were 23:56:40
and 0:00:00. I can repro this by adding the following to your code:

_putenv("TZ=XXX-8");
_tzset();

MS now has an excellent bug reporting facility:

http://lab.msdn.microsoft.com/produc...k/Default.aspx

I suggest you enter a bug report there and post the link you receive so
others can follow it.

--
Doug Harrison
Visual C++ MVP


I too have reproduced this problem. It seems to occur when the local
time is within the local to GMT time difference, but only for the
64-bit localtime() function, and only for the first 4 days of 1970. I
wrote a quick program to check 60-second intervals thereafter with no
problems found (unless at some point the 32 and 64 bit localtimes()s
have the same bug!)

Chris

May 25 '06 #6
On 25 May 2006 04:35:59 -0700, ch*********@postmaster.co.uk wrote:
I too have reproduced this problem. It seems to occur when the local
time is within the local to GMT time difference, but only for the
64-bit localtime() function, and only for the first 4 days of 1970. I
wrote a quick program to check 60-second intervals thereafter with no
problems found (unless at some point the 32 and 64 bit localtimes()s
have the same bug!)


Apparently this bug was reported before and has since been fixed, but there
is no further info given:

http://lab.msdn.microsoft.com/produc...e-f0e798a99f5a

--
Doug Harrison
Visual C++ MVP
May 26 '06 #7
I called M$ at a number provided by online agent, and get the final answer
that we are on our own on this bug, all the thing I can do on this is to wait
and search internet to see if there is new hotfix or patch. Sigh...

-Yuanfei

"Doug Harrison [MVP]" wrote:
On 25 May 2006 04:35:59 -0700, ch*********@postmaster.co.uk wrote:
I too have reproduced this problem. It seems to occur when the local
time is within the local to GMT time difference, but only for the
64-bit localtime() function, and only for the first 4 days of 1970. I
wrote a quick program to check 60-second intervals thereafter with no
problems found (unless at some point the 32 and 64 bit localtimes()s
have the same bug!)


Apparently this bug was reported before and has since been fixed, but there
is no further info given:

http://lab.msdn.microsoft.com/produc...e-f0e798a99f5a

--
Doug Harrison
Visual C++ MVP

May 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anand CS | last post by:
Hi All I have question regarding time data structures... I have 64 bit unsigned microsecond resolution (unsigned __int64 for windows/and unsigned long long for others) variable. It stores the...
1
by: mimmo | last post by:
I have a problem while calculating timezone (from Europe/Rome to Brazil/East). Can someone help me ?? Thank you, Mimmo. On this site http://www.timezoneconverter.com/cgi-bin/tzc.tzc I have...
1
by: KW | last post by:
Hi all, Appreciate if someone can help me out on this. Currently, I have a tm structure holding information of the UTC time, which is very likely to be in the past, meaning not the current...
8
by: Ian Pilcher | last post by:
When adding two values of type time_t, how can I check for overflow? Maybe I'm just brain-cramped today, but I can't figure out how to do it. Thanks! --...
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
8
by: Henrik Goldman | last post by:
I have strings like "2006-03-26 21.51" which I would like to convert into time_t. I know that the string is in localtime. So far I've written the following code: time_t...
7
by: Angus Comber | last post by:
Hello I need to do this conversion but can't seem to find the function. Angus
7
by: Nick Keighley | last post by:
Hi, this is probably quite easy... How do I convert a UTC string into a time_t? eg. "2007-12-18 13:37:26" - a time_t. Now FAQ 13.3 says use mktime() to convert a struct tm into a time_t...
8
by: Abubakar | last post by:
Hi, Lets say I have three integers (date, month, and year) as part of a struct lets say like struct mydate{ int day, month, year}; I want to add x number of days to this date. And it should be a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.