Created
December 30, 2014 21:56
-
-
Save jennifersmith/e5724c2fcc08f81b9c9e to your computer and use it in GitHub Desktop.
This is a bit of a rubbish script that spiders thru json and decides whether something is a timestamp or not on some assy heuristics - basically if you have a bunch of numbers and you think some of them are dates then it will convert them
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 | |
require 'json' | |
require 'pry' | |
require 'date' | |
# basically if the number results in a ts that is less than 10 years ago it's probably a timestamp :D | |
YEARS_TO_BE_CONSIDERED_TIMESTAMP = 10 | |
def fix_potential_timestamp potential_timestamp | |
# binding.pry | |
as_timestamp = DateTime.strptime(potential_timestamp.to_s, "%Q") | |
if (DateTime.now.year - as_timestamp.year) > YEARS_TO_BE_CONSIDERED_TIMESTAMP | |
potential_timestamp | |
else | |
as_timestamp.strftime("%FT%T.%3NZ") | |
end | |
end | |
def fix_date data | |
case data | |
when String | |
data | |
when Fixnum | |
fix_potential_timestamp(data) | |
when Array | |
data.map {|x| fix_date(x)} | |
when Hash | |
data.inject({}) do |h, (k, v)| | |
h[k] = fix_date(v) | |
h | |
end | |
end | |
end | |
#ARGF.each do |line| | |
#puts JSON.generate(fix_date(JSON.parse(line))) | |
#end | |
ARGF.each do |line| | |
puts line | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment