Created
October 10, 2013 11:19
-
-
Save florentx/6916819 to your computer and use it in GitHub Desktop.
localized date parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dateutil.parser | |
from babel.core import Locale | |
class LocalizedParserinfo(dateutil.parser.parserinfo): | |
def __init__(self, locale): | |
locale = Locale.parse(locale) | |
# Build localized list of words, remove dots | |
self.WEEKDAYS = [(locale.days['format']['wide'][i], | |
locale.days['format']['abbreviated'][i].rstrip('.')) | |
for i in range(7)] | |
self.MONTHS = [(locale.months['format']['wide'][i], | |
locale.months['format']['abbreviated'][i].rstrip('.')) | |
for i in range(1, 13)] | |
super(LocalizedParserinfo, self).__init__() | |
def __call__(self): | |
return self | |
class DatetimeParser(object): | |
def __init__(self, locale): | |
self.info = LocalizedParserinfo(locale) | |
def parse(self, s): | |
return dateutil.parser.parse(s, parserinfo=self.info) | |
if __name__ == '__main__': | |
sample = u'14/Févr./2013 10:55:50' | |
result = DatetimeParser('fr_FR').parse(sample) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment