floraison / fugit

time tools (cron, parsing, durations, ...) for Ruby, rufus-scheduler, and flor
MIT License
353 stars 29 forks source link

Strange behaviour with DST? #53

Closed andyundso closed 3 years ago

andyundso commented 3 years ago

Issue description

First of all, I'm based in Switzerland. Our clock will change next week to summer time. We use Fugit in a project to allow users schedule background jobs. We have a test suite that has a test where a job should run each week with the instruction every monday at midnight. We compare the result of Fugit with an expected value to make sure our scheduler works.

Now, instructing Fugit to give the next date for said schedule strangely gives a date in two weeks instead of next week. Other hours seem to work fine:

irb(main):003:0> Fugit.parse("every monday at 3am").next_time.to_s
=> "2021-03-29 03:00:00 +0200"
irb(main):004:0> Fugit.parse("every monday at 2am").next_time.to_s
=> "2021-03-29 02:00:00 +0200"
irb(main):005:0> Fugit.parse("every monday at 1am").next_time.to_s
=> "2021-03-29 01:00:00 +0200"
irb(main):006:0> Fugit.parse("every monday at midnight").next_time.to_s
=> "2021-04-05 00:00:00 +0200"

I'm not sure if it is related to DST and if this is supposed to happen (I don't know cron too well).

How to reproduce

See above.

Error and error backtrace (if any)

Not needed, as no error will be printed.

Expected behaviour

next_time should print Mon, 29 Mar 2021 00:00:00 CEST +02:00.

Context

Please replace the content of this section with the output of the following commands:

uname -a
bundle exec ruby -v
bundle exec ruby -e "p [ :env_tz, ENV['TZ'] ]"
bundle exec ruby -r et-orbi -e "EtOrbi._make_info"
bundle exec ruby -r fugit -e "p Fugit::VERSION"

(It's supposed to look like

Linux apf-work-home 5.4.0-67-generic #75-Ubuntu SMP Fri Feb 19 18:03:38 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]
[:env_tz, nil]
(secs:1616405592.3763082,utc~:"2021-03-22 09:33:12.3763082027435303",ltz~:"CET")
(etz:nil,tnz:"CET",tziv:"1.2.9",tzidv:nil,rv:"2.5.8",rp:"x86_64-linux",win:false,rorv:nil,astz:nil,eov:"1.2.4",eotnz:#<TZInfo::DataTimezone: Europe/Zurich>,eotnfz:"+0100",eotlzn:"Europe/Zurich",eotnfZ:"CET",debian:"Europe/Zurich",centos:nil,osx:"Europe/Zurich")
"1.4.2"

)

jmettraux commented 3 years ago

Hello,

thanks for the report. I am working on it.

As a workaround, you could use "every tuesday at 00:00" as seen in https://github.com/floraison/fugit/blob/49b66702bc309bcffd8e5d7495dd45f080bf134c/spec/cron_spec.rb#L265-L288

I think I am guilty of interpreting "every monday at midnight" as "every monday at 00:00". I need to straighten that.

Now, I still have to double-check, the entrance into DST is done at 02:00, it should make a 00:00 schedule skip.

transition

jmettraux commented 3 years ago

@andyundso

Hello, could you please tell me if the above fix suits you? If yes, I will release 1.4.3.

require 'fugit'

ENV['TZ'] = 'Europe/Zurich'

t0 = Time.parse('2021-03-22 13:00')

p Fugit.parse('every monday at midnight').next_time(t0).to_s
  # ==>     "2021-03-29 00:00:00 +0200"
  # and not "2021-04-05 00:00:00 +0200"

Best regards.

andyundso commented 3 years ago

Good morning @jmettraux

Many thanks for the quick reaction. I tested the master branch with our test and now it works. I think you can release 1.4.3.

Best regards, Andy

jmettraux commented 3 years ago

It's released https://rubygems.org/gems/fugit/versions/1.4.3

Thanks again!

jmettraux commented 3 years ago

@andyundso

Hello,

for the sake of completeness, I added a spec for when leaving the DST (October) and it failed. I have fixed the problem and released 1.4.4. https://github.com/floraison/fugit/commit/550e571d0af076d918459c432bbd850245cf0702

Please upgrade before October.

Best regards.