class Fit4Ruby::FileNameCoder

This class provides encoder and decoder for the FIT file names typically used for activies and monitor data files.

Constants

CodeBook

Public Class Methods

decode(file_name) click to toggle source

Convert a FIT file name into the corresponding Time value. @param file_name [String] FIT file name. This can be a full path name

but must end with a '.FIT' extension.

@return [Time] corresponding Time value

# File lib/fit4ruby/FileNameCoder.rb, line 44
def FileNameCoder::decode(file_name)
  base = File.basename(file_name.upcase)
  unless /\A[0-9A-Z]{4}[0-9]{4}\.FIT\z/ =~ base
    raise ArgumentError, "#{file_name} is not a valid FIT file name"
  end

  year = 2010 + CodeBook.index(base[0])
  month = CodeBook.index(base[1])
  day = CodeBook.index(base[2])
  hour = CodeBook.index(base[3])
  minutes = base[4,2].to_i
  seconds = base[6,2].to_i
  if month == 0 || month > 12 || day == 0 || day > 31 ||
     hour >= 24 || minutes >= 60 || seconds >= 60
    raise ArgumentError, "#{file_name} is not a valid FIT file name"
  end

  Time.new(year, month, day, hour, minutes, seconds, "+00:00")
end
encode(time) click to toggle source

Convert a Time to a corresponding FIT file name. @param [Time] time stamp @return [String] FIT file name with extension '.FIT'

# File lib/fit4ruby/FileNameCoder.rb, line 25
def FileNameCoder::encode(time)
  utc = time.utc
  if (year = utc.year) < 2010 || year > 2033
    raise ArgumentError, "Year must be between 2010 and 2033"
  end
  year = CodeBook[year - 2010]
  month = CodeBook[utc.month]
  day = CodeBook[utc.day]
  hour = CodeBook[utc.hour]
  minutes = "%02d" % utc.min
  seconds = "%02d" % utc.sec

  year + month + day + hour + minutes + seconds + '.FIT'
end