在PyQt应用程序中更改本地日期

时间:2014-03-03 17:47:37

标签: python pyqt pyqt4

如何在PyQt应用程序中修改本地日期(我需要在linux和win平台上更改它)? E.g:

>>> date_now = QtCore.QDate.currentDate()
>>> print date_now
>>> PyQt4.QtCore.QDate(2014, 3, 3)

...some code for change current date

>>> date_now = QtCore.QDate.currentDate()
>>> print date_now
>>> PyQt4.QtCore.QDate(2012, 1, 1)

注意:外部系统日期不得更改。

1 个答案:

答案 0 :(得分:2)

你可以修补QDate

QtCore.QDate.currentDate = lambda: QtCore.QDate(2012, 1, 1)

这可能导致其他地方出现不一致。


工作示例:

Python 3.3.2+ (default, Oct  9 2013, 14:50:09) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt4 import QtCore
>>> QtCore.QDate.currentDate()
PyQt4.QtCore.QDate(2014, 3, 3)
>>> QtCore.QDate.currentDate = lambda: QtCore.QDate(2012, 1, 1)
>>> QtCore.QDate.currentDate()
PyQt4.QtCore.QDate(2012, 1, 1)
>>> 

对于python2.7:

class PatchedQDate (QtCore.QDate):
    @classmethod
    def currentDate (cls):
        return QtCore.QDate (2012, 1, 1)

QtCore.QDate = PatchedQDate