I can output a locale sensitive time format using strftime('%X'), but this always includes seconds. How might I display this time format without seconds?

>>> import locale
>>> import datetime
>>> locale.setlocale(locale.LC_ALL, 'en_IE.utf-8')
'en_IE.utf-8'
>>> print datetime.datetime.now().strftime('%X')
12:22:43
>>> locale.setlocale(locale.LC_ALL, 'zh_TW.utf-8')
'zh_TW.utf-8'
>>> print datetime.datetime.now().strftime('%X')
12時22分58秒

The only way I can think of doing this is attempting to parse the output of locale.nl_langinfo(locale.T_FMT) and strip out the seconds bit, but that brings it's own trickery.

>>> print locale.nl_langinfo(locale.T_FMT)
%H時%M分%S秒
>>> locale.setlocale(locale.LC_ALL, 'en_IE.utf-8')
'en_IE.utf-8'
>>> print locale.nl_langinfo(locale.T_FMT)
%T

Solution:

(Based on pixelbeat's answer.)

# -*- coding: utf-8 -*-
import locale
def locale_time(t, show_seconds=False):
    if show_seconds:
        return t.strftime('%X')
    replacement_fmts = [
        (u'.%S', u''),
        (u':%S', u''),
        (u',%S', u''),
        (u':%OS', ''),
        (u'ཀསར་ཆ%S', u''),
        (u' %S초', u''),
        (u'%S秒', u''),
        (u'%r', '%I:%M %p'),
        (u'%t', '%H:%M'),
        (u'%T', '%H:%M')
    ]
    enc=locale.getpreferredencoding(do_setlocale=False)
    t_fmt = locale.nl_langinfo(locale.T_FMT).decode(enc)
    for fmt in replacement_fmts:
        new_t_fmt = t_fmt.replace(*fmt)
        if new_t_fmt != t_fmt:
            return t.strftime(new_t_fmt.encode(enc))
    return t.strftime(t_fmt.encode(enc)

Usage:

>>> locale.setlocale(locale.LC_ALL, 'en_IE.utf-8')
'en_IE.utf-8'
>>> print locale_time(t)
15:47
>>> locale.setlocale(locale.LC_ALL, 'zh_TW.utf-8')
'zh_TW.utf-8'
>>> print locale_time(t)
15時47分
share|improve this question
up vote 4 down vote accepted

I would suggest special casing the returned T_FMT as there aren't that many to consider really:

$ for l in $(locale -a | grep utf8); do locale | cut -d= -f1 | LANG=$l xargs locale -kc | grep ^t_fmt=; done | sort -u

t_fmt="%H:%M:%S"
t_fmt="%H.%M.%S"
t_fmt="%H시 %M분 %S초"
t_fmt="ཆུ་ཚོད%Hཀསར་མ%Mཀསར་ཆ%S"
t_fmt="%H时%M分%S秒"
t_fmt="%H时%M分%S秒 %Z"
t_fmt="%H時%M分%S秒"
t_fmt="%I.%M.%S %p"
t_fmt="%I:%M:%S  %Z"
t_fmt="%I:%M:%S %Z"
t_fmt="%I.%M.%S. %Z"
t_fmt="%I時%M分%S秒 %Z"
t_fmt="kl. %H.%M %z"
t_fmt="%k,%M,%S"
t_fmt="%k:%M:%S"
t_fmt="%l:%M:%S"
t_fmt="%OH:%OM:%OS"
t_fmt="%OI:%OM:%OS %p"
t_fmt="%p%I.%M.%S %Z"
t_fmt="%r"
t_fmt="%t"
t_fmt="%T"
t_fmt="%Z %I:%M:%S "
share|improve this answer
    
t_fmt="%t" is a tab, so obviously a bug. I assume it should have been "%T" – Tim Kersten Mar 24 '10 at 15:50

This is bad solution. What happens with some new different locale?

Use following:

t.strftime(gettext('%H:%M'))

Now each translator for each languages would provide suitable format for each string, for en_US it would be '%I:M %p', for zh_TW: %H時%M分

This is how usually problems of missing resource in standard localization tools are solved.

share|improve this answer
    
Yes this is the most general solution, but also adds more work for the translators, especially considering they would need to look up strftime formats. The "H:M" resource is missing but I think it's fairly sensible to infer it from the "H:M:S" resource considering how infrequently new locales (formats) are added. – pixelbeat Apr 6 '10 at 10:15

Consider ICU, and pyICU.

>>> from icu import *
>>> locale = Locale('en_US')
>>> dtpg = DateTimePatternGenerator.createInstance(locale)
>>> pattern = dtpg.getBestPattern('hm a')             
>>> sdf = SimpleDateFormat(pattern, locale)           
>>> sdf.format(1507059935.0)                  
u'12:34 PM'
>>> locale = Locale('de_DE')
>>> dtpg = DateTimePatternGenerator.createInstance(locale)
>>> pattern = dtpg.getBestPattern('hm a')             
>>> sdf = SimpleDateFormat(pattern, locale)            
>>> sdf.format(1507059935.0)                   
u'12:34 nachm.'
share|improve this answer
    
Though your code may be self-explanatory, your answer would be improved by adding short and simple explanations about what each step of the code does; this helps readers who may not be familiar with the libraries you are using. Good luck on your future SO answers! – ContinuousLoad Oct 3 '17 at 23:52

The Babel library provides an easy and reliable solution for this problem. Here is a sample code using Python 2.

#!/usr/bin/env python2
from datetime import datetime
from babel.dates import format_time

date_time = datetime.now()
formatted_time = format_time(date_time, format='short', locale='en_US')
print formatted_time
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.