Skip to content

Instantly share code, notes, and snippets.

@robmiller
Last active December 24, 2015 21:49
Show Gist options
  • Save robmiller/6868358 to your computer and use it in GitHub Desktop.
Save robmiller/6868358 to your computer and use it in GitHub Desktop.
Sometimes I want to do natural language datetime calculations from the commandline, just to work out things like "what date will it be in 60 days" or "when was 30 days ago". This helps.
#!/usr/bin/env ruby
# Requirements: Ruby and the Chronic gem
#
# Install Chronic with: `gem install chronic`
#
# Examples:
#
# $ strtotime 'in 60 days'
# 6 Dec 2013 14:55:44
#
# $ strtotime '30 days ago' '2013-01-01'
# 2 Dec 2013 12:00:00
#
# Installation (adjust paths as appropriate):
#
# 1. Download it
# 2. mv ~/Downloads/strtotime.rb ~/bin/strtotime
# 3. chmod +x ~/bin/strtotime
require 'chronic'
now = ARGV[1] ? Chronic.parse(ARGV[1]) : Time.now
time = Chronic.parse(ARGV[0], :now => now)
if time
puts time.strftime('%-d %b %Y %H:%M:%S')
else
$stderr.puts "Couldn't parse date"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment