PDA

View Full Version : QDateTimeEdit as a time in unix?



kja
18th November 2010, 18:47
Hi,

I want to have a user enter a QDateTimeEdit time and then take that info and convert it to unix time that I can store in a double so that I can load up some data. Any Idea how this can be done?

I was thinking I could get the info then convert it to a char * and then maybe convert it to unix? But that is probably totally wrong:


QDateTimeEdit * startTime;
startTime = new QDateTimeEdit;

//slot activated when user clicks a pushbutton)
void myWidget::getStartTime
{
QString stEntered =startTime->text();
char * Start = qstrdup( stEntered.toLatin1() );

/// then convert to unix...maybe
}


Is there a different/better way to convert some QDateTimeEdit to a double that will store the time in seconds since Epoch?

Thanks for the help.

kja
20th November 2010, 03:01
OK so I am getting closer but I think I am making it harder than it is.

I'm using secsTo() to get the difference between the beginning of unix time Utc and my start and end times. This works ok, but secsTo automatically converts my start and end times to utc which messes up my code because they are already in utc time (which I want). To get around this I add the difference between my time and Utc time to epoch.setTime. There has got to be a better way....






void MyWidget::getDates(){

double diff = 60*60*7; // sec*min*hours - offset for local time

QDateTime stEntered = startTime->dateTime();
QDateTime epoch;
epoch.setTime_t(diff);
time_t st = epoch.secsTo(stEntered);//secsTo converts both epoch and stEntered
//to utc time then finds the diff between them, so problem is that my stEntered is
//already in utc (result ends up being 7 hrs ahead of utc)

stT = double(st);

QDateTime endEntered = EnterEnd->dateTime();
time_t et = epoch.secsTo(endEntered);
endT = double(et);

}

thanks for the help

SixDegrees
20th November 2010, 19:16
Is there something wrong with QDateTime::toTime_t()?

kja
22nd November 2010, 15:21
When I use QDateTime::toTime_t() it seems to take my data and convert it to utc time again, which gives me a wrong value because the time was originally in utc

this is how i used it:



.........
EnterSt = new QDateTimeEdit( QDateTime::currentDateTimeUtc().addDays(-10));
EnterEnd = new QDateTimeEdit( QDateTime::currentDateTimeUtc(), this);
.........

void MyWidget::getDates(){

QDateTime stEntered =EnterSt->dateTime();
time_t st = stEntered.toTime_t();
stT = double(st);

QDateTime endEntered = EnterEnd->dateTime();
time_t et = endEntered.toTime_t();
endT = double(et);


stT and entT are now 7 hours later than EnterSt and EnterEnd (7hrs is my difference from UTC time)

????

wysota
22nd November 2010, 15:32
Check out QDateTime::toTimeSpec() and QDateTime::toLocalTime()

kja
22nd November 2010, 19:52
I think toTimeSpec() and toLocalTime() will convert my info. All I want to do it turn a QDateTime into a time_T with no changes made to the data. It starts out in UTC and I want it to stay in UTC just as a time_t (actually I want it to be a double but I can easily convert the time_t to a double)

this is what I am doing now:


time_t rawtime2;
time(&rawtime2);
double diff = (double)(mktime(gmtime(&rawtime2)) - mktime(localtime((&rawtime2))));//offset for local time

QDateTime stEntered = EnterSt->dateTime();
QDateTime endEntered = EnterEnd->dateTime();
time_t st = stEntered.toTime_t()-diff;
time_t et = endEntered.toTime_t()-diff;
stT = double(st);
endT = double(et);

wysota
22nd November 2010, 20:07
And how should Qt know it is in UTC if you don't tell it that? Tell Qt the time is in UTC and all will be fine.

Why don't you use Qt's methods for querying time? You wouldn't have all those problems at all.