-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Time instead of DateTime #1952
Comments
Makes sense to me. We also need an entry in the Ruby style guide for this, as it's not something Rails-specific. |
Hi, I saw this on Ruby issues. If there is no hurry I'd like to work on this task. It will help me to improve my knowledge of Ruby, Rubocop, testing and Git. I'll read the contributing info, sample cops source etc., and see how far I can get. Where can I turn to if I have Rubocop specific questions? Best, |
Post them right here, in this thread? |
You're right :-) |
Hi, Thank you! |
Personally I usually run it as |
Thanks! I think now my first task is to research what I should put into the Ruby Style Guide about choosing between |
@studiolo: Are you still working on this? Mind if I pick it up? |
@Drenmi: I'm afraid I have let this slide. I'd be glad if you picked it up :-) |
I don't understand this cop. The original problem from gist: >> shakespeare = DateTime.iso8601('1616-04-23', Date::ENGLAND)
=> Tue, 23 Apr 1616 00:00:00 +0000
>> cervantes = DateTime.iso8601('1616-04-23', Date::ITALY)
=> Sat, 23 Apr 1616 00:00:00 +0000 (incorrect code/output, but OK) The warning from cop:
Okay, let's use pry: main > DateTime.iso8601('1616-04-23', Date::ENGLAND).strftime('%c')
=> "Tue Apr 23 00:00:00 1616"
pry: main > DateTime.iso8601('1616-04-23', Date::ITALY).strftime('%c')
=> "Sat Apr 23 00:00:00 1616"
pry: main > Date.iso8601('1616-04-23', Date::ENGLAND).strftime('%c')
=> "Tue Apr 23 00:00:00 1616"
pry: main > Date.iso8601('1616-04-23', Date::ITALY).strftime('%c')
=> "Sat Apr 23 00:00:00 1616" Oh, the same result… So…? |
If that's happening that's a bug. It should not report an offense if a historic date is used. When making the cop I tested against the examples in the ruby style guide which all worked. I'll dig into this. |
I don't understand this cop either. I'm using irb(main):024:0> DateTime.now.to_s
=> "2018-01-25T14:42:28+01:00"
irb(main):025:0> Time.now.to_s
=> "2018-01-25 14:42:30 +0100" As you can see, |
Also disabling this. |
What's point of this issue if I think it should be reverted. |
Also confused about this cop. There is no explanation here about why Time is supposedly better than DateTime. |
Ruby docs on DateTime provide a good explanation. |
But no about difference between |
I'm not sure about this
|
@Mifrill, emm: Time.parse("Ends from 28 Jun 2018 12:00 BST").utc.to_s
NoMethodError: undefined method `parse' for Time:Class https://repl.it/repls/SpeedySeagreenProjector
|
Is there any case where using If so, the cops message could be clarified to prefer @rmoriz wrote:
How about |
I got it! (You need to https://repl.it/@AlexWayfer/AquaAmbitiousProspect So, DateTime.parse("28 Jun 2018 12:00 BST").to_time.utc.to_s # => 2018-06-28 12:00:00 +0100
# or 2018-06-28 11:00:00 UTC, and this is correct
Time.parse("28 Jun 2018 12:00 BST").utc.to_s # => 2018-06-28 12:00:00 +0000
# or 2018-06-28 12:00:00 UTC, and this is not correct I think this cop should be totally removed. |
From the Ruby docs regarding
A modified example using a time zone abbreviation listed in DateTime.parse("28 Jun 2018 12:00 EST").to_time.utc.to_s
=> "2018-06-28 17:00:00 UTC"
Time.parse("28 Jun 2018 12:00 EST").utc.to_s
=> "2018-06-28 17:00:00 UTC" So maybe this would be better fixed upstream. Same thing with the different |
Or maybe RuboCop should not enforce imperfect class instead of more complete.
I don't understand the question. |
I meant that if you feel
You don't have to account for timezones? Because that's exactly the argument why DateTime is often not the right choice: require "time"
time = Time.parse "2018-03-11 1:00:00" # OS set to Eastern Standard Time
=> 2018-03-11 01:00:00 -0500
time.dst?
=> false
summer_time = time + 60*60 # advanced one hour
=> 2018-03-11 03:00:00 -0400
summer_time.dst?
=> true
datetime = DateTime.parse "2018-03-11 1:00:00 -05:00"
=> #<DateTime: 2018-03-11T01:00:00-05:00 ((2458189j,21600s,0n),-18000s,2299161j)>
datetime.dst? # this doesn't work
NoMethodError: undefined method `dst?' for #<DateTime:0x00007fbc2010c258>
summer_datetime = datetime + (1.0/24) # advanced one hour
=> #<DateTime: 2018-03-11T02:00:00-05:00 ((2458189j,25200s,0n),-18000s,2299161j)>
# UTC offset wise this is Jamaica instead of Montreal
summer_time.to_datetime # same time, 1 hour different UTC offsets
=> #<DateTime: 2018-03-11T03:00:00-04:00 ((2458189j,25200s,0n),-14400s,2299161j)> In #6189 you asked me for an example of a bug if you mix the two classes - memory is a bit hazy since it's a couple of years ago but the problem was basically "off by 1 hour" around DST transitions because the frontend didn't show timezones or UTC offsets. Something like: summer_time.strftime "%Y-%m-%d %H:%M:%S"
=> "2018-03-11 03:00:00"
summer_datetime.strftime "%Y-%m-%d %H:%M:%S"
=> "2018-03-11 02:00:00" The other argument
Both Time and DateTime pick the first option (Eastern Standard Time) when asked to parse The solution to unambiguously refer to a timezone is to refer to the UTC offset, which means you'll never have problems with parsing time, date and timezone from string either: string = Time.now.iso8601
=> "2018-08-19T23:31:21-04:00"
time = Time.iso8601 string
=> 2018-08-19 23:31:21 -0400
time.dst?
=> true Imperfect as you might find |
I have to account. This makes more sense, thanks. But do you know about "wall time"? Like
Danger: you're loosing timezone info and summer times: > Time.parse('2018-03-11 03:00:00 -0400').dst?
=> false
> Time.parse('2018-08-19 23:31:21 -0400').dst?
=> false Oh, stop, WTF? > time = Time.parse('2018-03-11 01:00:00 -0500')
=> 2018-03-11 01:00:00 -0500
> time.dst?
=> false
> summer_time = time + 3600
=> 2018-03-11 02:00:00 -0500
> summer_time.dst?
=> false The same with |
I think it's best to disable this cop by default, and not completely delete it |
What environment are you seeing this on? These examples return
Better here is not to use |
What? First you write " |
Sorry, I'm not sure what's going on with your environment here.
I assumed with 'wall time' you wanted to measure elapsed time. You could use |
So, why we should enforce one class instead of another, if you even can't describe different behavior in different computers? Please, everybody, have strong (simple) arguments before enforcing something.
Or future event. "Plan a review of work day at 19:00, independently of your timezone". |
This Stack Overflow answer goes into detail where the data comes from. Why it does not work as expected on your environment is a separate issue outside of Rubocop. The argument stays the same:
So it's a timestamp (based on the Postgres example you gave earlier) in which case I do it at that time (in the timezone)? Or it's a recurring event that comes with it's own set of challenges around daylight savings time? |
It's about
Your example with I see |
Also, FYI, ISO 8601 doesn't contain time zones. It contains offset, it's a different thing. Multiple timezones can have the same offset at the one point of time. Do, you're just loosing DST accounting and other things.
|
1. It doesn't resolve original described problem: rubocop#1952 (comment) 2. `Time#to_s` is not ISO6801 compliant by default: rubocop#1952 (comment) 3. `Time.parse` works wrong with timezones: rubocop#1952 (comment)
A recent gist (https://gist.github.com/pixeltrix/e2298822dd89d854444b, via This week in Rails) explains why you can almost always use
Time
orDate
instead ofDateTime
.Without having dug very deep, it looks like you can use one of the simpler classes unless a
DateTime
method is being called with astart
argument such asDate::ENGLAND
,Date::GREGORIAN
,Date::ITALY
orDate::JULIAN
.Should we have a (
rails orstyle) cop that check for uses ofDateTime
where a simpleDate
orTime
class would suffice?The text was updated successfully, but these errors were encountered: