Created
February 1, 2013 10:31
-
-
Save wteuber/4690545 to your computer and use it in GitHub Desktop.
Converts a german date string to a Time object
The format of the german date is '%A, %d. %B %Y, %H:%M Uhr'
e.g.: 'Dienstag, 29. Januar 2013, 19:27 Uhr'
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
require 'date' | |
# Converts a german date string to a Time object | |
# The format of the german date is '%A, %d. %B %Y, %H:%M Uhr' | |
# e.g.: 'Dienstag, 29. Januar 2013, 19:27 Uhr' | |
# | |
# @params [String] german date | |
# @return [Time] | |
def intl_time(de_date) | |
en_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] | |
de_days = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] | |
en_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] | |
de_months = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"] | |
i18n_months = Hash[de_months.zip en_months] | |
i18n_days = Hash[de_days.zip en_days] | |
en_date = de_date | |
[i18n_months, i18n_days].each do |i18n| | |
i18n.each do |de, en| | |
en_date.gsub!(/#{de}/, en) | |
end | |
end | |
DateTime.strptime(en_date, '%A, %d. %B %Y, %H:%M Uhr').to_time.utc | |
end | |
de_date = 'Dienstag, 29. Januar 2013, 19:27 Uhr' | |
time = intl_time(de_date) | |
puts time.strftime('%Y-%m-%d %H:%M:%S') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment