|
35 | 35 | import java.time.LocalDateTime; |
36 | 36 | import java.time.ZoneId; |
37 | 37 | import java.util.Arrays; |
38 | | -import java.util.Calendar; |
39 | | -import java.util.Date; |
40 | 38 | import java.util.TimeZone; |
41 | 39 | import java.util.UUID; |
42 | 40 | import java.util.regex.Matcher; |
@@ -130,83 +128,127 @@ public static TimestampData toTimestamp(String str, String format, String timezo |
130 | 128 | } |
131 | 129 | } |
132 | 130 |
|
133 | | - public static int timestampDiff( |
134 | | - String symbol, |
| 131 | + // Be compatible with the existing definition of Function TIMESTAMP_DIFF |
| 132 | + public static Integer timestampDiff( |
| 133 | + String timeIntervalUnit, |
135 | 134 | LocalZonedTimestampData fromTimestamp, |
136 | | - LocalZonedTimestampData toTimestamp) { |
137 | | - return timestampDiff( |
138 | | - symbol, fromTimestamp.getEpochMillisecond(), toTimestamp.getEpochMillisecond()); |
139 | | - } |
140 | | - |
141 | | - public static int timestampDiff( |
142 | | - String symbol, TimestampData fromTimestamp, TimestampData toTimestamp) { |
143 | | - return timestampDiff(symbol, fromTimestamp.getMillisecond(), toTimestamp.getMillisecond()); |
144 | | - } |
145 | | - |
146 | | - public static int timestampDiff( |
147 | | - String symbol, TimestampData fromTimestamp, LocalZonedTimestampData toTimestamp) { |
148 | | - return timestampDiff( |
149 | | - symbol, fromTimestamp.getMillisecond(), toTimestamp.getEpochMillisecond()); |
| 135 | + LocalZonedTimestampData toTimestamp, |
| 136 | + String timezone) { |
| 137 | + if (fromTimestamp == null || toTimestamp == null) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + return DateTimeUtils.timestampDiff( |
| 141 | + timeIntervalUnit, |
| 142 | + fromTimestamp.getEpochMillisecond(), |
| 143 | + timezone, |
| 144 | + toTimestamp.getEpochMillisecond(), |
| 145 | + timezone); |
| 146 | + } |
| 147 | + |
| 148 | + // Be compatible with the existing definition of Function TIMESTAMP_DIFF |
| 149 | + public static Integer timestampDiff( |
| 150 | + String timeIntervalUnit, |
| 151 | + TimestampData fromTimestamp, |
| 152 | + TimestampData toTimestamp, |
| 153 | + String timezone) { |
| 154 | + if (fromTimestamp == null || toTimestamp == null) { |
| 155 | + return null; |
| 156 | + } |
| 157 | + return DateTimeUtils.timestampDiff( |
| 158 | + timeIntervalUnit, |
| 159 | + fromTimestamp.getMillisecond(), |
| 160 | + "UTC", |
| 161 | + toTimestamp.getMillisecond(), |
| 162 | + "UTC"); |
| 163 | + } |
| 164 | + |
| 165 | + // Be compatible with the existing definition of Function TIMESTAMP_DIFF |
| 166 | + public static Integer timestampDiff( |
| 167 | + String timeIntervalUnit, |
| 168 | + TimestampData fromTimestamp, |
| 169 | + LocalZonedTimestampData toTimestamp, |
| 170 | + String timezone) { |
| 171 | + if (fromTimestamp == null || toTimestamp == null) { |
| 172 | + return null; |
| 173 | + } |
| 174 | + return DateTimeUtils.timestampDiff( |
| 175 | + timeIntervalUnit, |
| 176 | + fromTimestamp.getMillisecond(), |
| 177 | + "UTC", |
| 178 | + toTimestamp.getEpochMillisecond(), |
| 179 | + timezone); |
150 | 180 | } |
151 | 181 |
|
152 | | - public static int timestampDiff( |
153 | | - String symbol, LocalZonedTimestampData fromTimestamp, TimestampData toTimestamp) { |
154 | | - return timestampDiff( |
155 | | - symbol, fromTimestamp.getEpochMillisecond(), toTimestamp.getMillisecond()); |
| 182 | + // Be compatible with the existing definition of Function TIMESTAMP_DIFF |
| 183 | + public static Integer timestampDiff( |
| 184 | + String timeIntervalUnit, |
| 185 | + LocalZonedTimestampData fromTimestamp, |
| 186 | + TimestampData toTimestamp, |
| 187 | + String timezone) { |
| 188 | + if (fromTimestamp == null || toTimestamp == null) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + return DateTimeUtils.timestampDiff( |
| 192 | + timeIntervalUnit, |
| 193 | + fromTimestamp.getEpochMillisecond(), |
| 194 | + timezone, |
| 195 | + toTimestamp.getMillisecond(), |
| 196 | + "UTC"); |
156 | 197 | } |
157 | 198 |
|
158 | | - public static int timestampDiff( |
159 | | - String symbol, ZonedTimestampData fromTimestamp, ZonedTimestampData toTimestamp) { |
160 | | - return timestampDiff(symbol, fromTimestamp.getMillisecond(), toTimestamp.getMillisecond()); |
| 199 | + public static Integer timestampdiff( |
| 200 | + String timeIntervalUnit, |
| 201 | + LocalZonedTimestampData fromTimestamp, |
| 202 | + LocalZonedTimestampData toTimestamp, |
| 203 | + String timezone) { |
| 204 | + return timestampDiff(timeIntervalUnit, fromTimestamp, toTimestamp, timezone); |
161 | 205 | } |
162 | 206 |
|
163 | | - public static int timestampDiff( |
164 | | - String symbol, LocalZonedTimestampData fromTimestamp, ZonedTimestampData toTimestamp) { |
165 | | - return timestampDiff( |
166 | | - symbol, fromTimestamp.getEpochMillisecond(), toTimestamp.getMillisecond()); |
| 207 | + public static Integer timestampdiff( |
| 208 | + String timeIntervalUnit, |
| 209 | + TimestampData fromTimestamp, |
| 210 | + TimestampData toTimestamp, |
| 211 | + String timezone) { |
| 212 | + return timestampDiff(timeIntervalUnit, fromTimestamp, toTimestamp, timezone); |
167 | 213 | } |
168 | 214 |
|
169 | | - public static int timestampDiff( |
170 | | - String symbol, ZonedTimestampData fromTimestamp, LocalZonedTimestampData toTimestamp) { |
171 | | - return timestampDiff( |
172 | | - symbol, fromTimestamp.getMillisecond(), toTimestamp.getEpochMillisecond()); |
| 215 | + public static Integer timestampdiff( |
| 216 | + String timeIntervalUnit, |
| 217 | + TimestampData fromTimestamp, |
| 218 | + LocalZonedTimestampData toTimestamp, |
| 219 | + String timezone) { |
| 220 | + return timestampDiff(timeIntervalUnit, fromTimestamp, toTimestamp, timezone); |
173 | 221 | } |
174 | 222 |
|
175 | | - public static int timestampDiff( |
176 | | - String symbol, TimestampData fromTimestamp, ZonedTimestampData toTimestamp) { |
177 | | - return timestampDiff(symbol, fromTimestamp.getMillisecond(), toTimestamp.getMillisecond()); |
| 223 | + public static Integer timestampdiff( |
| 224 | + String timeIntervalUnit, |
| 225 | + LocalZonedTimestampData fromTimestamp, |
| 226 | + TimestampData toTimestamp, |
| 227 | + String timezone) { |
| 228 | + return timestampDiff(timeIntervalUnit, fromTimestamp, toTimestamp, timezone); |
178 | 229 | } |
179 | 230 |
|
180 | | - public static int timestampDiff( |
181 | | - String symbol, ZonedTimestampData fromTimestamp, TimestampData toTimestamp) { |
182 | | - return timestampDiff(symbol, fromTimestamp.getMillisecond(), toTimestamp.getMillisecond()); |
| 231 | + public static LocalZonedTimestampData timestampadd( |
| 232 | + String timeIntervalUnit, |
| 233 | + Integer interval, |
| 234 | + LocalZonedTimestampData timePoint, |
| 235 | + String timezone) { |
| 236 | + if (interval == null || timePoint == null) { |
| 237 | + return null; |
| 238 | + } |
| 239 | + return LocalZonedTimestampData.fromEpochMillis( |
| 240 | + DateTimeUtils.timestampAdd( |
| 241 | + timeIntervalUnit, interval, timePoint.getEpochMillisecond(), timezone)); |
183 | 242 | } |
184 | 243 |
|
185 | | - public static int timestampDiff(String symbol, long fromDate, long toDate) { |
186 | | - Calendar from = Calendar.getInstance(); |
187 | | - from.setTime(new Date(fromDate)); |
188 | | - Calendar to = Calendar.getInstance(); |
189 | | - to.setTime(new Date(toDate)); |
190 | | - Long second = (to.getTimeInMillis() - from.getTimeInMillis()) / 1000; |
191 | | - switch (symbol) { |
192 | | - case "SECOND": |
193 | | - return second.intValue(); |
194 | | - case "MINUTE": |
195 | | - return second.intValue() / 60; |
196 | | - case "HOUR": |
197 | | - return second.intValue() / 3600; |
198 | | - case "DAY": |
199 | | - return second.intValue() / (24 * 3600); |
200 | | - case "MONTH": |
201 | | - return to.get(Calendar.YEAR) * 12 |
202 | | - + to.get(Calendar.MONDAY) |
203 | | - - (from.get(Calendar.YEAR) * 12 + from.get(Calendar.MONDAY)); |
204 | | - case "YEAR": |
205 | | - return to.get(Calendar.YEAR) - from.get(Calendar.YEAR); |
206 | | - default: |
207 | | - LOG.error("Unsupported timestamp diff: {}", symbol); |
208 | | - throw new RuntimeException("Unsupported timestamp diff: " + symbol); |
| 244 | + public static TimestampData timestampadd( |
| 245 | + String timeIntervalUnit, Integer interval, TimestampData timePoint, String timezone) { |
| 246 | + if (interval == null || timePoint == null) { |
| 247 | + return null; |
209 | 248 | } |
| 249 | + return TimestampData.fromMillis( |
| 250 | + DateTimeUtils.timestampAdd( |
| 251 | + timeIntervalUnit, interval, timePoint.getMillisecond(), "UTC")); |
210 | 252 | } |
211 | 253 |
|
212 | 254 | public static boolean betweenAsymmetric(String value, String minValue, String maxValue) { |
|
0 commit comments