473,804 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4262
On Wed, 24 May 2006 02:10:02 -0700, Yuanfei
<Yu*****@discus sions.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*****@discus sions.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*****@discus sions.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*****@discus sions.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*****@discus sions.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*********@pos tmaster.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*********@pos tmaster.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
25729
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 microsoconds elapsed since the Epoch... I want to convert this time into a string form.... AFAIK normally the procedure from a time_t structure to string format is something like...convert time_ to struct tm using localtime method......
1
5239
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 for this date 2003/01/01 00:00:00:
1
24531
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 time. So, let's say I'm in CST(-6) now, and I want to know the equivalent local time of that tm structure, how do I do that taking into consideration daylight savings adjustments?
8
4978
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! -- ======================================================================== Ian Pilcher i.pilcher@comcast.net ========================================================================
17
2213
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
12344
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 LocalTimeFromString(string str) { struct tm t; memset(&t, 0, sizeof(t));
7
37821
by: Angus Comber | last post by:
Hello I need to do this conversion but can't seem to find the function. Angus
7
6033
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 or you have to use some non-standard thing to parse the
8
4187
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 real date calculation thing. Like if the date is 31st december 2008, when i add 1 to this, it should become 1st feb 2009. This of course may require converting the above struct values to some data type that crt's date functons can understand. I...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.