Module: AdiwgDateTimeFun
- Defined in:
- lib/adiwg/mdtranslator/internal/module_dateTimeFun.rb
Class Method Summary collapse
- .dateTimeFromString(inDateTime) ⇒ Object
- .stringDateFromDateObject(obj) ⇒ Object
- .stringDateFromDateTime(myDateTime, dateResolution) ⇒ Object
- .stringDateTimeFromDateObject(obj) ⇒ Object
- .stringDateTimeFromDateTime(myDateTime, dateResolution) ⇒ Object
- .stringTimeFromDateTime(myDateTime, dateResolution) ⇒ Object
- .writeDuration(hDuration) ⇒ Object
Class Method Details
.dateTimeFromString(inDateTime) ⇒ Object
13 14 15 16 17 18 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 |
# File 'lib/adiwg/mdtranslator/internal/module_dateTimeFun.rb', line 13 def self.dateTimeFromString(inDateTime) inDate, inTime = inDateTime.split('T') # test for valid date before passing to formatter y, m, d = inDate.split('-') m = 1 if m.nil? d = 1 if d.nil? haveDate = Date.valid_date?(y.to_i, m.to_i, d.to_i) return nil, 'ERROR' unless haveDate # test for valid time before passing to formatter # complete time regex /^[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|[zZ])/ # concatenate each permitted time format unless inTime.nil? timeReg = /^[0-2]\d|^[0-2]\d([+-][0-2]\d:[0-5]\d|[zZ])|^[0-2]\d:[0-5]\d|^[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|[zZ])|^[0-2]\d:[0-5]\d:[0-5]\d|^[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|[zZ])|^[0-2]\d:[0-5]\d:[0-5]\d\.\d+|^[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/ return nil, 'ERROR' unless (timeReg =~ inTime) == 0 end hFormats = {'YMDhmsLZ' => '%Y-%m-%dT%H:%M:%S.%L%z', 'YMDhmsL' => '%Y-%m-%dT%H:%M:%S.%L', 'YMDhmsZ' => '%Y-%m-%dT%H:%M:%S%z', 'YMDhms' => '%Y-%m-%dT%H:%M:%S', 'YMDhmZ' => '%Y-%m-%dT%H:%M%z', 'YMDhm' => '%Y-%m-%dT%H:%M', 'YMDhZ' => '%Y-%m-%dT%H%z', 'YMDh' => '%Y-%m-%dT%H', 'YMD' => '%Y-%m-%d', 'YM' => '%Y-%m', 'Y' => '%Y'} hFormats.each do |dateResolution, format| myDateTime = DateTime.strptime(inDateTime, format) rescue false return myDateTime, dateResolution if myDateTime end return nil, 'ERROR' end |
.stringDateFromDateObject(obj) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/adiwg/mdtranslator/internal/module_dateTimeFun.rb', line 162 def self.stringDateFromDateObject(obj) date = obj[:date] if date.nil? date = obj[:dateTime] end dateRes = obj[:dateResolution] unless date.nil? dateStr = stringDateFromDateTime(date, dateRes) end return dateStr end |
.stringDateFromDateTime(myDateTime, dateResolution) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/adiwg/mdtranslator/internal/module_dateTimeFun.rb', line 51 def self.stringDateFromDateTime(myDateTime, dateResolution) s = case dateResolution when 'YMDhmsLZ', 'YMDhmsL', 'YMDhmsZ', 'YMDhms', 'YMDhmZ', 'YMDhm', 'YMDhZ', 'YMDh', 'YMD' myDateTime.strftime('%Y-%m-%d') when 'YM' myDateTime.strftime('%Y-%m') when 'Y' myDateTime.strftime('%Y') else 'ERROR' end return s end |
.stringDateTimeFromDateObject(obj) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/adiwg/mdtranslator/internal/module_dateTimeFun.rb', line 176 def self.stringDateTimeFromDateObject(obj) date = obj[:date] if date.nil? date = obj[:dateTime] end dateRes = obj[:dateResolution] unless date.nil? case dateRes when 'Y', 'YM', 'YMD' dateStr = stringDateFromDateTime(date, dateRes) else dateStr = stringDateTimeFromDateTime(date, dateRes) end end return dateStr end |
.stringDateTimeFromDateTime(myDateTime, dateResolution) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/adiwg/mdtranslator/internal/module_dateTimeFun.rb', line 67 def self.stringDateTimeFromDateTime(myDateTime, dateResolution) s = case dateResolution when 'YMDhmsLZ' myDateTime.strftime('%Y-%m-%dT%H:%M:%S.%L%:z') when 'YMDhmsL' myDateTime.strftime('%Y-%m-%dT%H:%M:%S.%L') when 'YMDhmsZ' myDateTime.strftime('%Y-%m-%dT%H:%M:%S%:z') when 'YMDhms', 'YMD', 'YM', 'Y' myDateTime.strftime('%Y-%m-%dT%H:%M:%S') when 'YMDhmZ' myDateTime.strftime('%Y-%m-%dT%H:%M%:z') when 'YMDhm' myDateTime.strftime('%Y-%m-%dT%H:%M') when 'YMDhZ' myDateTime.strftime('%Y-%m-%dT%H%:z') when 'YMDh' myDateTime.strftime('%Y-%m-%dT%H') else 'ERROR' end return s end |
.stringTimeFromDateTime(myDateTime, dateResolution) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/adiwg/mdtranslator/internal/module_dateTimeFun.rb', line 92 def self.stringTimeFromDateTime(myDateTime, dateResolution) s = case dateResolution when 'YMDhmsLZ' myDateTime.strftime('%H:%M:%S.%L%:z') when 'YMDhmsL' myDateTime.strftime('%H:%M:%S.%L') when 'YMDhmsZ' myDateTime.strftime('%H:%M:%S%:z') when 'YMDhms' myDateTime.strftime('%H:%M:%S') when 'YMDhmZ' myDateTime.strftime('%H:%M%:z') when 'YMDhm' myDateTime.strftime('%H:%M') when 'YMDhZ' myDateTime.strftime('%H%:z') when 'YMDh' myDateTime.strftime('%H') else 'ERROR' end return s end |
.writeDuration(hDuration) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/adiwg/mdtranslator/internal/module_dateTimeFun.rb', line 117 def self.writeDuration(hDuration) # format P[n]Y[n]M[n]DT[n]H[n]M[n]S # example P1DT12H sDuration = 'P' unless hDuration[:years] == 0 sDuration += hDuration[:years].to_s + 'Y' end unless hDuration[:months] == 0 sDuration += hDuration[:months].to_s + 'M' end unless hDuration[:days] == 0 sDuration += hDuration[:days].to_s + 'D' end haveTime = false unless hDuration[:hours] == 0 unless haveTime sDuration += 'T' haveTime = true end sDuration += hDuration[:hours].to_s + 'H' end unless hDuration[:minutes] == 0 unless haveTime sDuration += 'T' haveTime = true end sDuration += hDuration[:minutes].to_s + 'M' end unless hDuration[:seconds] == 0 unless haveTime sDuration += 'T' end sDuration += hDuration[:seconds].to_s + 'S' end return sDuration end |