Archer
Archer

Reputation: 1134

Rails Timezone disappears

I need to check if the time right now is between a timerange.

But I can't compare the time, beacuse the timezone disapears suddenly.

 now = Time.now.in_time_zone('Berlin')
 => Wed, 11 Jan 2017 14:58:34 CET +01:00

 time_from = send("#{day}_available_from".to_sym).in_time_zone('Berlin')
 => Sat, 01 Jan 2000 08:00:00 CET +01:00
 time_till = send("#{day}_available_till".to_sym).in_time_zone('Berlin')
 => Sat, 01 Jan 2000 14:50:00 CET +01:00

  date_from = DateTime.parse("#{Time.now.in_time_zone('Berlin').strftime('%d.%m.%Y')} #{time_from.strftime('%H:%M')}")
  => Wed, 11 Jan 2017 08:00:00 +0000
 date_till = DateTime.parse("#{Time.now.in_time_zone('Berlin').strftime('%d.%m.%Y')} #{time_till.strftime('%H:%M')}")
 => Wed, 11 Jan 2017 14:50:00 +0000

  now.between?(date_from, date_till)
  => true

But this is wrong. 14:58:34 isn't between 08:00:00 -> 14:50:00

Upvotes: 2

Views: 21

Answers (1)

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

The timezone dissapears because you do not provide it to DateTime.parse.

Update time_from.strftime('%H:%M') to time_from.strftime('%H:%M %z').

Upvotes: 1

Related Questions