R
R
rd-r2020-10-16 11:06:00
Ruby on Rails
rd-r, 2020-10-16 11:06:00

Working with timezones in Ruby on Rails?

Thanks in advance to everyone who answers.
I have the following situation. The client side sends the EXIF ​​data of the photo to the server. Moreover, any date ( originalDateTime, dateTime or digitizedDateTime) is sent in the rfc3339 format with a zeroed offset (for example: 2017-20-10T12:50:30+00:00, why exactly and not otherwise, another question. I can’t change it now ). On the server side, the following happens. We calculate the timeZone based on the coordinates, create a date-time object with the desired offset, then bring this date to the server's default time zone (Central Daylight Time) and then save it to the database. I cannot change now the format in which we receive the date and in which we save it in the database. Further from the database, this date is taken out, converted again into the rfc3339 format, and sent to the client side.
I think there is definitely some kind of error here, with the conversion of the date and time, which I can't see right now.
Question: Is there anything valid in all these actions?

if picture[:picture_timestamp]

                if picture[:latitude] && picture[:longitude]

                  parsed_date_time = DateTime.rfc3339(picture[:picture_timestamp]).to_s(:db)
                  timezone = Timezone.lookup(picture[:latitude], picture[:longitude])
                  date_obj = ActiveSupport::TimeZone[timezone.name].parse(parsed_date_time)

                  date_with_offset = DateTime.new(date_obj.year, date_obj.month, date_obj.day, date_obj.hour, date_obj.min, date_obj.sec, date_obj.zone).to_s

                  @dailypost.picture_timestamp = Time.zone.parse(date_with_offset)
                end
 end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2020-10-17
@2ord

Moreover, any date ( originalDateTime, dateTime or digitizedDateTime) is sent in the rfc3339 format with a zeroed offset
Because UTC .
The timezone offset can be computed from location using the timezone gem.
Accordingly, in the database, you need to store the time for each image in the datetime column, and not varchar , in order to avoid unnecessary parsing and formatting of dates.
If the client needs to give time in rfc3339 format, then format it directly when generating an HTTP response.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question