solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 229 forks source link

Add Integer.to_datetime and Float.to_datetime coercion #128

Closed brutuscat closed 11 years ago

brutuscat commented 11 years ago

This way integer unix-epoch timestamp and float unix-epoch timestamp with microseconds as fractional part can be coerced into a DateTime attribute.

Virtus::Coercion::Fixnum.to_datetime(0) 
=> Thu, 01 Jan 1970 00:00:00 +0000

date = Virtus::Coercion::Float.to_datetime(1000000000.999) 
=> Sun, 09 Sep 2001 01:46:40 +0000
date.to_f
=> 1000000000.999
date.strftime("%3N")  # milliseconds
=> "999"
brutuscat commented 11 years ago

Updated the code to avoid using "%s" in DateTime.strptime since there is a rubinius bug. See issue #2082

Now all tests pass

dkubb commented 11 years ago

@brutuscat I might suggest testing for RUBY_ENGINE before using it, eg:

RBX = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
mbj commented 11 years ago

@dkubb BTW for this stuff I'd like to see a gem. I see this kind of code quite often in devtools, tasks, edgecases etc. Your opinion? Sure platform specific hacks/branches should be avoided at all costs, but when we have to do them we should use our standard "deduplicate into library" pattern?

coveralls commented 10 years ago

Coverage Status

Changes Unknown when pulling 22010546c2a8d10a8691b354f7b44930af57c42b on brutuscat:master into \ on solnic:master**.