![]() TypeError: can't convert String into an exact number So, for some non-integer argument examples: Time.at("1318996912") The docs also say "If a numeric argument is given, the result is in local time." which makes sense, but was a little confusing to me because they don't give any examples of non-integer arguments in the docs. Although, if we look at the strings, we can see they are indeed the same time, just printed in different time zones. Again, this is because the values are basically the same (although different classes, the #= method takes care of this for you), but the #to_s method prints drastically different strings. However, as had to remind me twice of: Time.at(1318996912) = DateTime.strptime("1318996912",'%s') # => trueĪn equality comparison between the two return values still returns true. So even though I passed the same integer to both methods, I seemingly two different results because of how the class' #to_s method works. ![]() DateTime.strptime("1318996912",'%s') # => Wed, 04:01:52 +0000ĭisplays a return value in UTC and requires the seconds to be a String and outputs a UTC Time object, whereas Time.at(1318996912) # => 00:01:52 -0400ĭisplays a return value in the LOCAL time zone, normally requires a FixNum argument, but the Time object itself is still in UTC even though the display is not. I just want to clarify, even though this has been commented so future people don't miss this very important distinction. Recent Update: I benchmarked the top solutions in this thread while working on a HA service a week or two ago, and was surprised to find that Time.at(.) outperforms DateTime.strptime(.) (update: added more benchmarks). Time.at(seconds_since_epoch_integer).to_datetimeīrief example (this takes into account the current system timezone): $ date +%s
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |