Created
July 7, 2020 22:30
-
-
Save mkasberg/a91de2c709c54db5ddae5c6739b6b4be to your computer and use it in GitHub Desktop.
Shift dates uniformly in any XML file
This file contains hidden or 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 | |
require 'time' | |
usage = """ | |
Usage: date_shift.rb SECONDS FILE | |
Look for dates like 2020-06-01T12:26:41Z in FILE | |
and shift all of them by SECONDS. Print the result to | |
STDOUT. | |
E.g. date_shift.rb +100 in.txt > out.txt | |
""" | |
unless ARGV.length == 2 | |
print "Incorrect args!" | |
print usage | |
exit 1 | |
end | |
seconds = ARGV[0].to_i | |
infile = ARGV[1] | |
input = File.read(infile) | |
result = input.gsub(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/) do |dateString| | |
time = Time.xmlschema(dateString) | |
(time + seconds).xmlschema() | |
end | |
print result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment