문제

I take date from QDateTimeEdit and convert it to seconds like this:

import time
from datetime import datetime

date = self.__ui.dateTimeEdit.date().toString("dd/MM/yy")
dateString = str(date)

seconds = time.mktime(datetime.strptime(dateString, "%d/%m/%y").timetuple()) 

This works well, but since it looks to long to me, my question is: Is it possible to convert self.__ui.dateTimeEdit.date() directly, without those string conversions?

EDIT1 Unfortunately toMSecsSinceEpoch() as falsetru suggested, doesn't work for me.

AttributeError: 'QDateTime' object has no attribute 'toMSecsSinceEpoch'

I'm using PyQt 4.7.1 for Python 2.6

EDIT2 based on jonrsharpe's answer I've escaped string conversions:

    date = self.__ui.dateTimeEdit.date().toPyDate()
    seconds = time.mktime(date.timetuple()) 

result is the same.

EDIT3 even shorter solution based on falsetru's comment:

self.__ui.dateTimeEdit.dateTime().toTime_t()
도움이 되었습니까?

해결책

Use QDateTime.toMSecsSinceEpoch:

>>> import PyQt4.QtCore
>>> d = PyQt4.QtCore.QDateTime(2014, 2, 20, 17, 10, 30)
>>> d.toMSecsSinceEpoch() / 1000
1392883830L

UPDATE

Alternative using QDateTime.toTime_t:

>>> d = PyQt4.QtCore.QDateTime(2014, 2, 20, 17, 10, 30)
>>> d.toTime_t()
1392883830L

다른 팁

The QDate you get from

self.__ui.dateTimeEdit.date()

has another method toPyDate that will save you the round trip through a string.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top