Class: TZInfo::Timestamp
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
| Subclasses: | |
| Super Chains via Extension / Inclusion / Inheritance | |
| Instance Chain: 
          self,
          Comparable
         | |
| Inherits: | Object | 
| Defined in: | lib/tzinfo/timestamp.rb | 
Overview
A time represented as an Integer number of seconds since 1970-01-01
00:00:00 UTC (ignoring leap seconds and using the proleptic Gregorian
calendar), the fraction through the second (sub_second as a Rational) and
an optional UTC offset. Like Ruby's Time class, Timestamp can
distinguish between a local time with a zero offset and a time specified
explicitly as UTC.
Constant Summary
- 
    JD_EPOCH =
    private
    # File 'lib/tzinfo/timestamp.rb', line 16The Unix epoch (1970-01-01 00:00:00 UTC) as a chronological Julian day number. 2440588
Class Method Summary
- 
    
      .create(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, sub_second = 0, utc_offset = nil)  ⇒ Timestamp 
    
    Returns a new Timestamprepresenting the (proleptic Gregorian calendar) date and time specified by the supplied parameters.
- 
    
      .for(value, offset = :preserve) {|timestamp| ... } ⇒ Object 
    
    When used without a block, returns a Timestamprepresentation of a givenTime,DateTimeorTimestamp.
- 
    
      .new(value, sub_second = 0, utc_offset = nil)  ⇒ Timestamp 
    
    constructor
    Initializes a new Timestamp.
- 
    
      .utc(value, sub_second = 0)  
    
    Creates a new UTC Timestamp.
- 
    
      .for_datetime(datetime, ignore_offset, target_utc_offset)  ⇒ Timestamp 
    
    private
    Creates a Timestampthat represents a givenDateTime, optionally ignoring the offset.
- 
    
      .for_time(time, ignore_offset, target_utc_offset)  ⇒ Timestamp 
    
    private
    Creates a Timestampthat represents a givenTime, optionally ignoring the offset.
- 
    
      .for_time_like(time_like, ignore_offset, target_utc_offset)  ⇒ Timestamp 
    
    private
    Creates a Timestampthat represents a givenTime-like object, optionally ignoring the offset (if thetime_likeresponds to #utc_offset).
- 
    
      .for_timestamp(timestamp, ignore_offset, target_utc_offset)  ⇒ Timestamp 
    
    private
    Returns a Timestampthat represents anotherTimestamp, optionally ignoring the offset.
- .is_time_like?(value) ⇒ Boolean private
- 
    
      .new!(value, sub_second = 0, utc_offset = nil)  ⇒ Timestamp 
    
    private
    Constructs a new instance of self(i.e.Timestampor a subclass ofTimestamp) without validating the parameters.
Instance Attribute Summary
- #sub_second ⇒ Numeric readonly
- #utc? ⇒ Boolean readonly
- #utc_offset ⇒ Integer readonly
- #value ⇒ Integer readonly
Instance Method Summary
- 
    
      #<=>(t)  ⇒ Integer 
    
    Compares this Timestampwith another.
- 
    
      #add_and_set_utc_offset(seconds, utc_offset)  ⇒ Timestamp 
    
    Adds a number of seconds to the Timestampvalue, setting the UTC offset of the result.
- #eql?
- #hash ⇒ Integer
- #inspect ⇒ String
- 
    
      #strftime(format)  ⇒ String 
    
    Formats this Timestampaccording to the directives in the given format string.
- 
    
      #to_datetime  ⇒ DateTime 
    
    Converts this Timestampto a GregorianDateTime.
- 
    
      #to_i  ⇒ Integer 
    
    Converts this Timestampto anIntegernumber of seconds since 1970-01-01 00:00:00 UTC (ignoring leap seconds).
- #to_s ⇒ String
- 
    
      #to_time  ⇒ Time 
    
    Converts this Timestampto aTime.
- #utc ⇒ Timestamp readonly
- 
    
      #new_datetime(klass = DateTime)  
    
    protected
    Internal use only
    Internal use only
    Constructs a new instance of a DateTimeorDateTime-like class with the same #value, #sub_second and #utc_offset as thisTimestamp.
- 
    
      #new_time(klass = Time)  
    
    protected
    Internal use only
    Internal use only
    Creates a new instance of a TimeorTime-like class matching the #value and #sub_second of thisTimestamp, but not setting the offset.
- 
    
      #initialize!(value, sub_second = 0, utc_offset = nil)  
    
    private
    Initializes a new Timestampwithout validating the parameters.
- 
    
      #sub_second_to_s  ⇒ String 
    
    private
    Converts the #sub_second value to a Stringsuitable for appending to theStringrepresentation of aTimestamp.
- 
    
      #value_and_sub_second_to_s(offset = 0)  ⇒ String 
    
    private
    Converts the value and sub-seconds to a String, adding on the given offset.
Constructor Details
    .new(value, sub_second = 0, utc_offset = nil)  ⇒ Timestamp 
  
Initializes a new Timestamp.
# File 'lib/tzinfo/timestamp.rb', line 344
def initialize(value, sub_second = 0, utc_offset = nil) raise ArgumentError, 'value must be an Integer' unless value.kind_of?(Integer) raise ArgumentError, 'sub_second must be a Rational or the Integer 0' unless (sub_second.kind_of?(Integer) && sub_second == 0) || sub_second.kind_of?(Rational) raise RangeError, 'sub_second must be >= 0 and < 1' if sub_second < 0 || sub_second >= 1 raise ArgumentError, 'utc_offset must be an Integer, :utc or nil' if utc_offset && utc_offset != :utc && !utc_offset.kind_of?(Integer) initialize!(value, sub_second, utc_offset) end
Class Method Details
    .create(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, sub_second = 0, utc_offset = nil)  ⇒ Timestamp 
  
Returns a new Timestamp representing the (proleptic Gregorian
calendar) date and time specified by the supplied parameters.
If #utc_offset is nil, :utc or 0, the date and time parameters will
be interpreted as representing a UTC date and time. Otherwise the date
and time parameters will be interpreted as a local date and time with
the given offset.
# File 'lib/tzinfo/timestamp.rb', line 55
def create(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, sub_second = 0, utc_offset = nil) raise ArgumentError, 'year must be an Integer' unless year.kind_of?(Integer) raise ArgumentError, 'month must be an Integer' unless month.kind_of?(Integer) raise ArgumentError, 'day must be an Integer' unless day.kind_of?(Integer) raise ArgumentError, 'hour must be an Integer' unless hour.kind_of?(Integer) raise ArgumentError, 'minute must be an Integer' unless minute.kind_of?(Integer) raise ArgumentError, 'second must be an Integer' unless second.kind_of?(Integer) raise RangeError, 'month must be between 1 and 12' if month < 1 || month > 12 raise RangeError, 'day must be between 1 and 31' if day < 1 || day > 31 raise RangeError, 'hour must be between 0 and 23' if hour < 0 || hour > 23 raise RangeError, 'minute must be between 0 and 59' if minute < 0 || minute > 59 raise RangeError, 'second must be between 0 and 59' if second < 0 || second > 59 # Based on days_from_civil from https://howardhinnant.github.io/date_algorithms.html#days_from_civil after_february = month > 2 year -= 1 unless after_february era = year / 400 year_of_era = year - era * 400 day_of_year = (153 * (month + (after_february ? -3 : 9)) + 2) / 5 + day - 1 day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year days_since_epoch = era * 146097 + day_of_era - 719468 value = ((days_since_epoch * 24 + hour) * 60 + minute) * 60 + second value -= utc_offset if utc_offset.kind_of?(Integer) new(value, sub_second, utc_offset) end
    .for(value, offset = :preserve) {|timestamp| ... } ⇒ Object 
  
When used without a block, returns a Timestamp representation of a
given Time, DateTime or Timestamp.
When called with a block, the Timestamp representation of #value is
passed to the block. The block must then return a Timestamp, which
will be converted back to the type of the initial value. If the initial
value was a Timestamp, the block result will be returned. If the
initial value was a DateTime, a Gregorian DateTime will be returned.
The UTC offset of #value can either be preserved (the Timestamp
representation will have the same UTC offset as #value), ignored (the
Timestamp representation will have no defined UTC offset), or treated
as though it were UTC (the Timestamp representation will have a
#utc_offset of 0 and #utc? will return true).
# File 'lib/tzinfo/timestamp.rb', line 112
def for(value, offset = :preserve) raise ArgumentError, 'value must be specified' unless value case offset when :ignore ignore_offset = true target_utc_offset = nil when :treat_as_utc ignore_offset = true target_utc_offset = :utc when :preserve ignore_offset = false target_utc_offset = nil else raise ArgumentError, 'offset must be :preserve, :ignore or :treat_as_utc' end time_like = false = case value when Time for_time(value, ignore_offset, target_utc_offset) when DateTime for_datetime(value, ignore_offset, target_utc_offset) when Timestamp (value, ignore_offset, target_utc_offset) else raise ArgumentError, "#{value.class} values are not supported" unless is_time_like?(value) time_like = true for_time_like(value, ignore_offset, target_utc_offset) end if block_given? result = yield raise ArgumentError, 'block must return a Timestamp' unless result.kind_of?(Timestamp) case value when Time result.to_time when DateTime result.to_datetime else # A Time-like value or a Timestamp time_like ? result.to_time : result end else end end
    .for_datetime(datetime, ignore_offset, target_utc_offset)  ⇒ Timestamp  (private)
  
Creates a Timestamp that represents a given DateTime, optionally
ignoring the offset.
# File 'lib/tzinfo/timestamp.rb', line 231
def for_datetime(datetime, ignore_offset, target_utc_offset) value = (datetime.jd - JD_EPOCH) * 86400 + datetime.sec + datetime.min * 60 + datetime.hour * 3600 sub_second = datetime.sec_fraction if ignore_offset utc_offset = target_utc_offset else utc_offset = (datetime.offset * 86400).to_i value -= utc_offset end new!(value, sub_second, utc_offset) end
    .for_time(time, ignore_offset, target_utc_offset)  ⇒ Timestamp  (private)
  
Creates a Timestamp that represents a given Time, optionally
ignoring the offset.
# File 'lib/tzinfo/timestamp.rb', line 206
def for_time(time, ignore_offset, target_utc_offset) value = time.to_i sub_second = time.subsec if ignore_offset utc_offset = target_utc_offset value += time.utc_offset elsif time.utc? utc_offset = :utc else utc_offset = time.utc_offset end new!(value, sub_second, utc_offset) end
    .for_time_like(time_like, ignore_offset, target_utc_offset)  ⇒ Timestamp  (private)
  
Creates a Timestamp that represents a given Time-like object,
optionally ignoring the offset (if the time_like responds to
#utc_offset).
# File 'lib/tzinfo/timestamp.rb', line 296
def for_time_like(time_like, ignore_offset, target_utc_offset) value = time_like.to_i sub_second = time_like.subsec.to_r if ignore_offset utc_offset = target_utc_offset value += time_like.utc_offset.to_i if time_like.respond_to?(:utc_offset) elsif time_like.respond_to?(:utc_offset) utc_offset = time_like.utc_offset.to_i else utc_offset = 0 end new(value, sub_second, utc_offset) end
    .for_timestamp(timestamp, ignore_offset, target_utc_offset)  ⇒ Timestamp  (private)
  
Returns a Timestamp that represents another Timestamp, optionally
ignoring the offset. If the result would be identical to #value, the
same instance is returned. If the passed in value is an instance of a
subclass of Timestamp, then a new Timestamp will always be returned.
# File 'lib/tzinfo/timestamp.rb', line 256
def (, ignore_offset, target_utc_offset) if ignore_offset if target_utc_offset unless target_utc_offset == :utc && .utc? || .utc_offset == target_utc_offset return new!(.value + (.utc_offset || 0), .sub_second, target_utc_offset) end elsif .utc_offset return new!(.value + .utc_offset, .sub_second) end end unless .instance_of?(Timestamp) # timestamp is identical in value, sub_second and utc_offset but is a # subclass (i.e. TimestampWithOffset). Return a new Timestamp # instance. return new!(.value, .sub_second, .utc? ? :utc : .utc_offset) end end
    .is_time_like?(value)  ⇒ Boolean  (private)
  
    .new!(value, sub_second = 0, utc_offset = nil)  ⇒ Timestamp  (private)
  
Constructs a new instance of self (i.e. Timestamp or a subclass of
Timestamp) without validating the parameters. This method is used
internally within Timestamp to avoid the overhead of checking
parameters.
# File 'lib/tzinfo/timestamp.rb', line 192
def new!(value, sub_second = 0, utc_offset = nil) result = allocate result.send(:initialize!, value, sub_second, utc_offset) result end
.utc(value, sub_second = 0)
Creates a new UTC Timestamp.
# File 'lib/tzinfo/timestamp.rb', line 172
def utc(value, sub_second = 0) new(value, sub_second, :utc) end
Instance Attribute Details
    #sub_second  ⇒ Numeric  (readonly)
  
# File 'lib/tzinfo/timestamp.rb', line 321
attr_reader :sub_second
    #utc?  ⇒ Boolean  (readonly)
  
# File 'lib/tzinfo/timestamp.rb', line 355
def utc? @utc end
    #utc_offset  ⇒ Integer  (readonly)
  
# File 'lib/tzinfo/timestamp.rb', line 325
attr_reader :utc_offset
    #value  ⇒ Integer  (readonly)
  
# File 'lib/tzinfo/timestamp.rb', line 316
attr_reader :value
Instance Method Details
    #<=>(t)  ⇒ Integer 
  
Compares this Timestamp with another.
Timestamp instances without a defined UTC offset are not comparable with
Timestamp instances that have a defined UTC offset.
# File 'lib/tzinfo/timestamp.rb', line 454
def <=>(t) return nil unless t.kind_of?(Timestamp) return nil if utc_offset && !t.utc_offset return nil if !utc_offset && t.utc_offset result = value <=> t.value result = sub_second <=> t.sub_second if result == 0 result end
    #add_and_set_utc_offset(seconds, utc_offset)  ⇒ Timestamp 
  
Adds a number of seconds to the Timestamp value, setting the UTC offset
of the result.
# File 'lib/tzinfo/timestamp.rb', line 372
def add_and_set_utc_offset(seconds, utc_offset) raise ArgumentError, 'seconds must be an Integer' unless seconds.kind_of?(Integer) raise ArgumentError, 'utc_offset must be an Integer, :utc or nil' if utc_offset && utc_offset != :utc && !utc_offset.kind_of?(Integer) return self if seconds == 0 && utc_offset == (@utc ? :utc : @utc_offset) Timestamp.send(:new!, @value + seconds, @sub_second, utc_offset) end
#eql?
[ GitHub ]# File 'lib/tzinfo/timestamp.rb', line 464
alias eql? ==
    #hash  ⇒ Integer 
  
# File 'lib/tzinfo/timestamp.rb', line 468
def hash [@value, @sub_second, !!@utc_offset].hash end
#initialize!(value, sub_second = 0, utc_offset = nil) (private)
Initializes a new Timestamp without validating the parameters. This
method is used internally within Timestamp to avoid the overhead of
checking parameters.
# File 'lib/tzinfo/timestamp.rb', line 538
def initialize!(value, sub_second = 0, utc_offset = nil) @value = value # Convert Rational(0,1) to 0. @sub_second = sub_second == 0 ? 0 : sub_second if utc_offset @utc = utc_offset == :utc @utc_offset = @utc ? 0 : utc_offset else @utc = @utc_offset = nil end end
    #inspect  ⇒ String 
  
# File 'lib/tzinfo/timestamp.rb', line 474
def inspect "#<#{self.class}: @value=#{@value}, @sub_second=#{@sub_second}, @utc_offset=#{@utc_offset.inspect}, @utc=#{@utc.inspect}>" end
#new_datetime(klass = DateTime) (protected)
Constructs a new instance of a DateTime or DateTime-like class with
the same #value, #sub_second and #utc_offset as this Timestamp.
# File 'lib/tzinfo/timestamp.rb', line 496
def new_datetime(klass = DateTime) # Can't specify the start parameter unless the jd parameter is an exact number of days. # Use #gregorian instead. datetime = klass.jd(JD_EPOCH + ((@value.to_r + @sub_second) / 86400)).gregorian @utc_offset && @utc_offset != 0 ? datetime.new_offset(Rational(@utc_offset, 86400)) : datetime end
#new_time(klass = Time) (protected)
Creates a new instance of a Time or Time-like class matching the
#value and #sub_second of this Timestamp, but not setting the offset.
# File 'lib/tzinfo/timestamp.rb', line 486
def new_time(klass = Time) klass.at(@value, @sub_second * 1_000_000) end
    #strftime(format)  ⇒ String 
  
Formats this Timestamp according to the directives in the given format
string.
# File 'lib/tzinfo/timestamp.rb', line 426
def strftime(format) raise ArgumentError, 'format must be specified' unless format to_time.strftime(format) end
    #sub_second_to_s  ⇒ String  (private)
  
Converts the #sub_second value to a String suitable for appending to
the String representation of a Timestamp.
# File 'lib/tzinfo/timestamp.rb', line 518
def sub_second_to_s if @sub_second == 0 '' else " #{@sub_second.numerator}/#{@sub_second.denominator}" end end
    #to_datetime  ⇒ DateTime 
  
Converts this Timestamp to a Gregorian DateTime.
# File 'lib/tzinfo/timestamp.rb', line 406
def to_datetime new_datetime end
    #to_i  ⇒ Integer 
  
Converts this Timestamp to an Integer number of seconds since
1970-01-01 00:00:00 UTC (ignoring leap seconds).
# File 'lib/tzinfo/timestamp.rb', line 415
def to_i value end
    #to_s  ⇒ String 
  
# File 'lib/tzinfo/timestamp.rb', line 432
def to_s return value_and_sub_second_to_s unless @utc_offset return "#{value_and_sub_second_to_s} UTC" if @utc sign = @utc_offset >= 0 ? '+' : '-' min, sec = @utc_offset.abs.divmod(60) hour, min = min.divmod(60) "#{value_and_sub_second_to_s(@utc_offset)} #{sign}#{'%02d' % hour}:#{'%02d' % min}#{sec > 0 ? ':%02d' % sec : nil}#{@utc_offset != 0 ? " (#{value_and_sub_second_to_s} UTC)" : nil}" end
    #to_time  ⇒ Time 
  
Converts this Timestamp to a Time.
    #utc  ⇒ Timestamp  (readonly)
  
# File 'lib/tzinfo/timestamp.rb', line 381
def utc return self if @utc Timestamp.send(:new!, @value, @sub_second, :utc) end
    #value_and_sub_second_to_s(offset = 0)  ⇒ String  (private)
  
Converts the value and sub-seconds to a String, adding on the given
offset.
# File 'lib/tzinfo/timestamp.rb', line 510
def value_and_sub_second_to_s(offset = 0) "#{@value + offset}#{sub_second_to_s}" end