Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 945 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time.use_zone is not working as expected

#1
So right now it is 2:54 PM PST in San Francisco. For some reason, this code block is not returning 12:54 PM HST in Hawaii. Am I missing something here? I would expect this code to return me the current time in Hawaii

Time.use_zone('Hawaii') do
Time.now
end
# => 2012-01-03 14:54:54 -0800
Reply

#2
Use `Time.current` if you want `now` with timezone support. `Time.now` is dangerous when working in a timezone aware application, as a rule of thumb I never use `Time.now`, only `Time.current`. Rails time helpers like `2.hours.ago` and `4.days.from_now` are based off of `Time.current` as well.

```
# Time.current will use Time.zone when needed (when Time.zone is present)
def current
::Time.zone ? ::Time.zone.now : ::Time.now
end
```

Also, this is a great article with a great cheat sheet at the bottom:

[To see links please register here]


### DOs
| | code | result |
|-------------------------------------------------------------|---------------------------------------------------------------------------|--------------------------------------|
| | `2.hours.ago` | Thu, 27 Aug 2015 14:39:36 AFT +04:30 |
| | `1.day.from_now` | Fri, 28 Aug 2015 16:39:36 AFT +04:30 |
| | `Time.zone.parse("2015-08-27T12:09:36Z")` | Thu, 27 Aug 2015 16:39:36 AFT +04:30 |
| | `Time.current` | Thu, 27 Aug 2015 16:39:36 AFT +04:30 |
| When supliyng an API | `Time.current.utc.iso8601` | 2015-08-27T12:09:36Z |
| If you can’t use Time.zone.parse | `Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z").in_time_zone` | Thu, 27 Aug 2015 16:39:36 AFT +04:30 |
| If you really can’t have a Time or DateTime for some reason | `Date.current` | Thu, 27 Aug 2015 |
| If you have a date and want to make the best out of it | `Date.current.in_time_zone` | Thu, 27 Aug 2015 00:00:00 AFT +04:30 |

### DON’Ts

| | code | result |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------|---------------------------|
| Returns system time and ignores your configured time zone. | `Time.now` | 2015-08-27 14:09:36 +0200 |
| Will assume time string given is in the system’s time zone. | `Time.parse("2015-08-27T12:09:36Z")` | 2015-08-27 12:09:36 UTC |
| Same problem as with Time.parse. | `Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z")` | 2015-08-27 12:09:36 UTC |
| This could be yesterday or tomorrow depending on the machine’s time zone, see [issue 1](

[To see links please register here]

) for more info. | `Date.today` | Thu, 27 Aug 2015 |
Reply

#3
Don't use `Time.now` this is using your local time zone instead use `Time.current`

Time.use_zone('Hawaii') do
p Time.current
end
Reply

#4
Time.now - using server time <br>
Time.zone.now - using rails application time (in config: config.time_zone) <br>
Time.use_zone - using 'your' timezone for given block <br>

<br>
This example is wrong, because Time.now get time in your server timezone and with method in_time_zone translate time into an equivalent time in Hawaii timezone. But it's no current Time in Hawaii! It's your server time with utc offset for Hawaii.

Time.use_zone('Hawaii') do
Time.now.in_time_zone
end
=> Wed, 14 Aug 2013 10:33:18 HST -10:00

Time.now.in_time_zone
=> Thu, 15 Aug 2013 00:32:30 MSK +04:00

For getting time in Hawaii timezone you must use

Time.use_zone('Hawaii') do
Time.zone.now
end
Reply

#5
This should work ok:

Time.use_zone('Hawaii') do
p Time.zone.now
end
Reply

#6
Try using `Time.now.in_time_zone` inside your block instead.

> Time.use_zone('Hawaii') do
> Time.now.in_time_zone
> end
=> Tue, 03 Jan 2012 13:07:06 HST -10:00
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through