Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Jul 4, 2024
1 parent 12a2c24 commit 5b55ecf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
97 changes: 53 additions & 44 deletions rxlib/src/main/java/org/rx/bean/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,51 +39,58 @@ 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;
}
}
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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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() {
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion rxlib/src/main/java/org/rx/io/EntityDatabaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rxlib/src/main/java/org/rx/net/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions rxlib/src/main/resources/rx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b55ecf

Please sign in to comment.