Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to set time offset with loading file + enhance spec for Local time #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ fit_file = Fit.load_file(ARGV[0])
records = fit_file.records.select{ |r| r.content.record_type != :definition }.map{ |r| r.content }
```

If some times returned for your file are around 1989 or 1990, then you need to get the offset and pass it to the `load_file` method
This requires loading the file 2 times (1 to get the offset, and 1 to read the file with the offseted date)
```ruby
require 'fit'

fit_file = Fit.load_file(ARGV[0])

offset = nil
records = fit_file.records.select { |r| r.content.record_type == :activity }.map(&:content)
rec = records[0]
local_ts = rec.send(:raw_timestamp).to_i
offset = rec.send(:raw_local_timestamp).to_i if local_ts < 268435456


fit_file = Fit.load_file(ARGV[0], offset)

records = fit_file.records.select{ |r| r.content.record_type != :definition }.map{ |r| r.content }

```

## Supported files

This library has been tested with files coming from the following devices:
Expand Down
3 changes: 2 additions & 1 deletion lib/fit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
require 'fit/version'

module Fit
def self.load_file(path)
def self.load_file(path, time_offset = nil)
Fit::File::Types.time_offset = time_offset
File.read ::File.open(path)
end
end
13 changes: 12 additions & 1 deletion lib/fit/file/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Fit
class File
module Types
@@types = {}
@@time_offset = nil

class << self
def add_type(name, type, option = {})
Expand All @@ -16,10 +17,20 @@ def get_type_definition(name)
nil
end

def time_offset=(time_offset)
@@time_offset = time_offset
end

def date_time_value(time, values, parameters)
val = values.invert
if time < val['min']
time.to_s
if @@time_offset
offsetted_time = @@time_offset + time
res = parameters[:utc] ? Time.utc(1989, 12, 31) + offsetted_time : Time.local(1989, 12, 31) + offsetted_time
res.to_s
else
time.to_s
end
else
res = parameters[:utc] ? Time.utc(1989, 12, 31) + time : Time.local(1989, 12, 31) + time
res.to_s
Expand Down
9 changes: 7 additions & 2 deletions spec/fit/file/types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
expect(described_class.date_time_value(9999, { 268435456 => 'min' }, { utc: true })).to eql '9999'
expect(described_class.date_time_value(9999, { 268435456 => 'min' }, { utc: false })).to eql '9999'
end

it 'returns offsetted system time when offset is defined' do
described_class.time_offset = 788392680
expect(described_class.date_time_value(738748, { 268435456 => 'min' },
{ utc: true })).to eql '2015-01-02 11:10:28 UTC'
end
end

context 'when value is above min' do
Expand All @@ -49,9 +55,8 @@

context 'with local mode' do
it 'returns exact date in locale time zone' do
# TODO: manage answer based on current system local
expect(described_class.date_time_value(790509304, { 268435456 => 'min' },
{ utc: false })).not_to match(/UTC$/)
{ utc: false })).to match(/^2015-01-18 09:55:04 [+-]\d{4}$/)
end
end
end
Expand Down