Module: Fit4Ruby::Converters

Included in:
FitDataRecord, GlobalFitMessage::Field
Defined in:
lib/fit4ruby/Converters.rb

Constant Summary collapse

FACTORS =
{
  'm' => { 'km' => 0.001, 'in' => 39.3701, 'ft' => 3.28084,
           'mi' => 0.000621371 },
  'mm' => { 'cm' => 0.1, 'in' => 0.0393701 },
  'm/s' => { 'km/h' => 3.6, 'mph' => 2.23694 },
  'min/km' => { 'min/mi' => 1.60934 },
  'kg' => { 'lbs' => 2.20462262 },
  'C' => { 'F' => 9.0 / 5.0 }
}.freeze
OFFSETS =
{
  'C' => { 'F' => 32 }
}.freeze

Instance Method Summary collapse

Instance Method Details

#conversion_factor(from_unit, to_unit) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fit4ruby/Converters.rb', line 28

def conversion_factor(from_unit, to_unit)
  return 1.0 if from_unit == to_unit

  unless FACTORS.include?(from_unit)
    Log.fatal 'No conversion factors defined for unit ' \
              "'#{from_unit}' to '#{to_unit}'"
  end

  factor = FACTORS[from_unit][to_unit]
  if factor.nil?
    Log.fatal "No conversion factor from '#{from_unit}' to '#{to_unit}' " \
              'defined.'
  end

  factor
end

#conversion_offset(from_unit, to_unit) ⇒ Object



45
46
47
48
49
50
# File 'lib/fit4ruby/Converters.rb', line 45

def conversion_offset(from_unit, to_unit)
  return 0.0 if from_unit == to_unit || !OFFSETS.include?(from_unit) ||
                !OFFSETS[from_unit].include?(to_unit)

  OFFSETS[from_unit][to_unit]
end

#fit_time_to_time(ft) ⇒ Object



97
98
99
# File 'lib/fit4ruby/Converters.rb', line 97

def fit_time_to_time(ft)
  Time.parse('1989-12-31T00:00:00+00:00') + ft.to_i
end

#secsToDHMS(secs) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/fit4ruby/Converters.rb', line 82

def secsToDHMS(secs)
  secs = secs.to_i
  s = secs % 60
  mins = secs / 60
  m = mins % 60
  hours = mins / 60
  h = hours % 24
  d = hours / 24
  "#{d} days #{h}:#{'%02d' % m}:#{'%02d' % s}"
end

#secsToHM(secs) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/fit4ruby/Converters.rb', line 64

def secsToHM(secs)
  secs = secs.to_i
  s = secs % 60
  mins = secs / 60
  m = mins % 60
  h = mins / 60
  "#{h}:#{'%02d' % m}"
end

#secsToHMS(secs) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/fit4ruby/Converters.rb', line 73

def secsToHMS(secs)
  secs = secs.to_i
  s = secs % 60
  mins = secs / 60
  m = mins % 60
  h = mins / 60
  "#{h}:#{'%02d' % m}:#{'%02d' % s}"
end

#speedToPace(speed, distance = 1000.0) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fit4ruby/Converters.rb', line 52

def speedToPace(speed, distance = 1000.0)
  if speed && speed > 0.01
    # We only show 2 fractional digits, so make sure we round accordingly
    # before we crack it up.
    pace = (distance.to_f / (speed * 60.0)).round(2)
    int, dec = pace.divmod 1
    "#{int}:#{'%02d' % (dec * 60)}"
  else
    "-:--"
  end
end

#time_to_fit_time(t) ⇒ Object



93
94
95
# File 'lib/fit4ruby/Converters.rb', line 93

def time_to_fit_time(t)
  (t - Time.parse('1989-12-31T00:00:00+00:00')).to_i
end