Oj Time dump - differences in linux and windows - ruby

I have a time stamp I am obtaining from an Access database. Unfortunately, the timestamp represents the time only, but is stored as 1899-12-30 13:05:00 +0000. The date part of it is in a different field. I need to preserve the data as much as possible while I am storing it in a json blob.
I am using Oj (which is wonderful) to dump the data to json. I have encountered the following behavior on the Windows platform:
irb(main):001:0> require 'oj'
=> true
irb(main):002:0> t = Time.new(1899,12,30,13,5,0) #this is my actual timestamp
=> 1899-12-30 13:05:00 +0000
irb(main):003:0> Oj.dump(t)
RangeError: bignum too big to convert into `long'
from (irb):3:in `dump'
from (irb):3
from C:/RailsInstaller/Ruby2.2.0/bin/irb:11:in `<main>'
irb(main):004:0>
Now, on linux:
2.3.0 :001 > require 'oj'
=> true
2.3.0 :002 > t = Time.new(1899,12,30,13,5,0)
=> 1899-12-30 13:05:00 +0000
2.3.0 :003 > Oj.dump(t)
=> "{\"^t\":-2209114500.000000000e0}"
I need to make this work on Windows because my source database is MS Access. Please help.

Time is not handled properly, but you can convert to float. The result string differ a bit from your original:
irb(main):001:0> require 'oj'
=> true
irb(main):002:0> t = Time.new(1899,12,30,13,5,0)
=> 1899-12-30 13:05:00 +0100
irb(main):003:0> Oj.dump(t)
RangeError: bignum too big to convert into `long'
from (irb):3:in `dump'
from (irb):3
from C:/Ruby23-x64/bin/irb.cmd:19:in `<main>'
irb(main):005:0> Oj.dump(t.to_f)
=> "-2209118100.0"
Or if you need the exact response you can use an array:
irb(main):001:0> require 'oj'
=> true
irb(main):002:0> t = Time.new(1899,12,30,13,5,0)
=> 1899-12-30 13:05:00 +0100
irb(main):012:0> A1 = Array.new(1)
=> [nil]
irb(main):017:0> A1[0] = '^t'
=> "^t"
irb(main):028:0> A1[1] = t.to_f
=> "-2209118100.0"
irb(main):035:0> Oj.dump(A1).tr("[","{").tr("]","}")
=> "{\"^t\",\"-2209118100.0\"}"

Related

How does using String#between? with DateTime objects work in Rails?

Using Ruby 2.3.2, Rails 5.0.0.1
In Ruby console
2.3.2 :001 > date_time_str = "2015-01-08 08:17:15 UTC"
=> "2015-01-08 08:17:15 UTC"
2.3.2 :002 > date_time_range_start = "2015-01-08 08:16:15 UTC"
=> "2015-01-08 08:16:15 UTC"
2.3.2 :003 > date_time_range_end = "2015-01-08 08:20:15 UTC"
=> "2015-01-08 08:20:15 UTC"
2.3.2 :008 > require 'date'
=> true
2.3.2 :009 > date_time_str.between?(DateTime.parse(date_time_range_start), DateTime.parse(date_time_range_end))
ArgumentError: comparison of String with DateTime failed
from (irb):9:in `between?'
from (irb):9
from /home/jignesh/.rvm/rubies/ruby-2.3.2/bin/irb:11:in `<main>'
I expected the above behaviour as I was attempting to compare a String instance with a DateTime instance. However when I attempt the same in rails console it returns expected result as shown below.
In Rails console
Loading development environment (Rails 5.0.0.1)
2.3.2 :001 > date_time_str = "2015-01-08 08:17:15 UTC"
=> "2015-01-08 08:17:15 UTC"
2.3.2 :002 > date_time_range_start = "2015-01-08 08:16:15 UTC"
=> "2015-01-08 08:16:15 UTC"
2.3.2 :003 > date_time_range_end = "2015-01-08 08:20:15 UTC"
=> "2015-01-08 08:20:15 UTC"
2.3.2 :004 > date_time_str.between?(DateTime.parse(date_time_range_start), DateTime.parse(date_time_range_end))
=> true
2.3.2 :005 > date_time_str_2 = "2015-01-08 08:21:15 UTC"
=> "2015-01-08 08:21:15 UTC"
2.3.2 :006 > date_time_str_2.between?(DateTime.parse(date_time_range_end), DateTime.parse(r_e))
=> false
Can anybody please help me understand what Rails does internally to make the above work?
String#<=> in ruby tries to compare itself to the other object. If the other object is incomparable (not a string), then the implementation for string specifically (c-source here) compares the other object to the receiving string.
In other words, "str" <=> obj is equivalent to
if obj.is_a?(String)
# compare bytes
elsif obj.respond_to?(:to_str)
obj = obj.to_str
# compare bytes
else
# a few safety checks
-1 * (obj <=> "str")
end
Now we have "str" <=> obj boiling down to obj <=> "str". So far this is just Ruby, no Rails. Since Datetime#<=> has no special handling for string, it returns nil and story ends here.
In Rails, they redefined Datetime#<=> and defined String#to_datetime

Encoding and decoding ruby symbols

I discovered this behavior of multi_json ruby gem:
2.1.0 :001 > require 'multi_json'
=> true
2.1.0 :002 > sym = :symbol
=> :symbol
2.1.0 :003 > sym.class
=> Symbol
2.1.0 :004 > res = MultiJson.load MultiJson.dump(sym)
=> "symbol"
2.1.0 :005 > res.class
=> String
Is this an appropriate way to store ruby symbols? Does JSON provide some way to distinguish :symbol from "string"?
Nope is the simple answer. Most of the time it only really matters for hashes and there's a cheat on hashes, symbolize_keys!. Bottom line is that JSON does not understand symbols, just strings.
Since you are using MultiJson, you can also ask MultiJson to do this for you...
MultiJson.load('{"abc":"def"}', :symbolize_keys => true)

Convert string datetime to Ruby datetime

How do I convert this "2013-10-20 18:36:40" into a Ruby datetime?
I'm trying the following, but it's not working:
"2013-10-20 18:36:40".to_datetime
That's making it this and missing the time:
2013-10-20 00:00:00 UTC
Use DateTime::strptime:
require 'date'
DateTime.strptime("2013-10-20 18:36:40", "%Y-%m-%d %H:%M:%S")
#<DateTime: 2013-10-20T18:36:40+00:00 ((2456586j,67000s,0n),+0s,2299161j)>
You can do the following if rails is installed on your system:--
require 'active_support/core_ext/string/conversions'
"2013-10-20 18:36:40".to_time
1.9.2p320 :001 > require 'active_support/core_ext/string/conversions'
=> true
1.9.2p320 :003 > "2013-10-20 18:36:40".to_time
=> 2013-10-20 18:36:40 UTC
There is also DateTime#parse method:
2.1.0 :001 > require 'date'
=> true
2.1.0 :002 > DateTime.parse('2013-10-20 18:36:40')
=> #<DateTime: 2013-10-20T18:36:40+00:00 ((2456586j,67000s,0n),+0s,2299161j)>
If your work with rails consider writing timezone-safe code:
Time.zone.parse("2013-10-20 18:36:40")
http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails

How are named capture groups used in RE2 regexps?

On this page http://swtch.com/~rsc/regexp/regexp3.html it says that RE2 supports named expressions.
RE2 supports Python-style named captures (?P<name>expr), but not the
alternate syntaxes (?<name>expr) and (?'name'expr) used by .NET and
Perl.
ruby-1.9.2-p180 :003 > r = RE2::Regexp.compile("(?P<foo>.+) bla")
#=> #<RE2::Regexp /(?P<foo>.+) bla/>
ruby-1.9.2-p180 :006 > r = r.match("lalal bla")
#=> #<RE2::MatchData "lalal bla" 1:"lalal">
ruby-1.9.2-p180 :009 > r[1] #=> "lalal"
ruby-1.9.2-p180 :010 > r[:foo]
TypeError: can't convert Symbol into Integer
ruby-1.9.2-p180 :011 > r["foo"]
TypeError: can't convert String into Integer
But I'm not able to access the match with the name, so it seems like a useless implementation. Am I missing something?
Looking at your code output, it seems that you are using the Ruby re2 gem which I maintain.
As of the latest release (0.2.0), the gem does not support the underlying C++ re2 library's named capturing groups. The error you are seeing is due to the fact that any non-integer argument passed to MatchData#[] will simply be forwarded onto the default Array#[]. You can confirm this in an irb session like so:
irb(main):001:0> a = [1, 2, 3]
=> [1, 2, 3]
irb(main):002:0> a["bob"]
TypeError: can't convert String into Integer
from (irb):2:in `[]'
from (irb):2
from /Users/mudge/.rbenv/versions/1.9.2-p290/bin/irb:12:in `<main>'
irb(main):003:0> a[:bob]
TypeError: can't convert Symbol into Integer
from (irb):3:in `[]'
from (irb):3
from /Users/mudge/.rbenv/versions/1.9.2-p290/bin/irb:12:in `<main>'
I will endeavour to add the ability to reference captures by name as soon as possible and update this answer once a release has been made.
Update: I just released version 0.3.0 which now supports named groups like so:
irb(main):001:0> r = RE2::Regexp.compile("(?P<foo>.+) bla")
=> #<RE2::Regexp /(?P<foo>.+) bla/>
irb(main):002:0> r = r.match("lalal bla")
=> #<RE2::MatchData "lalal bla" 1:"lalal">
irb(main):003:0> r[1]
=> "lalal"
irb(main):004:0> r[:foo]
=> "lalal"
irb(main):005:0> r["foo"]
=> "lalal"

File.(filename).mtime the same output format like Date.today (ruby)

I want to subtract the result of File.mtime and Date.today. Time and weekends are ignored. I am after something like
Date.today - File.mtime
File.mtime gives me Fri Oct 08 11:00:18 +1100 2010
and
Date.today 2010-10-11
I thought that to_s() would work for me but File.mtime(filename).to_s gives me the same result like File.mtime(filename)
any idea how I can get the desired date format from File.mtime? And why to_s is not working?
Something like this?
irb(main):001:0> File.mtime("file")
=> 2010-10-08 17:56:10 +0800
irb(main):002:0> File.mtime("file").year
=> 2010
irb(main):003:0> File.mtime("file").month
=> 10
irb(main):004:0> File.mtime("file").day
=> 8
Similarly with Date
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.today
=> #<Date: 2010-10-11 (4910961/2,0,2299161)>
irb(main):003:0> Date.today.year
=> 2010
irb(main):004:0> Date.today.month
=> 10
irb(main):005:0> Date.today.day
=> 11
Or you can use strftime
irb(main):001:0> File.mtime("file").strftime("%Y-%m-%d")
=> "2010-10-08"
Try using Time.now and convert to days manually:
age = Time.now - File.mtime(filename)
age_in_days = (age / 24*60*60).to_i

Resources