class Textrepo::Timestamp
Timestamp
is generated from a Time object. It converts a time to string in the obvious format, such “20201023122400”.
Since the obvious format contains only year, month, day, hour, minute, and second, the resolution of time is a second. That is, two Time object those are different only in second will generates equal Timestamp
objects.
If a client program of Textrepo::Timestamp
wants to distinguish those Time objects, an attribute `suffix` could be used.
For example, the `suffix` will be converted into a 3 character string, such “012”, “345”, “678”, … etc. So, millisecond part of a Time object will be suitable to pass as `suffix` when creating a Timestamp
object.
Attributes
String object which is regarded as a value of Timestamp
object. The value is generated from @time and @suffix.
An integer specified in `new` method to create the Timestamp
object.
Time object which generates the Timestamp
object.
Public Class Methods
Creates a Timestamp
object from a Time object. In addition, an Integer can be passed as a suffix use.
Since Textrepo
adapts 1 second as the time resolution, the subsec part of a given time will be ignored.
# File lib/textrepo/timestamp.rb, line 51 def initialize(time, suffix = nil) raise ArgumentRangeError, suffix unless is_valid_suffix?(suffix) parts = [:year, :mon, :day, :hour, :min, :sec].map{ |s| time.send(s) } @time = Time.new(*parts) @suffix = suffix @str = time_to_str(@time, @suffix) end
Private Class Methods
Returns a Timestamp
object generated from the current time.
# File lib/textrepo/timestamp.rb, line 316 def now(suffix = nil) Timestamp.new(Time.now, suffix) end
Generate a Timestamp
object from a string which represents a timestamp, such “20201028163400”.
Raises InvalidTimestampStringError
if cannot convert the argument into a Timestamp
object.
# File lib/textrepo/timestamp.rb, line 350 def parse_s(stamp_str) raise InvalidTimestampStringError, "(nil)" if stamp_str.nil? raise InvalidTimestampStringError, "(empty string)" if stamp_str.empty? raise InvalidTimestampStringError, stamp_str if stamp_str.size < 14 begin ye, mo, da, ho, mi, se, sfx = split_stamp(stamp_str).map(&:to_i) Timestamp.new(Time.new(ye, mo, da, ho, mi, se), sfx) rescue ArgumentRangeError, ArgumentError => _ raise InvalidTimestampStringError, stamp_str end end
Splits a string which represents a timestamp into components. Each component represents a part of constructs to instantiate a Time object.
yyyymoddhhmiss sfx yyyy mo dd hh mi ss sfx "20201230123456" -> "2020", "12", "30", "12", "34", "56" "20201230123456_789" -> "2020", "12", "30", "12", "34", "56", "789"
Raises InvalidTimestampStringError
if nil was passed as an arguemnt.
# File lib/textrepo/timestamp.rb, line 331 def split_stamp(stamp_str) raise InvalidTimestampStringError, "(nil)" if stamp_str.nil? # yyyy mo dd hh mi ss sfx a = [0..3, 4..5, 6..7, 8..9, 10..11, 12..13, 15..17].map {|r| stamp_str[r]} a.delete_at(-1) if a[-1].nil? a.map{|e| e || "" } end
Public Instance Methods
Returns difference of seconds between self and an argument. If the argument is an Integer object, returns a new Timestamp
object which is the given seconds behind.
Even if the suffix is not nil, the new Timestamp
object will always have nil as its suffix.
# File lib/textrepo/timestamp.rb, line 129 def -(arg) case arg when Time @time - arg when Timestamp @time - arg.time when Integer Timestamp.new(@time - arg, nil) when NilClass raise TypeError, "can't convert nil into an exact number" else raise ArgumentError, arg end end
Returns a character or sub-string specified with args.
Following type of objects could be used as args:
-
Integer : specifies an index
-
Integer, Integer : specified an start index and length of sub-string
-
Range : specified range of sub-string
-
Symbol : specified a type of part
Following symbols could be specified:
-
:year
-
:mon, or :month
-
:day
-
:hour
-
:min
-
:sec
-
:suffix
# File lib/textrepo/timestamp.rb, line 190 def [](*args) raise ArgumentError, "wrong number of arguments (given %s, execpted 1..2)" % args.size unless (1..2).include?(args.size) arg = args[0] case arg when Symbol, String key = arg.to_sym if key == :suffix @suffix.nil? ? nil : FMTSTRS[key] % @suffix elsif FMTSTRS.keys.include?(key) @time.strftime(FMTSTRS[key]) else nil end else @str[*args] end end
Returns a Timestamp
object which has a next Time object.
If true was passed as an argument, use incremented suffix as base instead of a next Time object.
For example,
"20201110160100" -> "20201110160101" (false as arg) "20201110160100" -> "20201110160100_001" (true as arg) "20201110160200_001" -> "20201110160201" (false as arg) "20201110160200_001" -> "20201110160200_002" (true as arg)
If suffix was 999 before call this method, raises ArgumentRangeError
.
# File lib/textrepo/timestamp.rb, line 227 def next(use_suffix = nil) if use_suffix Timestamp.new(@time, increase_suffix(@suffix.to_i, 1)) else Timestamp.new(@time + 1, nil) end end
Updates the time value to a next Time destructively. See the document for Timestamp#next
for more details.
If suffix was 999 before call this method, raises ArgumentRangeError
.
# File lib/textrepo/timestamp.rb, line 244 def next!(use_suffix = nil) if use_suffix @suffix = increase_suffix(@suffix.to_i, 1) else @time += 1 @suffix = nil end @str = time_to_str(@time, @suffix) self end
Splits the timestamp string into array of time parts, such as year, month, day, hour, minute, and second. Then, returns the array.
When a block was passed, it would apply to each part of the array. Then, returns self.
# File lib/textrepo/timestamp.rb, line 265 def split(_ = $;, _ = 0, &blk) parts = Timestamp.split_stamp(@str) if blk.nil? parts else parts.each { |p| yield p } self end end
Generates an array contains components of the Timestamp
object. Components means “year”, “mon”, “day”, “hour”, “min”, “sec”, and “suffix”.
# File lib/textrepo/timestamp.rb, line 149 def to_a a = [:year, :mon, :day, :hour, :min, :sec, :suffix].map { |s| self.send(s) } a.delete_at(-1) if a[-1].nil? a end
Generates an obvious time string.
%Y %m %d %H %M %S suffix "2020-12-30 12:34:56 (0 | nil)" -> "20201230123456" "2020-12-30 12:34:56 (7)" -> "20201230123456_007"
# File lib/textrepo/timestamp.rb, line 75 def to_s @str end