|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.inlong.sdk.sort.util; |
| 20 | + |
| 21 | +import org.apache.inlong.sdk.sort.entity.InLongTopic; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import java.time.LocalDateTime; |
| 26 | +import java.time.ZoneId; |
| 27 | +import java.time.format.DateTimeFormatter; |
| 28 | +import java.util.Optional; |
| 29 | + |
| 30 | +public class TimeUtil { |
| 31 | + private static final Logger logger = LoggerFactory.getLogger(TimeUtil.class); |
| 32 | + private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| 33 | + private static final String KEY_SDK_START_TIME = "sortSdk.startTime"; |
| 34 | + private static final String KEY_SDK_STOP_TIME = "sortSdk.stopTime"; |
| 35 | + private static final long DEFAULT_START_TIME = -1L; |
| 36 | + private static final long DEFAULT_STOP_TIME = Long.MAX_VALUE; |
| 37 | + |
| 38 | + public static long parseStartTime(InLongTopic inLongTopic) { |
| 39 | + return Optional.ofNullable(inLongTopic.getProperties().get(KEY_SDK_START_TIME)) |
| 40 | + .map(s -> { |
| 41 | + try { |
| 42 | + LocalDateTime time = LocalDateTime.parse(s.toString(), DATE_FORMAT); |
| 43 | + return LocalDateTime.from(time).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
| 44 | + } catch (Throwable t) { |
| 45 | + logger.error("parse start time failed, plz check the format of start time : {}", s, t); |
| 46 | + } |
| 47 | + return DEFAULT_START_TIME; |
| 48 | + }) |
| 49 | + .orElse(DEFAULT_START_TIME); |
| 50 | + } |
| 51 | + |
| 52 | + public static long parseStopTime(InLongTopic inLongTopic) { |
| 53 | + return Optional.ofNullable(inLongTopic.getProperties().get(KEY_SDK_STOP_TIME)) |
| 54 | + .map(s -> { |
| 55 | + try { |
| 56 | + LocalDateTime time = LocalDateTime.parse(s.toString(), DATE_FORMAT); |
| 57 | + return LocalDateTime.from(time).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
| 58 | + } catch (Throwable t) { |
| 59 | + logger.error("parse start time failed, plz check the format of stop time : {}", s, t); |
| 60 | + } |
| 61 | + return DEFAULT_STOP_TIME; |
| 62 | + }) |
| 63 | + .orElse(DEFAULT_STOP_TIME); |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments