Class: Embulk::Java::TimeParserHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/java/time_helper.rb

Defined Under Namespace

Classes: Factory

Instance Method Summary collapse

Constructor Details

#initialize(format_string, default_time) ⇒ TimeParserHelper

Returns a new instance of TimeParserHelper.



20
21
22
23
# File 'lib/embulk/java/time_helper.rb', line 20

def initialize(format_string, default_time)
  @format_string = format_string
  @default_time = default_time
end

Instance Method Details

#getZoneObject

Override



74
75
76
# File 'lib/embulk/java/time_helper.rb', line 74

def getZone
  @zone
end

#strptimeUsec(text) ⇒ Object

Override



26
27
28
29
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
69
70
71
# File 'lib/embulk/java/time_helper.rb', line 26

def strptimeUsec(text)
  hash = Date._strptime(text, @format_string)
  unless hash
    raise Java::TimestampParseException.new("Failed to parse '" + text + "'")
  end

  if seconds = hash[:seconds]
    sec_fraction = hash[:sec_fraction]  # Rational
    usec = sec_fraction * 1_000_000 if sec_fraction
    return seconds * 1_000_000 + usec.to_i

  else
    year = hash[:year]
    mon = hash[:mon]
    day = hash[:mday]
    hour = hash[:hour]
    min = hash[:min]
    sec = hash[:sec]
    sec_fraction = hash[:sec_fraction]
    usec = sec_fraction * 1_000_000 if sec_fraction
    zone = hash[:zone]

    now = @default_time
    begin
      break if year; year = now.year
      break if mon; mon = now.mon
      break if day; day = now.day
      break if hour; hour = now.hour
      break if min; min = now.min
      break if sec; sec = now.sec
      break if sec_fraction; usec = now.tv_usec
    end until true

    year ||= 1970
    mon ||= 1
    day ||= 1
    hour ||= 0
    min ||= 0
    sec ||= 0
    usec ||= 0

    @zone = zone
    time = Time.utc(year, mon, day, hour, min, sec, usec)
    return time.tv_sec * 1_000_000 + time.tv_usec
  end
end