Last active
October 5, 2015 06:18
-
-
Save mattroyer/2763152 to your computer and use it in GitHub Desktop.
Date Formatting
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
# Date Formatting | |
# From: http://www.ludism.org/mentat/DateCode | |
# =========================== # | |
# FORMAT FOR YEAR 2000-2099 # | |
# =========================== # | |
2-digit-year|month|day | |
# ======================== # | |
# FORMAT FOR YEAR 0-3199 # | |
# ======================== # | |
31-digit-0:V|2-digit-year|month|day | |
# ==================== # | |
# MONTHS OF THE YEAR # | |
# ==================== # | |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | |
Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec | |
# ================== # | |
# DAYS OF THE WEEK # | |
# ================== # | |
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31 | |
. . . . . . . . . A B C D E F G H I J K L M N O P Q R S T U V | |
# ==================================== # | |
# STANDARD ALPHABET LETTER POSITIONS # | |
# ==================================== # | |
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26 | |
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z | |
# =================================== # | |
# ALPHABET POSITION MNEMONIC: EJOTY # | |
# =================================== # | |
5 10 15 20 25 | |
E J O T Y | |
# ========== # | |
# EXAMPLES # | |
# ========== # | |
Today's Date: | |
125L => (May 21, 2012) | |
My Birthday: | |
J821F => (January 15, 1982) |
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
# Decode dates encoded in a special format | |
# | |
# Example: | |
# | |
# > ruby dateCode.rb 125M | |
# => The date is: May 22, 2012 | |
# Define a method to decode the dates | |
def dateDecode(date) | |
# Create a hash of the numbers 10-31 | |
# and assign them to their corresponding | |
# letters | |
numbersTo31 = Hash[('A'..'V').zip('10'..'31')] | |
# Create a hash of the months of the year | |
# and assign them to numbers | |
monthNames = {'1' => 'January', '2' => 'February', '3' => 'March', | |
'4' => 'April', '5' => 'May', '6' => 'June', | |
'7' => 'July', '8' => 'August', '9' => 'September', | |
'A' => 'October', 'B' => 'November', 'C' => 'December' } | |
# Take the length of the date input and | |
# depending on if it's 4 or 5 characters | |
# long, assign the needed variables to the | |
# correct indexes: century, year, month, day | |
# | |
# If the length is not 4 or 5 characters | |
# long, then output an error message to | |
# the console and end the program by | |
# exiting | |
if date.length == 4 | |
year = date[0..1] | |
month = date[2..2] | |
day = date[3..3] | |
elsif date.length == 5 | |
century = date[0..0] | |
year = date[1..2] | |
month = date[3..3] | |
day = date[4..4] | |
else | |
puts | |
puts "Wrong date format. Please try again." | |
exit | |
end | |
# If century exists (by having a length of 5 characters) | |
# depending on if the number is higher than 9 or not, | |
# create a variable named prefix that will hold either | |
# the number 1-9 or one of the letters in the | |
# numbersTo31 hash | |
# | |
# If century doesn't exist (by having a length of 4 characters) | |
# then the prefix will equal 20 | |
prefix = century ? numbersTo31[century] || century : '20' | |
# Output a string to the console containing the converted | |
# date, along with the extra "The date is: " string | |
puts | |
puts "The date is: #{monthNames[month]} #{numbersTo31[day] || day}, #{prefix}#{year}" | |
end | |
# Call the method and pass ARGV[0] to grab | |
# input from the user when they run the app | |
dateDecode(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment