Module: ADIWG::Mdtranslator::Readers::Fgdc::DateTime

Defined in:
lib/adiwg/mdtranslator/readers/fgdc/modules/module_dateTime.rb

Class Method Summary collapse

Class Method Details

.unpack(date, time, hResponseObj) ⇒ Object



19
20
21
22
23
24
25
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/adiwg/mdtranslator/readers/fgdc/modules/module_dateTime.rb', line 19

def self.unpack(date, time, hResponseObj)

   # instance classes needed in script
   intMetadataClass = InternalMetadata.new
   hDateTime = intMetadataClass.newDateTime

   if date.nil? || date == ''
      hResponseObj[:readerExecutionMessages] << 'ERROR: FGDC reader: date missing from dateTime creation'
      hResponseObj[:readerExecutionPass] = false
      return nil
   end

   # remove invalid date and time input strings
   unless date =~ /^[0-9\-]*$/
      return nil
   end
   unless time =~ /^[0-9:]*$/
      time = ''
   end

   # determine if date/time is universal (default) or local
   zoneFlag = Fgdc.
   zoneFlag = 'universal time' if zoneFlag.nil? || zoneFlag == ''

   # convert date from fgdc to iso format
   year = date.byteslice(0,4)
   month = date.byteslice(4,2)
   day = date.byteslice(6,2)

   haveFullDate = false
   dtIn = year
   unless month.nil? || month == ''
      dtIn += '-' + month
      unless day.nil? || day == ''
         dtIn += '-' + day
         haveFullDate = true
      end
   end

   # if have full date and some time expression
   # add time element to date string
   if haveFullDate
      unless time == ''
         aScan = time.scan(/:/)
         if aScan.empty?
            hour = time.byteslice(0,2)
            minute = time.byteslice(2,2)
            second = time.byteslice(4,2)
         else
            aTime = time.split(':')
            hour = aTime[0]
            minute = aTime[1]
            second = aTime[2]
         end

         # all times must include timezone
         # timezone format requires hours, minutes, and seconds
         hour = '00' if hour.nil? || hour == ''
         minute = '00' if minute.nil? || minute == ''
         second = '00' if second.nil? || second == ''
         tmIn = 'T' + hour + ':' + minute + ':' + second

         dtIn += tmIn

         # add offset to date/time string
         if zoneFlag == 'universal time'
            dtIn = dtIn + '+00:00'
         else
            timeOffset = Time.now.gmt_offset
            aOffset = timeOffset.divmod(3600)
            hourOff = aOffset[0]
            minOff = aOffset[1] * 60
            if hourOff >= 0
               zone = '+' + '%02d' % hourOff + ':' + '%02d' % minOff
            else
               zone = '%03d' % hourOff + ':' + '%02d' % minOff
            end
            dtIn = dtIn + zone
         end

      end
   end

   # if dateTimeFromString fails, [0] = nil; [1] = 'ERROR'
   aDateTimeReturn = AdiwgDateTimeFun.dateTimeFromString(dtIn)
   if aDateTimeReturn[1] == 'ERROR'
      hResponseObj[:readerExecutionMessages] << 'ERROR: FGDC reader: Conversion of dateTime string to object failed'
      hResponseObj[:readerExecutionPass] = false
      return nil
   end

   # always output 'universal time'
   if zoneFlag == 'universal time'
      utc = aDateTimeReturn[0]
   else
      utc = aDateTimeReturn[0].new_offset(Rational(0,24))
   end

   # build internal dateTime object
   hDateTime[:dateTime] = utc
   hDateTime[:dateResolution] = aDateTimeReturn[1]

   return hDateTime

end