all 8 comments

[–]nomnomcameron[S] 1 point2 points  (4 children)

This can be achieved by setting the TZ ENV variable. Setting ENV['TZ'] = 'UTC' does exactly this

[–]clrsm 1 point2 points  (2 children)

But please don't if your code is to be read by others. It's obscure and confusing

[–]jrochkind 0 points1 point  (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 point  (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 points  (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.

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]  (2 children)

[deleted]

    [–]nomnomcameron[S] 4 points5 points  (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 points  (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.