[RUBY] In Time.strptime,% j (total date of the year) is

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 " ruby but strptime?", Bingo !!

a problem occured

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.

Eh? % 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 ...

Conclusion

The .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 ,,,

[^ 3]: 2.6.3p62 is OK.

Verification

Execute the following code with 1.9.2p290, 2.0.0p481, 2.7.1p83, respectively.

test.rb


require 'date'
require 'time'

#Self-made function for control
def manu(t)
  arr = t.split(/[,: ]/).map(&:to_i)
  Time.mktime(arr[0],1,1,arr[2],arr[3],arr[4]) + ( arr[1] - 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

In 1.9.2 and 2.0.0, Time.strptime does not interpret% j. DateTime.strptime will interpret it.

shell-session:1.9.2p290,2.0.0p481


$ ruby test.rb
1047352911
1047352911
1041391311  # <= 2003/01/01 12:21:51 JST

On the other hand, 2.7.1 also interprets Time.strptime.

shell-session:2.7.1p83


$ 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 ...

Recommended Posts

In Time.strptime,% j (total date of the year) is
When the month of the date is acquired, the January shift
Was done in the base year of the Java calendar week
Determine that the value is a multiple of 〇 in Ruby
What is the representation of domain knowledge in the [DDD] model?
'% 02d' What is the percentage of% 2?
The design concept of Java's Date and Time API is interesting
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
What is @Override or @SuppressWarnings ("SleepWhileInLoop") in front of the function? ?? ??
[Reference example] Copyright is described in the comment of the source code.
Order of processing in the program
[Swift] Determine the constellation from the date of birth entered in the UIDatePicker
Code of the part where server.servlet.session.timeout is set in spring.session.timeout in spring-boot2 system
[Payment of work] Summarize the mistakes made in 2020 [2nd year from inexperience]
This and that of the implementation of date judgment within the period in Java
Where is the home directory location of "Ubuntu"? (Solved in the comment section)
Error when the member of Entity class used in SpringWebFlux is final
How to find the total number of pages when paging in Java
[Git] The horrifying story of deleting the master branch ~ The answer is in English ~
Get the result of POST in Java
Spring Autowired is written in the constructor
What is testing? ・ About the importance of testing
The identity of params [: id] in rails
The story of AppClip support in Anyca
What is the main method in Java?
What is the data structure of ActionText?
How to get the date in java
The story of writing Java in Emacs
Write the movement of Rakefile in the runbook
In the topic of "total concentration", "How to use Docker" is summarized for the virtual Mameko who sleeps in me.
Use hashes well in Ruby to calculate the total amount of an order
Login fails because the redirect URL of the self-login screen is incorrect in spring-security
[Java] Where is the implementation class of annotation that exists in Bean Validation?
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Retrieve the first day of week in current locale (what day of the week is it today?)
Whether the total product of the first n prime numbers plus 1 is a prime number
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
[Java] Calculate the day of the week from the date (Calendar class is not used)
Correspondence of the part where Authentication # getDetails is done in the unit test of spring-security
[Order method] Set the order of data in Rails
What is Pullback doing in The Composable Architecture
What is JSP? ~ Let's know the basics of JSP !! ~
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
ActiveSupport underscore is not the inverse of camelize
About the idea of anonymous classes in Java
The order of Java method modifiers is fixed
The ruby version is managed in the .rbenv / version file
The intersection type introduced in Java 10 is amazing (?)
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
The date time of java8 has been updated
The official name of Spring MVC is Spring Web MVC
[Java] Something is displayed as "-0.0" in the output
Import files of the same hierarchy in Java
When the project is not displayed in eclipse
Ebean.update () is not executed in the inherited model.