Module: Osu::DB::TimeUtil

Defined in:
lib/osu-db/timeutil.rb

Overview

Conversion between System.DateTime.Ticks in .NET and Time in Ruby

Constant Summary collapse

TICKS_PER_SEC =

A single tick represents one hundred nanoseconds or one ten-millionth of a second.

10 ** 7
EPOCH_TO_TICKS =

The value of DateTime.Ticks represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.

0 - Time.utc(1, 1, 1).to_i * TICKS_PER_SEC

Class Method Summary collapse

Class Method Details

.ticks_to_time(ticks) ⇒ Object

Convert DateTime.Ticks to Time



18
19
20
21
22
23
24
# File 'lib/osu-db/timeutil.rb', line 18

def self.ticks_to_time(ticks)
  if ticks.kind_of? Numeric
    Time.at(Rational(ticks - EPOCH_TO_TICKS, TICKS_PER_SEC))
  else
    ticks
  end
end

.time_to_ticks(time) ⇒ Object

Convert Time to DateTime.Ticks



27
28
29
30
31
32
33
# File 'lib/osu-db/timeutil.rb', line 27

def self.time_to_ticks(time)
  if time.kind_of? Numeric
    time.to_i
  else
    EPOCH_TO_TICKS + (time.to_r * TICKS_PER_SEC).to_i
  end
end