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 |