DateFormatter引起的问题

本文探讨了一个酒店订票应用中日期显示错误的问题,通过解析DateFormatter的dateFormat属性,解释了yyyy-MM-dd与YYYY-MM-dd的区别,并提供了避免使用错误日期格式的建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

昨天老板让我帮忙解决个问题。一个没人在维护的某酒店订票应用的预定日期等很多有日期的地方都显示错误了。具体说就是2012年12月30日以及2012年12月31日显示成了2013年12月30日以及2013年12月31日。很明显就是日期转换成字符串的时候出错了嘛,但具体是什么原因以前还真没遇到过。后面Debug跟了下,发现DateFormatter的dateFormat是"YYYY-MM-dd",与我平时用的"yyyy-MM-dd"的不同在于年份,好吧问题发现了。但具体为啥不同呢?以前真没想过,demo让用这个就用这个了,所以就去查了下class reference,具体区别描述如下:

It uses yyyy to specify the year component. A common mistake is to use YYYYyyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

就是说yyyy指定的是“日历年”,YYYY指定的是“星期年”,具体什么是日历年星期年的没去再找是什么,但打开日历似乎就明白了。所以日后注意DateFormatter里面的年份,你不会想用YYYY的~~~

转载于:https://my.oschina.net/ansonyc/blog/99444

``` import psutil import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation import matplotlib import time from datetime import datetime matplotlib.use('Qt5Agg') # 设置 Qt 后端以提升性能 plt.rcParams["font.sans-serif"] = ["SimHei"] # 显示中文,解决图中无法显示中文的问题 plt.rcParams["axes.unicode_minus"] = False # 设置显示中文后,负号显示受影响。解决坐标轴上乱码问题 # 初始化数据存储 time_data = [] cpu_data, mem_data, disk_data, net_sent_data, net_recv_data = [], [], [], [], [] # 创建图表和子图 fig, axs = plt.subplots(2, 2, figsize=(12, 8)) fig.suptitle('系统资源监控', fontsize=16) #plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1, hspace=0.4, wspace=0.4)#如果你想要调整子图之间的间距 #axs[0,0] = fig.add_subplot(221) # CPU 使用率 #axs[0,1] = fig.add_subplot(222) # 内存使用率 #axs[1,0] = fig.add_subplot(223) # 硬盘使用率 #axs[1,1] = fig.add_subplot(224) # 网络流量 # 初始化折线图 def init(): axs[0,0].set_ylim(0, 100) axs[0,0].set_title('CPU 使用率 (%)') axs[0,1].set_ylim(0, 100) axs[0,1].set_title('内存使用率 (%)') #axs[1,0].set_ylim(0, 100) #axs[1,0].set_title('硬盘使用率 (%)') axs[1,0].set_ylim(0, 1000) axs[1,0].set_title('网络流量 (MB)') return axs[0,0],axs[0,1],axs[1,0]#axs[1,0] # 更新函数(每秒调用) def update(frame): # 获取当前时间戳 #time_data.append(len(time_data)) current_timestamp = time.time() #print("当前时间戳:", current_timestamp) # 将时间戳转换为datetime对象 current_datetime = datetime.fromtimestamp(current_timestamp) #print("当前时间(datetime):", current_datetime) # 格式化datetime对象为"年-月-日 时:分:秒"的格式 formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S") #print("格式化后的当前时间:", formatted_datetime) time_data.append(formatted_datetime) # 获取 CPU 使用率 cpu_usage = psutil.cpu_percent(interval=1) cpu_data.append(cpu_usage) time_data_current = time_data[-1] cpu_current = cpu_data[-1] # 获取内存使用率 mem_usage = psutil.virtual_memory().percent mem_data.append(mem_usage) time_data_current = time_data[-1] mem_current = mem_data[-1] # 获取硬盘使用率(默认取 C 盘) #disk_usage = psutil.disk_usage('/').percent #disk_data.append(disk_usage) # 获取网络流量(累计值转瞬时值) net_io = psutil.net_io_counters() net_sent = net_io.bytes_sent / 1024 / 1024 # 转换为 MB net_recv = net_io.bytes_recv / 1024 / 1024 net_sent_data.append(net_sent) net_recv_data.append(net_recv) time_data_current = time_data[-1] net_sent_data_current=net_sent_data[-1] net_recv_data_current=net_recv_data[-1] # 仅保留最近 600 秒数据 #max_length = 600 #for data in [time_data, cpu_data, mem_data, disk_data, net_sent_data, net_recv_data]: # if len(data) > max_length: # data.pop(0) # 更新折线图数据 axs[0,0].clear() axs[0,0].plot(formatted_datetime, cpu_current, 'r-') axs[0,0].set_ylim(0, 100) axs[0,0].set_title('CPU 使用率 (%)') axs[0,1].clear() axs[0,1].plot(formatted_datetime, mem_current, 'b-') axs[0,1].set_ylim(0, 100) axs[0,1].set_title('内存使用率 (%)') #axs[1,0].clear() #axs[1,0].plot(time_data, disk_data, 'g-') #axs[1,0].set_ylim(0, 100) #axs[1,0].set_title('硬盘使用率 (%)') axs[1,0].clear() axs[1,0].plot(formatted_datetime, net_sent_data_current, 'm-', label='发送') axs[1,0].plot(formatted_datetime, net_recv_data_current, 'c-', label='接收') axs[1,0].legend() axs[1,0].set_title('网络流量 (MB)') #return axs[0,0], axs[0,1], axs[1,0], #axs[1,1] return cpu_usage,mem_usage ,(net_sent, net_recv) # 启动动画 ani = FuncAnimation(fig, update, init_func=init, blit=True, interval=1000) plt.tight_layout() plt.show()```这段代码运行不了,有什么问题,如何优化
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值