justinwwhuang commented on code in PR #9135: URL: https://github.com/apache/inlong/pull/9135#discussion_r1374056506
########## inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/utils/file/NewDateUtils.java: ########## @@ -0,0 +1,955 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.inlong.agent.plugin.utils.file; + +import hirondelle.date4j.DateTime; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.StringTokenizer; +import java.util.TimeZone; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class NewDateUtils { + + public static final String FULL_FORMAT = "yyyyMMddHHmmss"; + public static final String NULL_DATA_TIME = "000000000000"; + public static final String DEFAULT_FORMAT = "yyyyMMddHHmm"; + public static final String DEFAULT_TIME_ZONE = "Asia/Shanghai"; + private static final Logger logger = LoggerFactory.getLogger(NewDateUtils.class); + private static final String TIME_REGEX = "YYYY(?:.MM|MM)?(?:.DD|DD)?(?:.hh|hh)?(?:.mm|mm)?(?:" + + ".ss|ss)?"; + private static final String LIMIT_SEP = "(?<=[a-zA-Z])"; + private static final String LETTER_STR = "\\D+"; + private static final String DIGIT_STR = "[0-9]+"; + private static final Pattern pattern = Pattern.compile(TIME_REGEX, + Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); + private static final Pattern bracePatt = Pattern.compile("\\{(.*?)\\}"); + private static final int DEFAULT_LENGTH = "yyyyMMddHHmm".length(); + public static long DAY_TIMEOUT_INTERVAL = 2 * 24 * 3600 * 1000; + public static long HOUR_TIMEOUT_INTERVAL = 2 * 3600 * 1000; + // 数据源配置异常 */ + public static final String DATA_SOURCE_CONFIG_ERROR = "ERROR-0-TDAgent|10001|ERROR" + + "|ERROR_DATA_SOURCE_CONFIG|"; + + /* Return the time in milliseconds for a data time. */ + /* + * public static long getTimeInMillis(String dataTime) { if (dataTime == null) { return 0; } + * + * try { SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_FORMAT); Date date = dateFormat.parse(dataTime); + * + * return date.getTime(); } catch (ParseException e) { return 0; } } + */ + /* Return the format data time string from milliseconds. */ + /* + * public static String getDataTimeFromTimeMillis(long dataTime) { SimpleDateFormat dateFormat = new + * SimpleDateFormat(DEFAULT_FORMAT); return dateFormat.format(new Date(dataTime)); } + */ + /* Return the should start time for a data time log file. */ + public static String getShouldStartTime(String dataTime, String cycleUnit, + String offset) { + if (dataTime == null || dataTime.length() > 12) { + return null; + } + + SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_FORMAT); + TimeZone timeZone = TimeZone.getTimeZone(NewDateUtils.DEFAULT_TIME_ZONE); + dateFormat.setTimeZone(timeZone); + + if (dataTime.length() < DEFAULT_LENGTH) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < DEFAULT_LENGTH - dataTime.length(); i++) { + sb.append("0"); + } + dataTime = dataTime + sb.toString(); + } + + Calendar calendar = Calendar.getInstance(); + try { + calendar.setTimeInMillis(dateFormat.parse(dataTime).getTime()); + } catch (ParseException e) { + return null; + } + + /* + * The delay should be added to the data time, so remove the - from offset. + */ + if (offset.startsWith("-")) { + offset = offset.substring(1, offset.length()); + } else { // 为正,配置提前读取文件 + offset = "-" + offset; + } + + return dateFormat + .format(new Date(getDateTime(calendar, cycleUnit, offset).getTimeInMillis())); + } + + private static Calendar getCurDate(String cycleUnit, String offset) { + if (cycleUnit == null || cycleUnit.length() == 0) { + return null; + } + + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(System.currentTimeMillis()); + + return getDateTime(calendar, cycleUnit, offset); + } + + public static String getDateTime(String dataTime, String cycleUnit, String offset) { + String retTime = NewDateUtils.millSecConvertToTimeStr( + System.currentTimeMillis(), cycleUnit); + try { + long time = NewDateUtils.timeStrConvertTomillSec(dataTime, cycleUnit); + + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(time); + Calendar retCalendar = getDateTime(calendar, cycleUnit, offset); + if (retCalendar == null) { + return dataTime; + } + + retTime = NewDateUtils.millSecConvertToTimeStr(retCalendar.getTime().getTime(), + cycleUnit); + } catch (Exception e) { + logger.error("getDateTime error: ", e); + } + return retTime; + } + + public static String getDateTime(long time, String cycleUnit, String offset) { + // String retTime = NewDateUtils.millSecConvertToTimeStr(System.currentTimeMillis(), + // cycleUnit); + + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(time); + Calendar retCalendar = getDateTime(calendar, cycleUnit, offset); + return NewDateUtils.millSecConvertToTimeStr(retCalendar.getTime().getTime(), cycleUnit); + + // return retTime; + } + + private static Calendar getDateTime(Calendar calendar, String cycleUnit, String offset) { + int cycleNumber = (cycleUnit.length() <= 1 + ? 1 + : Integer.parseInt(cycleUnit.substring(0, cycleUnit.length() - 1))); + + String offsetUnit = offset.substring(offset.length() - 1, offset.length()); + int offsetNumber = Integer.parseInt(offset.substring(0, offset.length() - 1)); + + /* + * For day task, the offset cycle unit can only be day; for hourly task, the offset can't be minute; for + * minutely task, the offset cycle unit can be day, hour and minute, but if the offset cycle unit is minute, the + * offset must be divided by cycle number. + */ + // if (("D".equalsIgnoreCase(cycleUnit) && !"D".equalsIgnoreCase(offsetUnit))) { Review Comment: get -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@inlong.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org