Reputation: 1467
I try to extract exif informations from an image and have now a problem with the time zone for date fields. In the image itself the date and time are stored without a time zone (e.g. 2014:01:07 20:44:32). When I run exifr I get a DateTime object back and I can print it to a string with this com-mand:
created = date.strftime("%FT%T%:z")
=> "2014-01-07T20:44:32+01:00" # on my system with UTC+1
=> "2014-01-07T20:44:32+00:00" # on TravisCI
The time zone of the output changes depending on the system settings. This is a problem as I need to get the same values for a specific image independently of the system I run the code. It would help if the time zone is always UTC.
How can I get a consistent result in UTC independent of the system settings? And how can I do this with Ruby 1.9 and 2.0? I found many solutions for Rails but non for plain Ruby. Can you help me?
Upvotes: 2
Views: 2107
Reputation: 12826
utc
method converts Time
to UTC.
created = date.utc.strftime("%FT%T%:z")
This method is available in 1.8.7, 1.9.3, 2.0.0 and 2.1.0.
If you don't want to affect the time when changing time zone you can add the difference before converting:
time = Time.now # => 2014-01-08 21:45:41 +0100
time += time.gmt_offset # => 2014-01-08 22:45:41 +0100
time.utc # => 2014-01-08 21:45:41 UTC
Upvotes: 3