Class: OpenSession::Stamp

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/session/time.stamp.rb

Overview

This stamp sits at the centre of a fundamental DevOps pattern concerned with infrastructure provisioning and configuraion management.

The central idea behind the pattern is to link every infrastructure object created during a session with a reference accurate to the nearest centi-second denoting the moment the software runtime (session) began.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStamp

This singleton (one instance) class sets the time just once.



327
328
329
330
331
# File 'lib/session/time.stamp.rb', line 327

def initialize

  @time_now = Time.now;

end

Instance Attribute Details

#time_nowObject (readonly)

Returns the value of attribute time_now.



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

def time_now
  @time_now
end

Class Method Details

.dddObject

Return three character abbreviated day of week.

Examples:

tue => on Tuesday




34
35
36
# File 'lib/session/time.stamp.rb', line 34

def self.ddd
  return Stamp.instance.time_now.strftime( "%a" ).downcase
end

.hhObject

Return two digit (character) hour of day from 00 to 23.

Examples:

22 => between 22.00.00 and 22.59.59 inclusive




41
42
43
# File 'lib/session/time.stamp.rb', line 41

def self.hh
  return Stamp.instance.time_now.strftime "%H"
end

.hhmmObject

Return the 4 digit amalgam of the hour and minute using the 24 hour clock.

Examples:

=> 1525
=> 03:25 pm


200
201
202
# File 'lib/session/time.stamp.rb', line 200

def self.hhmm
  return "#{hh}#{mm}"
end

.hhmm_sstObject

Return the time of day to a TENTH of a second accuracy.

8

characters will always be returned with the 5th one

being the (period) separator.

The first (separated) segment delivers a hhmm 24 hour clock representation of the stamped time.

The 3 digits of the second segment comprise of

second of minute => 2 digits | [00] to [59]
tenth of second  => 1 digit from [0] to [9]

Examples:

=> The time at the 562nd millisecond  of the 49th
   second of the 23rd minute of the 17th hour of
   the day ( 17:23:49.562 )

=> 8 chars
=> 1723.495


224
225
226
# File 'lib/session/time.stamp.rb', line 224

def self.hhmm_sst
  return "#{hhmm}.#{sst}"
end

.jjjObject

Return 3 digit julian day of year [001] to [366].



103
104
105
# File 'lib/session/time.stamp.rb', line 103

def self.jjj
  return Stamp.instance.time_now.strftime "%j"
end

.log_instance_timeObject

TODO:

move method contents into test class

Log segments of time pertaining to the time stamp.



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/session/time.stamp.rb', line 301

def self.log_instance_time

  log.info(x) { "[stamp] -------------- => -------------------------------- #" }
  log.info(x) { "[stamp] eco time stamp => [#{Stamp.instance.time_now.ctime}]" }
  log.info(x) { "[stamp] -------------- => -------------------------------- #" }
  log.info(x) { "[stamp] Univ Time Zone => #{zone}"  }
  log.info(x) { "[stamp] Month Index is => #{mo}"    }
  log.info(x) { "[stamp] Month Name is  => #{mmm}"   }
  log.info(x) { "[stamp] Day Of Week is => #{ddd}"   }
  log.info(x) { "[stamp] -------------- => -------------------------------- #" }
  log.info(x) { "[stamp] Two Digit Year => #{yy}"    }
  log.info(x) { "[stamp] Julian Cal Day => #{jjj}"   }
  log.info(x) { "[stamp] Yr and Jul Day => #{yyjjj}" }
  log.info(x) { "[stamp] Hour of Theday => #{hh}"    }
  log.info(x) { "[stamp] Minute of Hour => #{mm}"    }
  log.info(x) { "[stamp] Hour + Minute  => #{hhmm}"  }
  log.info(x) { "[stamp] Second of Min  => #{ss}"    }
  log.info(x) { "[stamp] 600 Min Slices => #{sst}"   }
  log.info(x) { "[stamp] -------------- => -------------------------------- #" }
  log.info(x) { "[stamp] The Time Stamp => #{yyjjj_hhmm_sst}" }
  log.info(x) { "[stamp] -------------- => -------------------------------- #" }

end

.mmObject

Return two digit minute of hour from [00] to [59].



47
48
49
# File 'lib/session/time.stamp.rb', line 47

def self.mm
  return Stamp.instance.time_now.strftime "%M"
end

.mmmObject

Return three character abbreviated month name.

Examples:

feb => in February




27
28
29
# File 'lib/session/time.stamp.rb', line 27

def self.mmm
  return Stamp.instance.time_now.strftime( "%b" ).downcase
end

.moObject

Return two digit [mo] month index from 01 to 12.

Examples:

02 => in February




20
21
22
# File 'lib/session/time.stamp.rb', line 20

def self.mo
  return Stamp.instance.time_now.strftime "%m"
end

.previous_month_chars(this_month_index, this_4digit_year) ⇒ Object

Given two integer parameters (month index and 4 digit year) representing the month in question this method returns the [PREVIOUS MONTHS] character amalgam in the format [yymo_mmm] where

=> yy  | previous month's two-digit year
=> mo  | previous month's two-digit month index
=> .   | a period (separator)
=> mmm | previous month's abbreviated month name

Example 1 (Simple)


returns char => 1907.jul
4 parameters => 8, 2019
representing => August, 2019

Example 2 (Last Year)


returns char => 1812.dec
4 parameters => 1, 2019
representing => January, 2019


148
149
150
151
152
153
154
155
156
157
158
# File 'lib/session/time.stamp.rb', line 148

def self.previous_month_chars this_month_index, this_4digit_year

  prev_month_index = this_month_index == 1 ? 12 : ( this_month_index - 1 )
  prev_2dig_mn_pad = sprintf '%02d', prev_month_index
  prev_4digit_year = this_month_index == 1 ? ( this_4digit_year - 1 ) : this_4digit_year
  prev_twodigit_yr = "#{prev_4digit_year.to_s}"[2..-1]
  prev_months_name = Date::ABBR_MONTHNAMES[prev_month_index].downcase

  return "#{prev_twodigit_yr}#{prev_2dig_mn_pad}.#{prev_months_name}"

end

.ssObject

Return two digit second of minute from [00] to [59].



53
54
55
# File 'lib/session/time.stamp.rb', line 53

def self.ss
  return Stamp.instance.time_now.strftime "%S"
end

.sstObject

Return a [3 digit] second and tenth of second representation.

The final digit is derived from the 1000 sliced millisecond of second running from 000 to 999.

Truncation (Not Rounding)

The [final] digit is acquired by TRUNCATING (chopping off) the last 2 of the 3 millisecond digits. No rounding is applied.

The 3 returned digits comprise of the

  • second of minute => 2 digits | [00] to [59] (and)

  • tenth of second => 1 digit from [0] to [9]

Examples:


=> The time at the 562nd millisecond  of the 49th
   second of the minute.

=> 3 chars
=> 495


82
83
84
85
# File 'lib/session/time.stamp.rb', line 82

def self.sst
  millisec_string = Stamp.instance.time_now.strftime "%L"
  return "#{ss}#{millisec_string[0]}"
end

.yyObject

Return the [two] digit year (eg 19 for 2019). that we are currently in.



90
91
92
# File 'lib/session/time.stamp.rb', line 90

def self.yy
  return Stamp.instance.time_now.strftime("%Y")[2..-1]
end

.yyjjjObject

Return 5 digit amalgam of year and julian day.

eg [19003] for [January 3rd 2019]


189
190
191
# File 'lib/session/time.stamp.rb', line 189

def self.yyjjj
  return "#{yy}#{jjj}"
end

.yyjjj_hhmm_ss_nanosecString

Return a string timestampt that is a period separated amalgam of the 2 digit year, 3 digit julian day, 2 digit hour, 2 digit minute, 2 digit second and 9 digit nanosecond.

As per the above example, the time returned

  • is the 836592034 nanosecond

  • of the 42nd second

  • of the 25th minute

  • of the 17th hour

  • of the 3rd day

  • of the 20th year

  • of the 21st century

Examples:

return  => 19003.1725.42.836592034
4 time  => 17:25:42 am on January 3rd 2019

Returns:

  • (String)

    Return the time of day to nanosecond accuracy. 23 characters are always returned with three (3) period separators at the 6th, 11th and 14th positions.



286
287
288
289
# File 'lib/session/time.stamp.rb', line 286

def self.yyjjj_hhmm_ss_nanosec
  nanosec_str = Stamp.instance.time_now.strftime "%9N"
  return "#{yyjjj}.#{hhmm}.#{ss}.#{nanosec_str}"
end

.yyjjj_hhmm_sstObject

Return a string timestampt that is a period separated amalgam of the 2 digit year, 3 digit julian day, 2 digit hour, 2 digit minute, 2 digit second and 1 digit rounded down tenth of second.

Return the time of day to a TENTH of a second accuracy.

8

characters will always be returned with the 5th one

being the (period) separator.

The first (separated) segment delivers a hhmm 24 hour clock representation of the stamped time.

The 3 digits of the second segment comprise of

  • second of minute => 2 digits | [00] to [59]

  • tenth of second => 1 digit from [0] to [9]

Examples:

=> 19003.1025
=> 10:25 am on January 3rd 2019
=> The time at the 562nd millisecond  of the 49th
   second of the 23rd minute of the 17th hour of
   the day ( 17:23:49.562 )

=> 8 chars
=> 1723.495


258
259
260
# File 'lib/session/time.stamp.rb', line 258

def self.yyjjj_hhmm_sst
  return "#{yyjjj}.#{hhmm}.#{sst}"
end

.yymo_mmmObject

yymo_mmm

returns an amalgam of

=> the two-digit year
=> the two-digit month index (starting at 01)
=> a period (separator)
=> the abbreviated month name

Examples:

=> 1908.aug
=> for August 2019


119
120
121
# File 'lib/session/time.stamp.rb', line 119

def self.yymo_mmm
  return "#{yy}#{mo}.#{mmm}"
end

.yymo_mmm_prevObject

Using the current class time this method returns the character amalgam for the [PREVIOUS MONTH] in the format [yymo_mmm] where

=> yy  | last month's two-digit year
=> mo  | last month's two-digit month index
=> .   | a period (separator)
=> mmm | last month's abbreviated month name

Example 1 (Simple)


returns => 1907.jul
if this month is => August 2019

Example 2 (Last Year)


returns => 1812.dec
if this month is => January 2019


182
183
184
# File 'lib/session/time.stamp.rb', line 182

def self.yymo_mmm_prev
  return previous_month_chars mo.to_i, yyyy.to_i
end

.yyyyObject

Return the [four] digit year (eg 2019) that we are currently in.



97
98
99
# File 'lib/session/time.stamp.rb', line 97

def self.yyyy
  return Stamp.instance.time_now.strftime("%Y")
end

.zoneObject

Return the Rubyfied time zone being used.



293
294
295
# File 'lib/session/time.stamp.rb', line 293

def self.zone
  return Stamp.instance.time_now.zone
end