Class: Fit4Ruby::FileNameCoder
- Inherits:
-
Object
- Object
- Fit4Ruby::FileNameCoder
- Defined in:
- lib/fit4ruby/FileNameCoder.rb
Overview
This class provides encoder and decoder for the FIT file names typically used for activies and monitor data files.
Constant Summary collapse
- CodeBook =
0.upto(9).map{ |i| (?0.ord + i).chr} + 0.upto(25).map{ |i|(?A.ord + i).chr}
Class Method Summary collapse
-
.decode(file_name) ⇒ Time
Convert a FIT file name into the corresponding Time value.
-
.encode(time) ⇒ String
Convert a Time to a corresponding FIT file name.
Class Method Details
.decode(file_name) ⇒ Time
Convert a FIT file name into the corresponding Time value.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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) ⇒ String
Convert a Time to a corresponding FIT file name.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 |