I want to use PyQt6 with QDateTime. Here's the problem. The first entry is ok. But turning the TEST switch to 1 or 2 will cause various errors. In particular, dealing with the time zones. I know that the QTimeZone has other time zones, but how do I convert them? I want the original string "20230329 15:40:00 US/Eastern" to be the same as the generated, a solution should using QDateTime and QTimeZone.
import re
from PyQt6.QtCore import QDateTime, Qt, QTimeZone, QByteArray
PATT_TIME_BASE = re.compile(r"\d{8} \d{2}:\d{2}:\d{2} \w+(/\w+)?")
for date_str in ["Wed Mar 26 22:37:40 2019 GMT-08","20230329 15:40:00 US/Eastern"]:
if PATT_TIME_BASE.match(date_str):
s = date_str.rsplit(" ", 1)
date_object = QDateTime.fromString(s[0],"yyyyMMdd HH:mm:ss")
# ---------
print("List:",s)
TEST = 0
if TEST == 1: # Error (1) : This doesn't work
qzt = QTimeZone(QByteArray(s[1])) # -> Not Ok
date_object.setTimeZone(qzt)
date_str_2 = date_object.toString("yyyyMMdd HH:mm:ss Z")
if TEST == 2: # Error (2): This doesn't work
date_object.setTimeZone(s[1])
date_str_2 = date_object.toString("yyyyMMdd HH:mm:ss Z")
else:
date_str_2 = date_object.toString("yyyyMMdd HH:mm:ss")
#
print("(2) ",date_str, " - ", date_object, " - ", date_str_2)
else:
date_object = QDateTime.fromString(date_str)
date_str_2 = date_object.toString()
#
print("(1) ",date_str, " - ", date_object, " - ", date_str_2)
What can I do?
QByteArray does not accept simple strings as argument, a
bytesobject must be provided.Also, the
Zexpression in the QDateTime format is not used for the time zone, but to show whether the time is UTC (Zulu time). If you want to display the time zone, you need to get itsid()and append it to the string.