Skip to content

Commit

Permalink
Creating a MediafluxTime class to handle timezone inconsistencies (#730)
Browse files Browse the repository at this point in the history
* 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
JaymeeH authored May 22, 2024
1 parent 12f5c72 commit 99530c1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/mediaflux/http/asset_metadata_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def metadata
metadata[:total_file_count] = asset.xpath("./collection/accumulator/value/non-collections").text
metadata[:size] = asset.xpath("./collection/accumulator/value/total/@h").text
metadata[:quota_allocation] = asset.xpath("./collection/quota/allocation/@h").text
metadata[:ctime] = asset.xpath("./ctime")

end

Expand Down
23 changes: 23 additions & 0 deletions app/models/mediaflux_time.rb
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
28 changes: 28 additions & 0 deletions spec/models/mediaflux_time_spec.rb
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

0 comments on commit 99530c1

Please sign in to comment.