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