Odoo12:strTime无法识别格式%p(PM/AM)?

2024-06-16 08:31:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个模型函数中得到了以下代码:

data_ordine = order_dict['date'].replace(" UTC","").rstrip()        
_logger.info('Simone --> DATA ORDINE : %s',data_ordine + "|")        
if data_ordine: 
 data_finale = datetime.strptime(data_ordine,"%m/%d/%Y %I:%M%p")

当我尝试执行它时,我收到以下错误:

File "/usr/lib/python3.5/_strptime.py", line 346, in _strptime    data_string[found.end():])ValueError: unconverted data remains: PM

在日志中,我保存了传递给strptime函数的数据,我可以看到它是:

2020-01-21 11:12:36,221 24082 INFO db_test odoo.addons.tepp_ept.models.sale_order: Simone --> DATA ORDINE : 01/12/2020 10:42PM|

我已经在Python online compiler上尝试了该代码,似乎还可以:

from datetime import datetime 
str_date ="01/12/2020 10:42PM" 
date = datetime.strptime(str_date,"%m/%d/%Y %I:%M%p") 
print(date)

我哪里做错了


Tags: 函数代码模型datadatetimedateorderdict
1条回答
网友
1楼 · 发布于 2024-06-16 08:31:45

对于那些可能处于相同情况的,如@Adan-Cortez所示,它与区域设置(特别是LC_时间设置)相关。 我通过临时重置它,然后在strtime之后分配运算符值来解决此问题:

locale.setlocale(locale.LC_TIME, 'en_US.utf8')
if data_ordine:
   data_finale = datetime.strptime(data_ordine,"%m/%d/%Y %I:%M%p")

locale.setlocale(locale.LC_TIME, self.env.context['lang'] + '.utf8')

相关问题 更多 >