Class: FunWith::Files::Utils::Timestamp
- Inherits:
-
Object
- Object
- FunWith::Files::Utils::Timestamp
- Defined in:
- lib/fun_with/files/utils/timestamp.rb
Class Method Summary collapse
-
.format(key) ⇒ Object
Currently exactly one format is supported.
- .timestamp(basename, format: :default, splitter: ".", time: Time.now) ⇒ Object
Class Method Details
.format(key) ⇒ Object
Currently exactly one format is supported. Laaaaame!
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fun_with/files/utils/timestamp.rb', line 7 def self.format( key ) @formats ||= { :default => TimestampFormat.new.recognizer( /^\d{17}$/ ).strftime("%Y%m%d%H%M%S%L"), :ymd => TimestampFormat.new.recognizer( /^\d{4}-\d{2}-\d{2}$/ ).strftime("%Y-%m-%d"), :ym => TimestampFormat.new.recognizer( /^\d{4}-\d{2}$/ ).strftime("%Y-%m"), :y => TimestampFormat.new.recognizer( /^\d{4}$/ ).strftime("%Y"), # UNIX timestamp :s => TimestampFormat.new.recognizer( /^\d{10}$/ ).strftime("%s") } if @formats.has_key?(key) @formats[key] else raise Errors::TimestampFormatUnrecognized.new( "Unrecognized timestamp format (#{key.inspect}). Choose from #{@formats.keys.inspect}" ) end end |
.timestamp(basename, format: :default, splitter: ".", time: Time.now) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/fun_with/files/utils/timestamp.rb', line 25 def self.( basename, format: :default, splitter: ".", time: Time.now ) filename_chunks = basename.to_s.split( splitter ) format = format.is_a?( TimestampFormat ) ? format : self.format( format ) = format.format_time( time ) = filename_chunks.map.each_with_index{ |str,i| format.matches?( str ) ? i : nil }.compact.last if filename_chunks[] = elsif filename_chunks.length == 1 filename_chunks << else filename_chunks.insert( -2, ) end filename_chunks.join( splitter ) end |