获取“2:35 pm”;而不是“02:35 PM”从Python日期/时间? [英] Get "2:35pm" instead of "02:35PM" from Python date/time?

查看:146
本文介绍了获取“2:35 pm”;而不是“02:35 PM”从Python日期/时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Python还是有点慢,所以我没有把它弄清楚文档中的显着内容等等。



我已经工作与Django有一点,他们通过模板标签添加了一些datetime格式化选项,但是在常规python代码中,如何获得没有前导零的12小时小时



有没有直接的方法来做到这一点?我正在查看strftime()的2.5和2.6文档,对于这种情况,似乎没有格式化选项。



我应该使用别的东西吗?



随时可以添加文档中不明显的任何其他时间格式化提示。 =)

解决方案

没有内置到 datetime 会这样做。您需要使用以下方式:

  datetime.time(1).strftime('%I:%M%p ').lstrip('0')



附录



正如@naktinis所指出的,这是针对使用这个特定的 strftime 参数而定制的。不幸的是,如果 strftime 参数的内容是未知的或未指定的(例如外部参数),则不存在通用的解决方案,因为它成为做我的意思,不是什么我说问题。

因此,鉴于您必须知道您的 strftime 参数中的内容,复杂的情况你可以解决这个部分:

  tval = datetime.time(1)
tval_str =(tval。 strftime('%A,%B')+ tval.strftime('%d')。lstrip('0')
+ tval.strftime('%Y,')+ tval.strftime :%M')lstrip('0')
+ tval.strftime('%p')。lower())

re 模块:

  tval = datetime.time(1)
tval_str = re.sub(r^ 0 |(?<= \s)0,,
re.sub (?<= [0-9])[AP] M,lambda m:m.group()。lower(),
tval.strftime('%A,%B%d% %I:%M%p')))

请注意,如果%p术语给您大写字母,可能是因为用户将其区域设置为这样工作,通过更改大小写,您将覆盖用户首选项,有时会导致错误报告。此外,用户可能想要比am或pm(例如a.m.)等 和下午。另请注意,这些不同的区域设置是不同的(例如 en_US locale给出 AM PM for %p ,但 de_DE 给出 am pm ),并且您可能不会在您假设的编码中获取字符。



从<一个href =http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior =noreferrer>关于strftime行为的文档:


由于格式取决于当前的区域设置,因此在作出关于输出值的假设时应小心。字段排序将有所不同(例如,月/日/年与日/月/年),输出可能包含使用区域设置的默认编码编码的Unicode字符(例如,如果当前语言环境为 js_JP ,默认编码可以是 eucJP SJIS 中的任何一个,或 utf-8 ;使用 locale.getlocale()确定当前语言环境的编码)


所以,简而言之,如果你认为你需要重写区域设置,确保你有一个很好的理由,所以你不会最终创建新的错误。


I'm still a bit slow with Python, so I haven't got this figured out beyond what's obviously in the docs, etc.

I've worked with Django a bit, where they've added some datetime formatting options via template tags, but in regular python code how can I get the 12-hour hour without a leading zero?

Is there a straightforward way to do this? I'm looking at the 2.5 and 2.6 docs for "strftime()" and there doesn't seem to be a formatting option there for this case.

Should I be using something else?

Feel free to include any other time-formatting tips that aren't obvious from the docs. =)

解决方案

Nothing built-in to datetime will do it. You'll need to use something like:

datetime.time(1).strftime('%I:%M%p').lstrip('0')

Addendum

As @naktinis points out, this is tailored to the use of this particular strftime parameter. Unfortunately, there is no generic solution if the content of the strftime parameter is unknown or unspecified (e.g. an external parameter), because it becomes a "do what I mean, not what I say" problem.

Thus, given that you have to know what's in your strftime parameter, in a more complex case you could solve this as parts:

tval = datetime.time(1)
tval_str = (tval.strftime('%A, %B ') + tval.strftime('%d').lstrip('0') 
    + tval.strftime(' %Y, ') + tval.strftime('%I:%M').lstrip('0') 
    + tval.strftime('%p').lower())

or with the re module:

tval = datetime.time(1)
tval_str = re.sub(r"^0|(?<=\s)0", "", 
    re.sub(r"(?<=[0-9])[AP]M", lambda m: m.group().lower(), 
    tval.strftime('%A, %B %d %Y, %I:%M%p')))

That said, bear in mind that if the "%p" term gives you uppercase letters, it may be because the user set their locale to work that way, and by changing case you are overriding user preferences, which sometimes leads to bug reports. Also, the user may want something other than "am" or "pm", such as "a.m." and "p.m.". Also note that these are different for different locales (e.g. en_US locale gives AM or PM for %p, but de_DE gives am or pm) and you might not be getting characters in the encoding you assume.

From the documentation on strftime behavior:

Because the format depends on the current locale, care should be taken when making assumptions about the output value. Field orderings will vary (for example, "month/day/year" versus "day/month/year"), and the output may contain Unicode characters encoded using the locale’s default encoding (for example, if the current locale is js_JP, the default encoding could be any one of eucJP, SJIS, or utf-8; use locale.getlocale() to determine the current locale’s encoding).

So, in short, if you think you need to override locale settings, make sure you have a good reason why, so you don't just end up creating new bugs.

这篇关于获取“2:35 pm”;而不是“02:35 PM”从Python日期/时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆