PyQt5 Date and Time 学习
2020-04-01 本文已影响0人
_Mirage
QDate, QTime, QDateTime
PyQt5 has QDate, QDateTime, QTime classes to work with date and time. The QDate is a class for working with a calendar date in the Gregorian calendar. It has methods for determining the date, comparing, or manipulating dates. The QTime class works with a clock time. It provides methods for comparing time, determining the time and various other time manipulating methods. The QDateTime is a class that combines both QDate and QTime objects into one object.
# coding='utf-8'
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt
class Tests:
def __init__(self):
pass
# Qdate,QTime,QDateTime测试
def test1(self):
# QDate是日期,currentDate返回当前日期(几月几日)
now = QDate.currentDate()
# print(now.toString())
# Qt.ISODate和Qt.DefaultLocaleLongDate是让日期用两种不同格式输出
print(now.toString(Qt.ISODate))
print(now.toString(Qt.DefaultLocaleLongDate))
# QdateTime是日期加上时间的组合(几月几日星期几,什么时间),
# currentDateTime返回当前的日期与时间
date_time = QDateTime.currentDateTime()
print(date_time.toString())
# Qtime是时间,currentTime返回当前时间(几点钟)
time = QTime.currentTime()
# print(time.toString())
# 将当前时间用不同的格式输出
print(time.toString(Qt.DefaultLocaleLongDate))
# 本地时间与国际时间
def test2(self):
date_time = QDateTime.currentDateTime()
print('本地时间:{}'.format(date_time.toString(Qt.ISODate)))
# toUTC funciton将给定时间转化成国际时间
print('国际时间:{}'.format(date_time.toUTC().toString(Qt.ISODate)))
# offsetFromTtc返回给定时间和国际时间的差值
print('两者偏差:{}秒.'.format(date_time.offsetFromUtc()))
# 计算本月本年天数
def test3(self):
date = QDate.currentDate()
print('本月有{}天'.format(date.daysInMonth()))
print('本年有{}天'.format(date.daysInYear()))
# 计算两个Date之间相差的天数
def test4(self):
d1 = QDate.currentDate()
d2 = QDate(2000, 7, 26)
print('从{}您出生到今天{},一共过去了{}天.'.
format(d2.toString(Qt.DefaultLocaleLongDate),
d1.toString(Qt.DefaultLocaleLongDate),
d2.daysTo(d1)))
print('您距离下一个生日还有{}天'.
format(d1.daysTo(QDate(2020, 7, 26))))
# DateTime加减天数,秒钟,月份,年份测试
def test5(self):
now = QDateTime.currentDateTime()
# now = QDateTime(2000, 2, 29, 2, 45, 30)
print('当前时间:{}'.format(now.toString(Qt.ISODate)))
print('加上{}天后:{}'.format(35, now.addDays(35).toString(Qt.ISODate)))
print('减去{}天后:{}'.format(15, now.addDays(-15).toString(Qt.ISODate)))
print('加上{}秒后:{}'.format(3600, now.addSecs(3600).toString(Qt.ISODate)))
print('加上{}月后:{}'.format(24, now.addMonths(24).toString(Qt.ISODate)))
print('加上{}年后:{}'.format(8, now.addYears(8).toString(Qt.ISODate)))
# Daylight saving time 测试(在夏季月份把时间提前,从而夜晚的白天时间更长,大概测试是不是夏天)
def test6(self):
now = QDateTime.currentDateTime()
# timeZoneAbbreviation 返回当前时间的时区
print('时区:{}'.format(now.timeZoneAbbreviation()))
# 可能是判断是不是在当前时区的夏天吧
if now.isDaylightTime():
print('当前时间在DST time里.')
else:
print('当前时间不在DST time里.')
# Unix epoch测试, Unix epoch大概是1970- 01-01T00:00:00Z ISO 8601
# 可以利用Unix epoch获得Unix time(从Unix epoch到现在过了多久的一个纯数字,秒钟)
def test7(self):
now = QDateTime.currentDateTime()
unix_time = now.toSecsSinceEpoch()
print('从Unix epoch到现在过去了{}秒钟'.format(unix_time))
# 从unix time转成datetime
d = QDateTime.fromSecsSinceEpoch(unix_time)
print('当前时间:', d.toString(Qt.ISODate))
# Julian day
# Julian day refers to a continuous count of \
# days since the beginning of the Julian Period
def test8(self):
now = QDate.currentDate()
print('当前日期的格林尼治时间:{}'.format(now.toString(Qt.ISODate)))
print('当前日期的Julian时间:{}'.format(now.toJulianDay()))
# Historical battles
# 计算时间跨度即使跨国几个世纪利用Julian day
def test9(self):
# 两个历史上的战役日期
borodino_battle = QDate(1812, 9, 7)
slavkov_battle = QDate(1805, 12, 2)
# 当前日期
now = QDate.currentDate()
j_today = now.toJulianDay()
j_borodino = borodino_battle.toJulianDay()
j_slavkov = slavkov_battle.toJulianDay()
# 计算第一个战争和当前时间在julian day下的跨度
d1 = j_today - j_borodino
# 计算美国奴隶战争和当前时间在julian day下的跨度
d2 = j_today - j_slavkov
# 输出差值
print('从Borodino战争到今天过去了:{}天'.format(d1))
print('从美国奴隶战争到今天过去了:{}天'.format(d2))
# Tests().test1()
# Tests().test2()
# Tests().test3()
# Tests().test4()
# Tests().test5()
# Tests().test6()
# Tests().test7()
# Tests().test8()
# Tests().test9()
运行结果:
image.png
image.png