I had to maintain someone else's code, and when I followed the code, it was a hassle to convert the
% j date to ʻUNIX time`.
Then, when I was disgusted and searched for "
strptime?", Bingo !!
Test before replacing.
Execute the following code
$ ruby -e 'require "time" ; p Time.strptime("2003,070,12:21:51 JST", "%Y,%j,%H:%M:%S %z").to_i' 1041391311 # ↑ 2003/01/01 12:21:51 JST
a problem occured.
% J Can you interpret it?
I couldn't get an answer even if I looked at Gugu [^ 1]
[^ 1]: It's special to use
% j in the first place ...
Try various things ...
.strptime of the
Time class (module?) Of the old
ruby doesn't seem to interpret
% j [^ 2]
In the verification,
2.7.1p83 was OK [^ 3]
[^ 2]: So, regarding code replacement, there are various problems with using
DateTime, so in the end, in the form of embedding the self-made code created for verification ,,,
2.6.3p62 is OK.
Execute the following code with
require 'date' require 'time' #Self-made function for control def manu(t) arr = t.split(/[,: ]/).map(&:to_i) Time.mktime(arr,1,1,arr,arr,arr) + ( arr - 1) * 24 * 60 * 60 end str = "2003,070,12:21:51 JST" # DateTime.The result of strptime p DateTime.strptime(str, "%Y,%j,%H:%M:%S %z").strftime("%s").to_i #Result of self-made function p ( manu str ).to_i # Timme.The result of strptime p Time.strptime(str, "%Y,%j,%H:%M:%S %z").to_i
Time.strptime does not interpret
DateTime.strptime will interpret it.
$ ruby test.rb 1047352911 1047352911 1041391311 # <= 2003/01/01 12:21:51 JST
On the other hand,
2.7.1 also interprets
$ ruby test.rb 1047352911 1047352911 1047352911
If there is something like the
ruby version of
delta, it would be one shot if you read it, but that passion does not spring up ...