Skip to content

Instantly share code, notes, and snippets.



Last active Nov 1, 2019
What would you like to do?
Date converter utility for things that want shitty Unix timestamps
# This program converts dates (from a semi-memorable, short format similar to the standard `date` utility).
# It will output them as unix timestamps (it will also accept ISO dates for this conversion).
# Or, it can output them in ISO8601, but this will always be in UTC because timezones are pointless.
require 'date'
class Usage < StandardError
Debug = false
Full = ARGV.delete('-d') or ARGV.delete('--date')
def date
raise Usage unless ARGV.length == 1
i = ARGV[0]
more, s = i.split ?.
if more.match /\A[\d-]+T[\d:]+(Z|\+.*)\Z/
return more # Already an ISO date
raise Usage unless more.length % 2 == 0
now =
more = more.scan /../
case more.length
when 1
h, = more
when 2
h, mi = more
when 3
d, h, mi = more
when 4
mo, d, h, mi = more
when 5
mo, d, h, mi, _y = more
y = now.year - (now.year % 100) + _y.to_i
when 6
mo, d, h, mi, _c, _y = more
y = _c.to_i * 100 + _y.to_i
raise Usage
Debug and p y, mo, d, h, mi, s
y ||= now.year
mo ||= now.month
d ||=
mi ||= '00'
s ||= '00'
Debug and puts
Debug and p y, mo, d, h, mi, s
return "#{y}-#{mo}-#{d}T#{h}:#{mi}:#{s}Z"
rescue Usage
$stderr.puts "Usage: #{$0} [-d|--date] [[MM]DD]hhmm[[CC]YY][.ss]"
$stderr.puts " --date: Print UTC (Z) ISO date string instead of timestamp"
exit 1
if Full
puts DateTime.parse(date).new_offset(0).iso8601.sub('+00:00','Z')
puts DateTime.parse(date).to_time.to_i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment