PDA

View Full Version : Get QDate from microseconds



iddqd
20th July 2010, 07:12
I have date which is stored in microseconds as int64.
How to set it to QDate/QDateTime object?

SixDegrees
20th July 2010, 08:45
Have a look at ctime, and other C system functions.

iddqd
20th July 2010, 08:56
Not very clear how these functions relate with an int64 value. Could you show simple example?

ChrisW67
20th July 2010, 09:06
Assuming your microsecond time is based on the UNIX epoch then something like?


int64 microsecs = some big value;
uint secs = microsecs / 1000000;
QDateTime result;
result.setTime_t(secs);
(I haven't tested this).

SixDegrees
20th July 2010, 09:15
time_t is an integer, typically 64 bits but occasionally 32 bits. Either way, the functions allow you to start with an integer representation of the number of seconds since the beginning of the Unix epoch and convert it into its constituent parts, found in tm_struct.

Or, you could use ChrisW67's suggestion, although this begs the question: what are your microseconds relative to? The Unix epoch? The current day? The middle of February?

iddqd
20th July 2010, 09:21
void setTime_t(uint secsSince1Jan1970UTC);
But what if I have some dates before 1.Jan.1970 and some dates after 1970?

SixDegrees
20th July 2010, 09:31
void setTime_t(uint secsSince1Jan1970UTC);
But what if I have some dates before 1.Jan.1970 and some dates after 1970?

Then your results will be system dependent. Assuming you're using the Unix epoch, dates prior to that point will be negative integers. Some systems accept these, other don't.

If the system routines won't work, you can do the conversion yourself by using Julian dates. There are lots of references around that cover this.

iddqd
20th July 2010, 09:51
Ok, thanks. I'll try to find a solution.