Class: RBEDI::EDIDateTime

Inherits:
Object
  • Object
show all
Defined in:
lib/rbedi/edi_date_time.rb

Constant Summary collapse

CCYYMMDD_STRFTIME =
"%Y%m%d"
YYMMDD_STRFTIME =
"%y%m%d"
HHMMSS_STRFTIME =
"%H%M%S"
HHMM_STRFTIME =
"%H%M"
DEFAULT_OFFSET =
DateTime.now.strftime("%z")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datetime_start, datetime_end = nil) ⇒ EDIDateTime

Returns a new instance of EDIDateTime.



9
10
11
12
13
14
15
16
17
# File 'lib/rbedi/edi_date_time.rb', line 9

def initialize(datetime_start, datetime_end = nil)
  if datetime_start.is_a?(EDIDateTime)
    @datetime_start = datetime_start.datetime_start
    @datetime_end = datetime_start.datetime_end
  else
    @datetime_start = datetime_start
    @datetime_end = datetime_end
  end
end

Instance Attribute Details

#datetime_endObject

Returns the value of attribute datetime_end.



67
68
69
# File 'lib/rbedi/edi_date_time.rb', line 67

def datetime_end
  @datetime_end
end

#datetime_startObject

Returns the value of attribute datetime_start.



67
68
69
# File 'lib/rbedi/edi_date_time.rb', line 67

def datetime_start
  @datetime_start
end

Class Method Details

.nowObject



19
20
21
# File 'lib/rbedi/edi_date_time.rb', line 19

def self.now
  new(DateTime.now)
end

.now_rangeObject



23
24
25
# File 'lib/rbedi/edi_date_time.rb', line 23

def self.now_range
  new(DateTime.now, DateTime.now)
end

.parse(date: "", time: "", offset: DEFAULT_OFFSET) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rbedi/edi_date_time.rb', line 27

def self.parse(date: "", time: "", offset: DEFAULT_OFFSET)
  start_date, end_date = date.split("-")
  start_time, end_time = time.split("-")

  if start_date.nil? || start_date.empty?
    date_strftime = ""
  else
    date_strftime = CCYYMMDD_STRFTIME if start_date.length == 8
    date_strftime = YYMMDD_STRFTIME if start_date.length == 6
  end

  if start_time.nil? || start_time.empty?
    time_strftime = ""
  else
    time_strftime = HHMMSS_STRFTIME if start_time.length == 6
    time_strftime = HHMM_STRFTIME if start_time.length == 4
  end

  start_datetime = DateTime.strptime("#{start_date}#{start_time}#{offset}", date_strftime + time_strftime + "%z")
  end_datetime = end_date || end_time ? DateTime.strptime("#{end_date}#{end_time}#{offset}", date_strftime + time_strftime + "%z") : nil

  new(start_datetime, end_datetime)
end

Instance Method Details

#ccyymmddObject



51
52
53
# File 'lib/rbedi/edi_date_time.rb', line 51

def ccyymmdd
  @datetime_end ? "#{@datetime_start.strftime(CCYYMMDD_STRFTIME)}-#{@datetime_end.strftime(CCYYMMDD_STRFTIME)}" : @datetime_start.strftime(CCYYMMDD_STRFTIME)
end

#hhmmObject



63
64
65
# File 'lib/rbedi/edi_date_time.rb', line 63

def hhmm
  @datetime_end ? "#{@datetime_start.strftime(HHMM_STRFTIME)}-#{@datetime_end.strftime(HHMM_STRFTIME)}" : @datetime_start.strftime(HHMM_STRFTIME)
end

#hhmmssObject



59
60
61
# File 'lib/rbedi/edi_date_time.rb', line 59

def hhmmss
  @datetime_end ? "#{@datetime_start.strftime(HHMMSS_STRFTIME)}-#{@datetime_end.strftime(HHMMSS_STRFTIME)}" : @datetime_start.strftime(HHMMSS_STRFTIME)
end

#yymmddObject



55
56
57
# File 'lib/rbedi/edi_date_time.rb', line 55

def yymmdd
  @datetime_end ? "#{@datetime_start.strftime(YYMMDD_STRFTIME)}-#{@datetime_end.strftime(YYMMDD_STRFTIME)}" : @datetime_start.strftime(YYMMDD_STRFTIME)
end