Comments (4)
I don't think the linked test matches/describes quite this behaviour -- that is showing a conversion between stored and reported time zones, which shifts the result back a few hours (ending up on the 31st). Here the actual time component is correct, but a full day back.
🤔
Oh, I think I understand.
The test behaviour feels intuitively correct to me: the stored value is midnight, and then when it's loaded in an offset timezone, the value is "N hours before midnight".
But here, the application is consistently working in Brasilia TZ, so it intends to store "22:00". Because the DB is using UTC, though, that ends up stored as midnight on the 2nd... truncated to 00:00 by the time field. And then when loaded, the DB said "midnight on the 1st, UTC"... which after TZ correction, is 2 hours before that, on the 31st.
I think this a fundamental problem with having the application and database timezone differing while storing unanchored time values: the storable range is represented by Jan 1 in the DB zone (because that's what the DB can represent), and that's what we're trying to translate for the application. If you look at the raw values stored in the DB at the midpoint of the test, I think that will show why end < start
is actually quite defensible, and the opposite would be more surprising -- the problem happens back during the save, where time
's storage limitations truncate away the future-ness.
I don't remember how timetz
fits in here, but it might help?
from rails.
@PedroAugustoRamalhoDuarte if your goal is to compare Time of Day without a specific date, one option is to use the Tod gem, particularly the ActiveRecord integration. Please ignore this if your goal is to compare on specific dates.
Time of Day comparison details
Expectation
We want to work with a time of day, no date involved. This comparison should be true: end_time 22:00
> start_time 12:00
Problem
The Ruby Time and ActiveSupport::TimeWithZone include date. They represent a specific point in time, not the concept of Time of Day.
Time.new => 2024-04-29 17:35:36.866882654 +0000
As @matthewd pointed out there is also conversion into/out of Postgres type which breaks the comparison in certain times/timezones as the UTC offset gets applied and then stored. Even if you set up the column to persist as a Postgres time
type (time of day-no date) instead of timestamp
(both date and time), you may run into this as it is cast back into a Time
or TimeWithZone
object and a date is applied.
Approach I've used
- Using the ToD
:time_only
ActiveRecord attribute for Time of Day representation in application logic - In Postgres, using the
time
column type. - Timezone config for both Rails and Postgres set to UTC and performing changes into desired local timezone before read/write/comparison using
.in_time_zone(location_timezone)
.
from rails.
I tried to help with this on discord... we couldn't get the time_zone config to stick. ENV["TZ"] and the like didn't help either. Is there an example of a single file bug repro that includes tz changes?
from rails.
Thanks for the fast response!!
Thanks, @mkbehbehani, for the solution's approach. I am using the third approach right now, and it's working nicely. And thanks for express my problem better.
I think we can improve the rails default conversion for time field.
When we work with time, even though we have to deal with the entire date, we only want to deal with time. So I believe that we can work on the conventions so that even with timezones configured in the application, the dates in the time fields always occur on January 1st to have a better integration with validations and calculations.
I understand is not a big deal for the framework, is a very particularly use case, but if you guys agreed to me, I can make a PR
from rails.
Related Issues (20)
- trix with rails 7.0 giving TypeError HOT 2
- in guides.rubyonrails documentation of threads: missing info about db access HOT 1
- HasMany/HasOne associations: combining `:through` and a scope including a `.merge` leads to missing table HOT 1
- [Question] Why doesn't set_pk_sequence! support the parameter for is_called? HOT 5
- Rails 6.0.6.1 transaction rollback doesn't reload object HOT 1
- Error `undefined method `start_with?' for nil` when logging
- new_framework_defaults initializer for Rails 7.1 not setting ActiveRecord.run_after_transaction_callbacks_in_order_defined
- [Feature Request][CSP] Support for multiple Content Security Policies HOT 1
- ActionCable: Invalid channel causes repeated subscription attempts
- Code generation memory bloat post alias fix HOT 8
- Rails throws an error when sending invalid UTF-8 query param HOT 1
- Lazy default_scope evaluation in joined models loses original context
- Some Rails integration tests break with rack 3.1 HOT 4
- ActionText Editor Previews uploaded pre 7.1.3.4 are broken in 7.1.3.4 after a resave HOT 7
- Inversion of associations with :through don't work until it's saved
- Incorrect `has_many through` load with custom association name and custom keys type
- Demo video on home page doesn't work as demo'd HOT 1
- Infinite loop passing form_builder in partial as local after update to 7.1.3.4 (was 7.0.8.4) HOT 2
- [ActiveRecord] alias_attribute does not work on methods defined in the superclass HOT 3
- Actionview: ArgumentError "Invalid formats" exception with empty invalid_values when additional format gets added after update to Rails 7.1.3.4 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rails.