-
-
Save zed/11524fa26fa6882ad4d0 to your computer and use it in GitHub Desktop.
Parse human-readable dates in French
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
See | |
http://stackoverflow.com/questions/26294333/parse-french-date-in-python | |
""" | |
import parsedatetime as pdt | |
class pdtLocale_fr(pdt.pdt_locales.pdtLocale_icu): | |
def __init__(self): | |
super(pdtLocale_fr, self).__init__(localeID='fr_FR') | |
self.dayOffsets.update({u"aujourd'hui": 0, u'demain': 1, u'hier': -1}) | |
pdt.pdtLocales['fr_FR'] = pdtLocale_fr | |
calendar = pdt.Calendar(pdt.Constants(localeID='fr_FR', usePyICU=False)) | |
for date_string in [u"Aujourd'hui", "3 juillet", u"4 Août", u"Hier", | |
u"au jour de hui", u"aujour-d’hui", | |
u"au-jour-d’hui", "demain", "hier", | |
u"today", "tomorrow", "yesterday"]: | |
dt, rc = calendar.parseDT(date_string) | |
if rc > 0: | |
print(date_string, dt.date()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment