Page 1 of 1

wxDateTime::Now() off by 10 hours

Posted: Tue Apr 11, 2006 4:18 pm
by chris
Hi all,

I'm experiencing a strange problem with wxDateTime::Now(). I'm logging some stuff to a wxTextCtrl via

Code: Select all

textctrl->AppendText(wxDateTime::Now().Format(_T("%x %X: ")+somestring);
Now to the problem: The first call to wxDateTime::Now().Format(...) is off by -10 hours; all subsequent calls display the correct time. Example:
04/11/06 08:16:03: foo
04/11/06 18:16:03: foo
04/11/06 18:16:03: foo
If I call wxDateTime::Now() once before the first wxDateTime::Now().Format(...) call it works as it should.

I'm in GMT+1 with DST, btw.

Any ideas?

Chris

Posted: Wed Apr 12, 2006 10:12 pm
by protocol
Is your locale setup correctly on your machine?

Posted: Thu Apr 13, 2006 8:13 am
by chris
Hi protocol,

Yes, it is.
I should have mentioned this happens under wxMSW 2.6.3 (and 2.6.2), but works fine under wxGTK 2.6.3.
The strange thing is that it's off just for the very first call and then works as expected. With a badly set up locale I'd assume a permanent shift.

Chris

Posted: Thu Apr 13, 2006 9:02 am
by Phoenix_pl
I had the same problem. This should help you:

Code: Select all

wxDateTime now = wxDateTime::Now();
textctrl->AppendText(now.Format(_T("%x %X: ")+somestring);
It worked for me.

Posted: Thu Apr 13, 2006 9:10 am
by chris
Hi Phoenix_pl,

It's good to know I'm not the only one having this problem. Are you using wxMSW, too?
I guess it's time to post a bug to the tracker...

Chris

edit: I'm sorry, but I fail to see why that should lead to a different behaviour. It's sematically identical to the call in my first post.

Posted: Thu Apr 13, 2006 9:29 am
by Phoenix_pl
Yes, I'm using wxMSW.

And yes, it helps and I'm not sure why. Just try it. Also I have included some screenshots.

Image

Image

Posted: Thu Apr 13, 2006 10:12 am
by chris
Whoa...you are right.

I don't understand this at all.

What's the big difference between

Code: Select all

wxDateTime now = wxDateTime::Now();
textctrl->AppendText(now.Format(_T("%x %X: ")+somestring);
and

Code: Select all

textctrl->AppendText(wxDateTime::Now().Format(_T("%x %X: ")+somestring);
except that one uses an anonymus variable and the other a named variable?

At first I thought this could be some wierd compiler/runtime library issue, but I see this both on Visual C++ 7.1 and MinGW 3.4.2.

Now() is an inlined method, could that have to do something with this? Though compiling with "-fno-inline" resp. "/Ob0" didn't make a change.

This is really strange. I'd like to understand this; any ideas?

Chris

Posted: Thu Apr 13, 2006 7:14 pm
by eco
I tried tracing the differences between the two. I didn't see anything glaringly wrong but the path is slightly complicated to trace. wxDateTime uses the default copy constructor so I doubt the issue is there. The first main difference I would think would be that the first one creates a wxDateTime instance then Now() is called versus the latter which calls Now then creates the instance. I don't immediately see why this would be a problem. I looked into Format() and my first thought was it had something to do with GetTimeZone() in datetime.cpp but it doesn't appear to care whether a wxDateTime instances has been created, it has its own static member variable it uses to determine whether the time zone has been established yet so the ordering of Now() and construction doesn't appear to matter (if it is, indeed a problem with GetTimeZone()).

I'll ping Vadim on the mailing list (he wrote these classes) and see what he thinks.

Posted: Thu Apr 13, 2006 9:32 pm
by chris
Hi eco,

I've been thinking of a copy ctor issue, too, but

Code: Select all

wxDateTime *now=&wxDateTime::Now();
textctrl->AppendText(now->Format(_T("%x %X: ")+somestring);
works as intended without calling a copy ctor, so it can't be related to that.

And thanks for looking into this and posting to wx-dev. I really want to know what's going on here.

Chris

Posted: Thu Apr 13, 2006 9:38 pm
by eco
Vadim's reply:
I've just tried it here (VC7.1, HEAD) and it works as expected. I'm not in
UTC-9 of course, but I'm not in UTC neither... I have really no idea why it
doesn't work for two of you.
I suspect you may have to break out the debugger if you want to get to the bottom of the issue. I'm a bit overloaded here or else I'd give it an attempt.

Posted: Fri Apr 14, 2006 3:18 am
by Jamie
Can you please download CVS HEAD and try again as I have committed a fix for this.

Thanks

Posted: Fri Apr 14, 2006 2:52 pm
by chris
Hi Jamie,

Yes, this is fixed for me in HEAD by your changes in datetime.cpp.

Many thanks for your time!

Chris

Posted: Sat Apr 15, 2006 11:29 am
by Peterj
Jamie,

While you were fixing this, did you also check out Today(). It also appears to be broken in 2.6.3 (alpha).

Code: Select all

wxDateTime defdate,tday;
tday = defdate.Today();
today = tday.FormatDate();// produces a date of 03/15/2006

today = tday.Format("%d/%m/%Y"); // produces correct date 15/04/2006
Regards,
Peter

Posted: Mon Apr 17, 2006 12:53 am
by Jamie
Try using CVS HEAD as this should also be fixed.