Last active
December 24, 2015 21:49
-
-
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.
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
#!/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