Skip to content

Instantly share code, notes, and snippets.

@mkasberg
Created July 7, 2020 22:30
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 mkasberg/a91de2c709c54db5ddae5c6739b6b4be to your computer and use it in GitHub Desktop.
Save mkasberg/a91de2c709c54db5ddae5c6739b6b4be to your computer and use it in GitHub Desktop.
Shift dates uniformly in any XML file
#!/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