Created
October 24, 2014 07:20
-
-
Save zed/591db3af5a35be3f4deb to your computer and use it in GitHub Desktop.
Parse date based on locale :python:
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
| # Answer for http://stackoverflow.com/questions/26528007/parse-date-based-on-locale-php-python | |
| from datetime import date | |
| import parsedatetime as pdt # $ pip install parsedatetime | |
| def unix(utc_date, epoch_ord=date(1970, 1, 1).toordinal()): | |
| return (utc_date.toordinal() - epoch_ord) * 86400 | |
| for date_string, locale in [("30 juin 2009", "fr_FR"), | |
| ("30.06.2009", "de_DE"), | |
| ("Jun 30, 2009", "en_US")]: | |
| calendar = pdt.Calendar(pdt.Constants(localeID=locale)) | |
| d, rc = calendar.parseDT(date_string) | |
| if rc == 1: # parsed as date | |
| print(unix(d)) | |
| else: | |
| assert 0, date_string | |
| """ | |
| ### Output | |
| 1246320000 | |
| 1246320000 | |
| 1246320000 | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment