-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating a MediafluxTime class to handle timezone inconsistencies (#730)
* Creating a mediaflux class to settle timezones Converts all times from mediaflux to UTC based on the gmt offset before converting to an america/new_york timezone and iso format closes #568 * removing unnecessary migration * Adding tests for mediaflux time conversion * refactoring comments to be compatable with yardocs
- Loading branch information
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
class MediafluxTime | ||
# Converts the givin time snippet into local princeton time while accounting for potential time zone of the asset. | ||
# | ||
# @param xml_snip [:nokigiri], response_xml from a mediaflux request | ||
# @return [String] returns a iso8601 Princeton time value. | ||
def convert(xml_snip:) | ||
xml = xml_snip | ||
time = xml.text.to_time | ||
gmt = xml.xpath("./@gmt-offset").text.to_f | ||
|
||
if gmt.zero? | ||
return time.in_time_zone("America/New_York").iso8601 | ||
elsif gmt.positive? | ||
time -= gmt.hours | ||
else | ||
time += gmt.hours | ||
end | ||
|
||
princeton_time = time.in_time_zone("America/New_York").iso8601 | ||
princeton_time | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe MediafluxTime, type: :model do | ||
let(:project) { FactoryBot.build :project_with_doi } | ||
let(:current_user) { FactoryBot.create(:user, uid: "jh1234") } | ||
subject(:instance) { described_class.new } | ||
|
||
describe "#convert", connect_to_mediaflux: true do | ||
it "converts mediaflux time objects to utc before converting to america/new_york" do | ||
project = FactoryBot.create(:project_with_doi) | ||
ProjectMediaflux.create!(project: project, session_id: current_user.mediaflux_session) | ||
metadata = Mediaflux::Http::AssetMetadataRequest.new( | ||
session_token: current_user.mediaflux_session, | ||
id: project.mediaflux_id | ||
).metadata | ||
|
||
|
||
xml_snip = metadata[:ctime] | ||
initial_tz = xml_snip.xpath("./@tz").text | ||
expect(initial_tz).to eq "Etc/UTC" | ||
|
||
final_tz = instance.convert(xml_snip:) | ||
expect(final_tz).to include("-04:00") or include("-05:00") #America/New_York changes based on daylights savings time | ||
end | ||
end | ||
end |