Class: TimeFile
- Inherits:
-
Object
- Object
- TimeFile
- Defined in:
- lib/vcseif/utils/time_file.rb
Constant Summary collapse
- ISO8601_FORMAT =
Store the last time the connector was run - just in case the connector stops and restarts Time (as UTC) is stored as a string in YYYY-MM-DD HH:MM:SS UTC“ format
"%Y-%m-%dT%H:%M:%S.%L%z"
- STD_TS_FORMAT =
"%Y-%m-%d %H:%M:%S Z"
- STD_STRPTIME_FMT =
"%Y-%m-%d %H:%M:%S %z"
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
Instance Method Summary collapse
- #exists? ⇒ Boolean
-
#initialize(filename, logger) ⇒ TimeFile
constructor
A new instance of TimeFile.
- #read ⇒ Object
- #write(timehack = nil) ⇒ Object
Constructor Details
#initialize(filename, logger) ⇒ TimeFile
Returns a new instance of TimeFile.
21 22 23 24 |
# File 'lib/vcseif/utils/time_file.rb', line 21 def initialize(filename, logger) @filename = filename @log = logger end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
18 19 20 |
# File 'lib/vcseif/utils/time_file.rb', line 18 def filename @filename end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
19 20 21 |
# File 'lib/vcseif/utils/time_file.rb', line 19 def log @log end |
Instance Method Details
#exists? ⇒ Boolean
26 27 28 |
# File 'lib/vcseif/utils/time_file.rb', line 26 def exists? return File.exists?(@filename) end |
#read ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/vcseif/utils/time_file.rb', line 30 def read() %{ Returns the time stored in the file as an epoch seconds value or a default value of the time 5 minutes ago (again in epoch seconds form). } # default to 5 minutes ago if file non-existent or empty default = Time.now - (5 * 60) = nil if exists? begin File.open(@filename, "r") do |file| line = file.gets if !line.nil? and !line.empty? line = line.strip if line =~ /\d+.*T\d+:\d+:\d+.\d+/ # in old-style ISO8601 format = Time.strptime(line, ISO8601_FORMAT) else begin = Time.strptime(line, STD_STRPTIME_FMT) rescue => ex problem = "Invalid format for timefile entry: #{line}" action = "reverting to default of #{}" @log.error("%s, %s" % [problem, action]) end end end end rescue => ex problem = "Could not read time entry from #{@filename}" action = "rewriting time file with default value" @log.error("%s, %s" % [problem, action]) write() end end = default if .nil? return end |
#write(timehack = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/vcseif/utils/time_file.rb', line 70 def write(timehack=nil) %{ Writes the time (expected as a Ruby Time object) to the file in ISO 8601 format } timehack = Time.now if timehack.nil? stamp = timehack.utc.strftime(STD_TS_FORMAT) File.open(@filename, 'w') { |file| file.puts "%s\n" % stamp } end |