Class: Backup::Timestamp

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/timestamp.rb

Class Method Summary collapse

Class Method Details

.create(time = nil) ⇒ Object



34
35
36
37
38
# File 'lib/backup/timestamp.rb', line 34

def self.create(time = nil)
  time = time.nil? ? Time.now : time

  time.utc.strftime "%y%m%d%H%M%S"
end

.last_from(list, end_date, start_date = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/backup/timestamp.rb', line 22

def self.last_from(list, end_date, start_date = nil)
  list.sort.reverse.find do |version|
    version = Backup::Timestamp.parse_timestamp version

    unless start_date.nil?
      version >= start_date and version <= end_date
    else
      version <= end_date
    end
  end
end

.parse_timestamp(version, last = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/backup/timestamp.rb', line 3

def self.parse_timestamp(version, last = false)
  version = version.gsub(".", "").gsub(" ", "").gsub(":", "")

  puts_fail "Invalid date format: #{version}" unless version.match /[0-9]{6,}/

  year, month, day, hour, min, sec =
    version.split(/([0-9]{2})/).map do |date|
      date.to_i unless date.empty?
    end.compact

  if last
    hour = 23 if hour.nil?
    min = 59 if min.nil?
    sec = 59 if sec.nil?
  end

  time = Time.new(year + 2000, month, day, hour, min, sec, 0)
end

.to_s(time) ⇒ Object



44
45
46
# File 'lib/backup/timestamp.rb', line 44

def self.to_s(time)
  time.strftime "%y.%m.%d %H:%M:%S" if time.is_a? Time
end

.to_str(version) ⇒ Object



40
41
42
# File 'lib/backup/timestamp.rb', line 40

def self.to_str(version)
  to_s parse_timestamp(version)
end