Part of the time zone strip from Time in Ruby

How to remove part of the time zone from the time left without changing the date and time? I am currently using the strftime function. My ultimate goal is to query MySQL with local time. I must be sure that local time will not be suddenly transformed into something else.

+5
source share
2 answers

You can use time.strftime('%Y-%m-%d %H:%M:%S') as used by activesupport, for example,

 Time.now.to_s(:db) 

a source

+10
source

Are you trying to use the same literal time (i.e. 10:56 ET and 10:56 PT) regardless of time zone?

Or are you trying to ensure that queries refer to the same exact time (e.g. 10:56 ET and 7:56 PT)?

In the first case (the same literal time), simply omit %z or %z from the strftime argument.

For the latter, I would recommend using Unix timestamps with the Ruby time.to_i function and the MySQL function FROM_UNIXTIME()

0
source

Source: https://habr.com/ru/post/1207490/


All Articles