1

From the wxWidgets online docs, it seems that there is no direct way to create a wxDateTime directly from a wxLongLong value as returned e.g. from wxGetUTCTimeMillisor wxGetLocalTimeMillis. Hence I wonder

  1. if there is some obscure reason for this omission and
  2. if the code below is the formally correct way to do it (or relies too much on assumptions about the underlying types or misses some obscure time zone or leap second considerations or ...).

OK, I do have a suspicion about 1.: We also have wxGetUTCTimeUSec() and so the "naked" wxLongLong does not tell it it is measured in milli- or microseconds. But still ...

#include <wx/time.h> 
wxLongLong myMillis = wxGetUTCTimeMillis()
...
#include <wx/datetime.h>
wxDateTime myDateTime;
myDateTime.Set( (time_t)((myMillis/1000).ToLong()) );
myDateTime.SetMilliSecond( (unsigned short)((myMillis % 1000).ToLong()) );
Hagen von Eitzen
  • 2,109
  • 21
  • 25

2 Answers2

2

Actually you can. It is wxDateTime (time_t timet), described as seconds since the Epoch 00:00:00 UTC, Jan 1, 1970.

The proof that this works (wxW 3.0.0):

wxDateTime wxDateTime::UNow()
{
    return wxDateTime(wxGetUTCTimeMillis());
}
catalin
  • 1,927
  • 20
  • 20
0

If it's just the current time you are interested in, there's also wxDateTime::UNow(). But in general, I would agree with your assessment. One would think a wxLongLong would be allowed from one of the constructors.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Actually it is allowed, but this ctor is not documented as it is rather dangerous and it's easy to create wrong `wxDateTime` objects if you are not careful with what you give it. Using `UNow()` is a much better idea. – VZ. Jun 15 '14 at 21:08