Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/gb_date/time.rb

Instance Method Summary collapse

Instance Method Details

#to_api_decimalBigDecimal

Seconds as BigDecimal with precision to microseconds

Returns:

  • (BigDecimal)


10
11
12
# File 'lib/gb_date/time.rb', line 10

def to_api_decimal
  BigDecimal.new("#{self.to_i}.#{sprintf('%06d',self.nsec/1000)}")
end

#to_api_fBigDecimal

Seconds as Float with precision to microseconds

Returns:

  • (BigDecimal)


16
17
18
# File 'lib/gb_date/time.rb', line 16

def to_api_f
  self.to_api_decimal.to_f
end

#to_api_sString

Return string with date represented by seconds with precision to microseconds

Returns:



22
23
24
# File 'lib/gb_date/time.rb', line 22

def to_api_s
  sprintf('%.6f', to_api_decimal)
end

#to_datetimeObject

Converts time to DateTime

Returns:

  • DateTime



28
29
30
31
32
33
34
35
36
# File 'lib/gb_date/time.rb', line 28

def to_datetime
  # Convert seconds + microseconds into a fractional number of seconds
  seconds = sec + Rational(usec, 10**6)

  # Convert a UTC offset measured in minutes to one measured in a
  # fraction of a day.
  offset = Rational(utc_offset, 60 * 60 * 24)
  DateTime.new(year, month, day, hour, min, seconds, offset)
end

#to_decimalBigDecimal

Seconds as BigDecimal

Returns:

  • (BigDecimal)


4
5
6
# File 'lib/gb_date/time.rb', line 4

def to_decimal
  BigDecimal.new("#{self.to_i}.#{sprintf('%09d',self.nsec)}")
end