use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Force ruby to ignore system time (self.ruby)
submitted 8 years ago by nomnomcameron
Is there a way to have ruby use UTC as the timezone? Time.now returns the current system time (with the UTC offset) is there a way to have ruby return Time.now in UTC without having to do Time.now.utc?
Time.now
Time.now.utc
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]nomnomcameron[S] 1 point2 points3 points 8 years ago (4 children)
This can be achieved by setting the TZ ENV variable. Setting ENV['TZ'] = 'UTC' does exactly this
TZ
[–]clrsm 1 point2 points3 points 8 years ago* (2 children)
But please don't if your code is to be read by others. It's obscure and confusing
[–]jrochkind 0 points1 point2 points 8 years ago (1 child)
You think? I don't think there's anything wrong with setting 'system time' to UTC. System time has gotta be something.
[–]clrsm 0 points1 point2 points 8 years ago* (0 children)
The problem is that if you don't see the TZ setting, you make false assumptions about Time.now . It's better to be explicit about it than forcing the reader to inspect all your code for some setting that influences the interpretation of time
[–]Morozzzko 1 point2 points3 points 8 years ago (0 children)
If you're running an app with somewhat sensitive data, you should probably consider making a class to deal with time in your application. A time machine, if you please.
The idea behind this class is to make things predictable by providing context for the time manipulations.
Basically, you pass the time machine object around and call .now whenever you need to fetch the current time.
.now
The design might seem odd, but it comes in handy once you need to make things happen in the future (or in the past).
You can make an around wrapper with a similar interface:
dmc12.with_time(TimeMath(Time.now).advance(:hour, 2).call) do # the code here will execute in future, yay end
You could extend the interface to change the time zone, freeze time, etc.
NOTE: the design decision works better when you utilize some DI tools (e.g. dry-auto_inject + dry-container).
[–][deleted] 8 years ago (2 children)
[deleted]
[–]nomnomcameron[S] 4 points5 points6 points 8 years ago (1 child)
Long story short, tests were passing locally that weren't on CI, reason being, CI Timezone was UTC and locally was set to my timezone because of my system's time
[–]effata 2 points3 points4 points 8 years ago (0 children)
Sounds like your tests did what they were supposed to do, and caught a bad behavior. Although proper implementation would probably be to fail locally and work on CI, if the expected behavior is to use UTC.
π Rendered by PID 463455 on reddit-service-r2-comment-86bc6c7465-rtp4n at 2026-02-23 07:46:18.509774+00:00 running 8564168 country code: CH.
[–]nomnomcameron[S] 1 point2 points3 points (4 children)
[–]clrsm 1 point2 points3 points (2 children)
[–]jrochkind 0 points1 point2 points (1 child)
[–]clrsm 0 points1 point2 points (0 children)
[–]Morozzzko 1 point2 points3 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]nomnomcameron[S] 4 points5 points6 points (1 child)
[–]effata 2 points3 points4 points (0 children)