[RUBY] How is the next value of the Time object correct?

Overview

When dealing with Time objects in Ruby, I run into a boundary problem. If you do it normally, it will not be a problem using the Range object, but I stumbled on it in an unusual way.

environment

Ruby: 2.6.2 Rails: 5.1.2

The problem I was addicted to in the first place

Time.now.end_of_day
=> 2020-05-15 23:59:59 +0900

I want to take the next value of this.

This was the first thing I came up with

Time.now.end_of_day + 1
=> 2020-05-16 00:00:00 +0900

However, this is a mistake

(Time.now.end_of_day + 1).iso8601(3)
=> "2020-05-16T00:00:00.999+09:00"

+ 1 is an addition of 1 second until you get tired of it, and it does not move to the next value, so it cannot be used.

When I was fishing the Time class, I found a method that looked like that. https://docs.ruby-lang.org/ja/latest/method/Time/i/succ.html

Time.now.end_of_day.succ
(pry):109: warning: Time#succ is obsolete; use time + 1
=> 2020-05-16 00:00:00 +0900

However, it seems that he is just doing the same thing as + 1 because he is angry when he uses it because it is outdated. No good.

Solution

After all, I didn't know how to do it, so I pushed it

In the example problem, I only see a small number of 9 digits

(Time.now.end_of_day + 1).iso8601(10)
=> "2020-05-16T00:00:00.9999999990+09:00"

In other words, add 1 nanosecond.

(Time.now.end_of_day + 1/1000000000.0).iso8601(9)
=> "2020-05-16T00:00:00.000000000+09:00"

did it!

bonus

Time.now.iso8601(10)
=> "2020-05-15T19:56:55.4979370000+09:00"

Time.new(2020, 5, 16, 16, 59, 59.3).iso8601(9)
=> "2020-05-16T16:59:59.299999999+09:00"

Time.parse("2020-05-16T16:59:59.3+09:00").iso8601(9)
=> "2020-05-16T16:59:59.300000000+09:00"

Time.parse("2020-05-16T16:59:59.35555555555555555555+09:00").iso8601(20)
=> "2020-05-16T16:59:59.35555555555555555555+09:00"

Was no good

Conclusion

--You can have an infinite number of fractional seconds in a Time object --Therefore, there is no concept of the next value in the first place. ――However, it depends on how the Time object is created, so if you pull it from the DB, you can force it if you consider the number of valid digits. --In the first place, you should compare using the Range object obediently

Recommended Posts

How is the next value of the Time object correct?
Android development, how to check null in the value of JSON object
How far is the correct answer to divide the process?
[Java] How to get the maximum value of HashMap
The illusion of object orientation
How to change the setting value of Springboot Hikari CP
[Swift] Get the timing when the value of textField is changed
What is an immutable object? [Explanation of how to make]
'% 02d' What is the percentage of% 2?
[Rails] Check the contents of the object
How to display 0 on the left side of the standard input value
The design concept of Java's Date and Time API is interesting
How to output the value when there is an array in the array
How to increment the value of Map in one line in Java
The content of the return value of executeBatch is different between 11g and 12c
How to change the value of a variable at a breakpoint in intelliJ
How to implement the email authentication function at the time of user registration
[Rails] How to solve the time lag of created_at after save method
About the operation of next () and nextLine ()
How to determine the number of parallels
[Java] How to set the Date time to 00:00:00
What is the data structure of ActionText?
Correct the stop position of smoothScrollToPosition of RecyclerView
How to pass the value to another screen
[swift] How to control the behavior when the back button of NavigationBar is pressed
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
What is JSP? ~ Let's know the basics of JSP !! ~
Verify the contents of the argument object with Mockito
ActiveSupport underscore is not the inverse of camelize
Customize how to divide the contents of Recyclerview
The order of Java method modifiers is fixed
Set the time of LocalDateTime to a specific time
Feel the passage of time even in Java
How to get today's day of the week
Output of how to use the slice method
About next () and nextLine () of the Scanner class
How Microservices Change the Way of Developing Applications
How to display the result of form input
The date time of java8 has been updated
Specify the default value with @Builder of Lombok
The official name of Spring MVC is Spring Web MVC
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
[Comparison verification] How different is the development productivity of Spring Boot apps from the past?
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
[ruby] How to assign a value to a hash by referring to the value and key of another hash