Class: ExoBasic::Timer

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

Constant Summary collapse

EPOCH =
Time.utc(1970, 1, 1)
END_OF_ALL_DAYS =
Time.utc(3000, 1, 1)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Timer

Returns a new instance of Timer.



8
9
10
11
12
13
14
# File 'lib/exobasic/time/timer.rb', line 8

def initialize(name=nil)
  @name    = name
  @start   = Time.now
  @prev    = @start
  @now     = @start
  @samples = 0
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/exobasic/time/timer.rb', line 3

def name
  @name
end

#nowObject (readonly)

Returns the value of attribute now.



3
4
5
# File 'lib/exobasic/time/timer.rb', line 3

def now
  @now
end

#prevObject (readonly)

Returns the value of attribute prev.



3
4
5
# File 'lib/exobasic/time/timer.rb', line 3

def prev
  @prev
end

#samplesObject (readonly)

Returns the value of attribute samples.



3
4
5
# File 'lib/exobasic/time/timer.rb', line 3

def samples
  @samples
end

#startObject (readonly)

Returns the value of attribute start.



3
4
5
# File 'lib/exobasic/time/timer.rb', line 3

def start
  @start
end

Class Method Details

.add_days(t, days) ⇒ Object



86
87
88
# File 'lib/exobasic/time/timer.rb', line 86

def self.add_days(t, days)
  t + days * 86400
end

.add_sec(t, sec) ⇒ Object



90
91
92
# File 'lib/exobasic/time/timer.rb', line 90

def self.add_sec(t, sec)
  t + sec
end

.date_by_day(t) ⇒ Object

YYYY, (1..366)


113
114
115
# File 'lib/exobasic/time/timer.rb', line 113

def self.date_by_day(t)
  [t.year, t.strftime('%-j').to_i]
end

.date_by_day_to_epoch(tuple) ⇒ Object



117
118
119
# File 'lib/exobasic/time/timer.rb', line 117

def self.date_by_day_to_epoch(tuple)
  Timer.to_epoch(Time.strptime("#{tuple[0]} #{tuple[1]}", '%Y %j'))
end

.date_by_month(t) ⇒ Object

YYYY, (1..12), (1..31)


144
145
146
# File 'lib/exobasic/time/timer.rb', line 144

def self.date_by_month(t)
  [t.year, t.month, t.strftime('%-d').to_i]
end

.date_by_month_to_epoch(tuple) ⇒ Object



148
149
150
# File 'lib/exobasic/time/timer.rb', line 148

def self.date_by_month_to_epoch(tuple)
  Timer.to_epoch(Time.strptime("#{tuple[0]} #{tuple[1]} #{tuple[2]}", '%Y %m %d'))
end

.date_by_week(t) ⇒ Object

YYYY, (1..54), (1..7)

week starts with Monday



127
128
129
# File 'lib/exobasic/time/timer.rb', line 127

def self.date_by_week(t)
  [t.year, t.strftime('%W').to_i + 1, t.strftime('%u').to_i]
end

.date_by_week_to_epoch(tuple) ⇒ Object



131
132
133
# File 'lib/exobasic/time/timer.rb', line 131

def self.date_by_week_to_epoch(tuple)
  Timer.to_epoch(Time.strptime("#{tuple[0]} #{tuple[1] - 1} #{tuple[2]}", '%Y %W %u'))
end

.day_of_month(t) ⇒ Object



156
157
158
# File 'lib/exobasic/time/timer.rb', line 156

def self.day_of_month(t)
  Timer.date_by_month(t)[2]
end

.day_of_week_of_year(t) ⇒ Object



139
140
141
# File 'lib/exobasic/time/timer.rb', line 139

def self.day_of_week_of_year(t)
  Timer.date_by_week(t)[2]
end

.day_of_year(t) ⇒ Object



121
122
123
# File 'lib/exobasic/time/timer.rb', line 121

def self.day_of_year(t)
  Timer.date_by_day(t)[1]
end

.duration_in_days(from, to) ⇒ Object



78
79
80
# File 'lib/exobasic/time/timer.rb', line 78

def self.duration_in_days(from, to)
  ((to - from) / 86400).to_i
end

.duration_in_sec(from, to) ⇒ Object



82
83
84
# File 'lib/exobasic/time/timer.rb', line 82

def self.duration_in_sec(from, to)
  to - from
end

.from_epoch(epoch) ⇒ Object

sec



95
96
97
# File 'lib/exobasic/time/timer.rb', line 95

def self.from_epoch(epoch)
  Time.at(epoch)
end

.get(date_time) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/exobasic/time/timer.rb', line 68

def self.get(date_time)
  if date_time.nil?
    Timer.time_now
  elsif date_time.is_a?(String)
    Time.parse(date_time)
  else
    date_time
  end
end

.month(t) ⇒ Object



152
153
154
# File 'lib/exobasic/time/timer.rb', line 152

def self.month(t)
  Timer.date_by_month(t)[1]
end

.time_nowObject



64
65
66
# File 'lib/exobasic/time/timer.rb', line 64

def self.time_now
  Time.now
end

.to_epoch(t) ⇒ Object

sec



100
101
102
# File 'lib/exobasic/time/timer.rb', line 100

def self.to_epoch(t)
  t.strftime('%s').to_i
end

.to_iso8601(t) ⇒ Object



104
105
106
# File 'lib/exobasic/time/timer.rb', line 104

def self.to_iso8601(t)
  t.iso8601
end

.tz_of(t) ⇒ Object

(+, -)HH:MM



161
162
163
# File 'lib/exobasic/time/timer.rb', line 161

def self.tz_of(t)
  t.strftime('%:z')
end

.week_of_year(t) ⇒ Object



135
136
137
# File 'lib/exobasic/time/timer.rb', line 135

def self.week_of_year(t)
  Timer.date_by_week(t)[1]
end

.year(t) ⇒ Object



108
109
110
# File 'lib/exobasic/time/timer.rb', line 108

def self.year(t)
  t.year
end

Instance Method Details

#checkpoint(msg = nil) ⇒ Object



59
60
61
62
# File 'lib/exobasic/time/timer.rb', line 59

def checkpoint(msg=nil)
  self.sample
  STDERR.puts self.show(msg)
end

#deltaObject



32
33
34
# File 'lib/exobasic/time/timer.rb', line 32

def delta
  Time.now - @now
end

#diff_from(that) ⇒ Object



40
41
42
# File 'lib/exobasic/time/timer.rb', line 40

def diff_from(that)
  @now - that.now
end

#lifetimeObject



36
37
38
# File 'lib/exobasic/time/timer.rb', line 36

def lifetime
  Time.now - @start
end

#merge_with(that) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/exobasic/time/timer.rb', line 44

def merge_with(that)
  if that.start < @start
    @start    = that.start
    @samples += 1
  end
  if that.now > @now
    @now      = that.now
    @samples += 1
  end
end

#sampleObject



16
17
18
19
20
21
# File 'lib/exobasic/time/timer.rb', line 16

def sample
  t         = Time.now
  @prev     = @now
  @now      = t
  @samples += 1
end

#show(msg = nil) ⇒ Object



55
56
57
# File 'lib/exobasic/time/timer.rb', line 55

def show(msg=nil)
  "-- TIME_MEASUREMENT #{@name}::#{msg}::#{@samples}\tfrom_prev = #{@now - @prev}\tfrom_start = #{@now - @start}"
end

#tickObject



23
24
25
# File 'lib/exobasic/time/timer.rb', line 23

def tick
  @now = Time.now
end

#warpObject



27
28
29
30
# File 'lib/exobasic/time/timer.rb', line 27

def warp
  @start   = @prev
  @samples = 1
end