Skip to content

Instantly share code, notes, and snippets.

@abstrctn
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abstrctn/10621244 to your computer and use it in GitHub Desktop.
Save abstrctn/10621244 to your computer and use it in GitHub Desktop.
"Special" day formats
# http://www.timeanddate.com/date/special-dates.html
patterns = [
"%Y-%m-%d", # 2001-01-31
"%y-%m-%d", # 01-01-31
"%Y-%-m-%-d", # 2001-1-31
"%y-%-m-%-d", # 01-1-31
"%m-%d-%Y", # 01-31-2001
"%m-%d-%y", # 01-31-01
"%-m-%-d-%Y", # 1-31-2001
"%-m-%-d-%y", # 1-31-01
"%d-%m-%Y", # 31-01-2001
"%d-%m-%y", # 31-01-01
"%-d-%-m-%Y", # 31-1-2001
"%-d-%-m-%y", # 31-1-01
]
special = []
# http://www.ssa.gov/planners/lifeexpectancy.htm
days = (82.1 * 365).to_i
start = Date.parse("1990-08-20")
days.times do |idx|
date = start + idx
special_date = false
patterns.each do |pattern|
formatted = date.strftime(pattern)
split_formatted = formatted.gsub(/-/, '').split('')
is_special = false
# Palindrome
palin = true
split_formatted.each_with_index do |char, i|
palin &= char == split_formatted[split_formatted.length - 1 - i]
end
is_special ||= palin
# Sequential
sections = formatted.split('-').map(&:to_i)
is_special ||= (sections[0] == sections[1] + 1) && (sections[0] == sections[2] + 2)
is_special ||= (sections[0] == sections[1] - 1) && (sections[0] == sections[2] - 2)
# Repeating sequences
(1..4).each do |length|
groups = split_formatted.each_slice(length).to_a
is_special ||= (groups.size > 1 && groups.uniq.size == 1)
end
if is_special
puts formatted
special_date = true
end
end
if special_date
special << date
end
end
puts
puts "There are at least #{special.size} days in your life with special formatting (out of #{days} total)."
puts "That comes out to #{special.size.to_f / days}% of the time, or on average once every #{1 / (special.size.to_f / days)} days."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment