0

We're using Babel to format datetime objects nicely according to the user's locale. In many cases we show data which does not contain non-zero seconds ever (such as real-life event start times).

While it'd be easy with a custom format string to get rid of the seconds, there doesn't appear any easy way to do so when using the locale-provided ones like 'medium' or 'short'.

Is there any clean way to modify these strings besides hooking into babel's internals to modify the format string returned from the locale data?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636

1 Answers1

1

Maybe by subclassing Locale and overriding the formatting properties?

class MyLocale(babel.Locale):
    @property
    def time_formats(self):
        formats = super(MyLocale, self).time_formats
        for k, v in formats.items():
            v.format = v.format.replace(':%(ss)s', '')
        return formats

>> l = MyLocale('en')
>> l.time_formats['medium'].apply(datetime.datetime.now(), l)
u'9:05 AM'
satoru
  • 31,822
  • 31
  • 91
  • 141