From 5b55ecfc9b47e83e4c5ecf4e4291d5fcf489de16 Mon Sep 17 00:00:00 2001 From: wxm <115806199+youfanx@users.noreply.github.com> Date: Thu, 4 Jul 2024 18:58:38 +0800 Subject: [PATCH] up --- rxlib/src/main/java/org/rx/bean/DateTime.java | 97 ++++++++++--------- .../java/org/rx/io/EntityDatabaseImpl.java | 2 +- .../main/java/org/rx/net/http/HttpClient.java | 2 +- rxlib/src/main/resources/rx.yml | 4 +- 4 files changed, 57 insertions(+), 48 deletions(-) diff --git a/rxlib/src/main/java/org/rx/bean/DateTime.java b/rxlib/src/main/java/org/rx/bean/DateTime.java index 72ed1739..15842c1b 100644 --- a/rxlib/src/main/java/org/rx/bean/DateTime.java +++ b/rxlib/src/main/java/org/rx/bean/DateTime.java @@ -5,6 +5,7 @@ import org.apache.commons.lang3.time.FastDateFormat; import org.rx.annotation.ErrorCode; import org.rx.core.RxConfig; +import org.rx.core.Strings; import org.rx.exception.ApplicationException; import java.text.ParseException; @@ -38,40 +39,43 @@ public final class DateTime extends Date { static final TimeZone UTC_ZONE = TimeZone.getTimeZone("UTC"); public static DateTime now() { - return new DateTime(System.currentTimeMillis()); - } - - public static DateTime now(String format) { - return valueOf(now().toString(format), format); + return new DateTime(System.currentTimeMillis(), TimeZone.getDefault()); } public static DateTime utcNow() { - return now().asUniversalTime(); + return new DateTime(System.currentTimeMillis(), UTC_ZONE); } public static DateTime ofToNull(Date d) { - return d != null ? new DateTime(d.getTime()) : null; + return d != null ? of(d) : null; } public static DateTime of(@NonNull Date d) { - return new DateTime(d.getTime()); + return d instanceof DateTime ? (DateTime) d : new DateTime(d.getTime(), TimeZone.getDefault()); } - @ErrorCode(cause = ParseException.class) public static DateTime valueOf(@NonNull String dateString) { + return valueOf(dateString, (TimeZone) null); + } + + @ErrorCode(cause = ParseException.class) + public static DateTime valueOf(@NonNull String dateString, TimeZone timeZone) { + if (Strings.isNumeric(dateString)) { + return new DateTime(Long.parseLong(dateString), timeZone); + } Throwable lastEx = null; int offset = dateString.length() >= 23 ? 0 : 3; int len = offset + 3, fb = 6; for (int i = offset; i < len; i++) { try { - return valueOf(dateString, FORMATS[i]); + return valueOf(dateString, FORMATS[i], timeZone); } catch (Throwable ex) { lastEx = ex; } } for (int i = fb; i < FORMATS.length; i++) { try { - return valueOf(dateString, FORMATS[i]); + return valueOf(dateString, FORMATS[i], timeZone); } catch (Throwable ex) { lastEx = ex; } @@ -79,10 +83,14 @@ public static DateTime valueOf(@NonNull String dateString) { throw new ApplicationException(values(String.join(",", FORMATS), dateString), lastEx); } - @SneakyThrows public static DateTime valueOf(String dateString, String format) { + return valueOf(dateString, format, null); + } + + @SneakyThrows + public static DateTime valueOf(String dateString, String format, TimeZone timeZone) { //SimpleDateFormat not thread safe - return DateTime.of(FastDateFormat.getInstance(format).parse(dateString)); + return DateTime.of(FastDateFormat.getInstance(format, timeZone).parse(dateString)); } private Calendar calendar; @@ -177,14 +185,20 @@ public double getTotalMilliseconds() { return super.getTime(); } - public DateTime(int year, Month month, int day, int hour, int minute, int second) { + public DateTime(int year, Month month, int day, int hour, int minute, int second, TimeZone zone) { Calendar c = getCalendar(); c.set(year, month.getValue() - 1, day, hour, minute, second); super.setTime(c.getTimeInMillis()); + if (zone != null) { + c.setTimeZone(zone); + } } - public DateTime(long ticks) { + public DateTime(long ticks, TimeZone zone) { super(ticks); + if (zone != null) { + getCalendar().setTimeZone(zone); + } } @Override @@ -195,34 +209,43 @@ public void setTime(long time) { } } + public void setTimeZone(TimeZone zone) { + getCalendar().setTimeZone(zone); + } + + public TimeZone getTimeZone() { + return getCalendar().getTimeZone(); + } + public DateTime getDatePart() { - return new DateTime(getYear(), getMonthEnum(), getDay(), 0, 0, 0); + return new DateTime(getYear(), getMonthEnum(), getDay(), 0, 0, 0, getTimeZone()); } public DateTime setDatePart(int year, Month month, int day) { - return new DateTime(year, month, day, getHours(), getMinutes(), getSeconds()); + return new DateTime(year, month, day, getHours(), getMinutes(), getSeconds(), getTimeZone()); } public DateTime setDatePart(String date) { - return DateTime.valueOf(toString(date + " HH:mm:ss"), DATE_TIME_FORMAT); + return DateTime.valueOf(toString(date + " HH:mm:ss"), DATE_TIME_FORMAT, getTimeZone()); } public DateTime getTimePart() { - return new DateTime(1970, Month.JANUARY, 1, getHours(), getMinutes(), getSeconds()); + return new DateTime(1970, Month.JANUARY, 1, getHours(), getMinutes(), getSeconds(), getTimeZone()); } public DateTime setTimePart(int hour, int minute, int second) { - return new DateTime(getYear(), getMonthEnum(), getDay(), hour, minute, second); + return new DateTime(getYear(), getMonthEnum(), getDay(), hour, minute, second, getTimeZone()); } public DateTime setTimePart(String time) { - return DateTime.valueOf(toString("yyy-MM-dd " + time), DATE_TIME_FORMAT); + return DateTime.valueOf(toString("yyy-MM-dd " + time), DATE_TIME_FORMAT, getTimeZone()); } - public boolean isToday() { - DateTime n = DateTime.now(); - return n.getYear() == getYear() && n.getMonth() == getMonth() && n.getDay() == getDay(); - } +// public boolean isToday() { +// DateTime n = DateTime.now(); +// n.setTimeZone(getTimeZone()); +// return n.getYear() == getYear() && n.getMonth() == getMonth() && n.getDay() == getDay(); +// } public DateTime addYears(int value) { return add(Calendar.YEAR, value); @@ -269,9 +292,7 @@ private DateTime add(int field, int value) { long mark = c.getTimeInMillis(); c.set(field, c.get(field) + value); try { - DateTime newVal = new DateTime(c.getTimeInMillis()); - newVal.getCalendar().setTimeZone(getCalendar().getTimeZone()); - return newVal; + return new DateTime(c.getTimeInMillis(), getTimeZone()); } finally { c.setTimeInMillis(mark); } @@ -282,30 +303,18 @@ private DateTime set(int field, int value) { long mark = c.getTimeInMillis(); c.set(field, value); try { - DateTime newVal = new DateTime(c.getTimeInMillis()); - newVal.getCalendar().setTimeZone(getCalendar().getTimeZone()); - return newVal; + return new DateTime(c.getTimeInMillis(), getTimeZone()); } finally { c.setTimeInMillis(mark); } } public DateTime add(@NonNull Date value) { - return new DateTime(super.getTime() + value.getTime()); + return new DateTime(super.getTime() + value.getTime(), getTimeZone()); } public DateTime subtract(@NonNull Date value) { - return new DateTime(super.getTime() - value.getTime()); - } - - public DateTime asLocalTime() { - getCalendar().setTimeZone(TimeZone.getDefault()); - return this; - } - - public DateTime asUniversalTime() { - getCalendar().setTimeZone(UTC_ZONE); - return this; + return new DateTime(super.getTime() - value.getTime(), getTimeZone()); } public String toDateString() { @@ -326,6 +335,6 @@ public String toString() { } public String toString(@NonNull String format) { - return FastDateFormat.getInstance(format, getCalendar().getTimeZone()).format(this); + return FastDateFormat.getInstance(format, getTimeZone()).format(this); } } diff --git a/rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java b/rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java index 3064e30f..24590790 100644 --- a/rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java +++ b/rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java @@ -202,7 +202,7 @@ public void clearTimeRollingFiles() { if (p.startsWith("~/")) { p = Sys.USER_HOME + p.substring(1); } - Files.deleteBefore(Files.getFullPath(p), DateTime.now(timeRollingPattern).addHours(-rollingHours), "*.mv.db"); + Files.deleteBefore(Files.getFullPath(p), DateTime.valueOf(DateTime.now().toString(timeRollingPattern), timeRollingPattern).addHours(-rollingHours), "*.mv.db"); } //region CRUD diff --git a/rxlib/src/main/java/org/rx/net/http/HttpClient.java b/rxlib/src/main/java/org/rx/net/http/HttpClient.java index e40a2cb1..01d98f5d 100644 --- a/rxlib/src/main/java/org/rx/net/http/HttpClient.java +++ b/rxlib/src/main/java/org/rx/net/http/HttpClient.java @@ -385,7 +385,7 @@ static OkHttpClient createClient(long connectTimeoutMillis, long readWriteTimeou .connectTimeout(connectTimeoutMillis, TimeUnit.MILLISECONDS) .readTimeout(readWriteTimeoutMillis, TimeUnit.MILLISECONDS) .writeTimeout(readWriteTimeoutMillis, TimeUnit.MILLISECONDS) - .callTimeout(readWriteTimeoutMillis * 2, TimeUnit.MILLISECONDS) + .callTimeout(Math.round(readWriteTimeoutMillis * 1.5), TimeUnit.MILLISECONDS) .proxy(proxy).proxyAuthenticator(authenticator); if (enableCookie) { builder.cookieJar(COOKIES); diff --git a/rxlib/src/main/resources/rx.yml b/rxlib/src/main/resources/rx.yml index 1633c3ed..b644986d 100644 --- a/rxlib/src/main/resources/rx.yml +++ b/rxlib/src/main/resources/rx.yml @@ -30,8 +30,8 @@ app: net: reactorThreadAmount: 0 enableLog: false - connectTimeoutMillis: 15000 - readWriteTimeoutMillis: 60000 + connectTimeoutMillis: 10000 + readWriteTimeoutMillis: 15000 poolMaxSize: 0 poolKeepAliveSeconds: 120 userAgent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/4.0.1301.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2875.116 Safari/537.36 NetType/WIFI MicroMessenger/7.0.5 WindowsWechat