mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Comparing DateTimes Down To Second Precision as a rails til
This commit is contained in:
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||||
|
|
||||||
_895 TILs and counting..._
|
_896 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -527,6 +527,7 @@ _895 TILs and counting..._
|
|||||||
- [Change The Nullability Of A Column](rails/change-the-nullability-of-a-column.md)
|
- [Change The Nullability Of A Column](rails/change-the-nullability-of-a-column.md)
|
||||||
- [Check Specific Attributes On ActiveRecord Array](rails/check-specific-attributes-on-activerecord-array.md)
|
- [Check Specific Attributes On ActiveRecord Array](rails/check-specific-attributes-on-activerecord-array.md)
|
||||||
- [Code Statistics For An Application](rails/code-statistics-for-an-application.md)
|
- [Code Statistics For An Application](rails/code-statistics-for-an-application.md)
|
||||||
|
- [Comparing DateTimes Down To Second Precision](rails/comparing-datetimes-down-to-second-precision.md)
|
||||||
- [Conditional Class Selectors in Haml](rails/conditional-class-selectors-in-haml.md)
|
- [Conditional Class Selectors in Haml](rails/conditional-class-selectors-in-haml.md)
|
||||||
- [Convert A Symbol To A Constant](rails/convert-a-symbol-to-a-constant.md)
|
- [Convert A Symbol To A Constant](rails/convert-a-symbol-to-a-constant.md)
|
||||||
- [Creating Records of Has_One Associations](rails/creating-records-of-has-one-associations.md)
|
- [Creating Records of Has_One Associations](rails/creating-records-of-has-one-associations.md)
|
||||||
|
|||||||
31
rails/comparing-datetimes-down-to-second-precision.md
Normal file
31
rails/comparing-datetimes-down-to-second-precision.md
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# Comparing DateTimes Down To Second Precision
|
||||||
|
|
||||||
|
You may have an RSpec test for your Rails codebase that asserts about the
|
||||||
|
datetime a record gets saved with:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
two_weeks_ago = 2.weeks.ago
|
||||||
|
|
||||||
|
record = Thing.create(two_weeks_ago)
|
||||||
|
|
||||||
|
expect(record.some_date_time).to eq(two_weeks_ago)
|
||||||
|
```
|
||||||
|
|
||||||
|
This comparison happens with precision down to the nanosecond. Unfortunately,
|
||||||
|
depending on your operating system and backing database, you may see
|
||||||
|
inconsistent results due to variations in precision.
|
||||||
|
|
||||||
|
One way to deal with this, if you only care about precision down to the second,
|
||||||
|
is to modify the expecationa little with the [`be_within`
|
||||||
|
matcher](https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/be-within-matcher).
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
two_weeks_ago = 2.weeks.ago
|
||||||
|
|
||||||
|
record = Thing.create(two_weeks_ago)
|
||||||
|
|
||||||
|
expect(record.some_date_time).to be_within(1.second).of(two_weeks_ago)
|
||||||
|
```
|
||||||
|
|
||||||
|
The `be_within` matcher can also be used as [a nested
|
||||||
|
matcher](https://twitter.com/jbrancha/status/1213162124777869319?s=20).
|
||||||
Reference in New Issue
Block a user