> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/get-the-current-time.md).

# Get The Current Time

Working with time and time zones in server development can get complicated. Time-sensitive code that worked locally can unexpected fail when deployed to a server in a different time zone. Or users can end up seeing timestamps that look a few hours off.

To avoid this kinds of mistakes in Rails development, we should avoid using `Time.now` and instead use `Time.current`.

> Rails saves timestamps to the database in UTC time zone. We should always use Time.current for any database queries, so that Rails will translate and compare the correct times.

```ruby
> Time.zone
=> #<ActiveSupport::TimeZone:0x00007fccf6b0a548
 @name="UTC",
 @tzinfo=#<TZInfo::DataTimezone: Etc/UTC>,
 @utc_offset=nil>
> Time.now
=> 2021-01-28 19:22:42.312577 -0600
> Time.current
=> Fri, 29 Jan 2021 01:22:45.926181000 UTC +00:00
> Time.zone = 'Eastern Time (US & Canada)'
=> "Eastern Time (US & Canada)"
> Time.now
=> 2021-01-28 19:23:28.255106 -0600
> Time.current
=> Thu, 28 Jan 2021 20:23:32.150545000 EST -05:00
```

My server's default time zone is UTC. `Time.now` gives me my computer's system time (Central Time). `Time.current` gives me the time in UTC. If I then change the server's time zone to Eastern Time, `Time.now` still offers up my system time whereas `Time.current` produces the current time in Easter Time.

[source](https://thoughtbot.com/blog/its-about-time-zones)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/get-the-current-time.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
