Skip to content

Commit

Permalink
Fix issue nutzam#524
Browse files Browse the repository at this point in the history
  • Loading branch information
zozoh committed Sep 17, 2013
1 parent 4379c22 commit bccb01a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
40 changes: 34 additions & 6 deletions src/org/nutz/lang/Times.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ public static Date now() {
}

private static Pattern _P_TIME = Pattern.compile("^((\\d{2,4})([/\\\\-])(\\d{1,2})([/\\\\-])(\\d{1,2}))?"
+ "(([ ])?"
+ "(([ T])?"
+ "(\\d{1,2})(:)(\\d{1,2})(:)(\\d{1,2})"
+ "(([.])"
+ "(\\d{1,}))?)?$");
+ "(\\d{1,}))?)?"
+ "(([+-])(\\d{1,2})(:\\d{1,2})?)?"
+ "$");

/**
* 根据默认时区计算时间字符串的绝对毫秒数
Expand All @@ -101,10 +103,10 @@ public static Date now() {
* 时间字符串
* @return 绝对毫秒数
*
* @see #ms(String, TimeZone)
* @see #ams(String, TimeZone)
*/
public static long ams(String ds) {
return ms(ds, TimeZone.getDefault());
return ams(ds, null);
}

/**
Expand All @@ -130,7 +132,7 @@ public static long ams(String ds) {
* 你给定的时间字符串是属于哪个时区的
* @return 时间
*/
public static long ms(String ds, TimeZone tz) {
public static long ams(String ds, TimeZone tz) {
Matcher m = _P_TIME.matcher(ds);
if (m.find()) {
int yy = _int(m, 2, 1970);
Expand All @@ -148,11 +150,37 @@ public static long ms(String ds, TimeZone tz) {
MS += (((long) HH) * 3600L + ((long) mm) * 60L + ss) * 1000L;
MS += (long) ms;

return MS - tz.getRawOffset();
// 如果没有指定时区,那么用字符串中带有的时区信息,如果依然木有,则用系统默认时区
long tzOffset;
if (null == tz) {
if (!Strings.isBlank(m.group(17))) {
tzOffset = Long.parseLong(m.group(19))
* 3600000L
* (m.group(18).charAt(0) == '-' ? -1 : 1);

} else {
tzOffset = TimeZone.getDefault().getRawOffset();
}
}
// 采用指定的时区
else {
tzOffset = tz.getRawOffset();
}
// 计算
return MS - tzOffset;
}
throw Lang.makeThrow("Unexpect date format '%s'", ds);
}

/**
* 这个接口函数是 1.b.49 提供了,下一版本将改名为 ams,预计在版本 1.b.51 之后被移除
*
* @deprecated since 1.b.49 util 1.b.51
*/
public static long ms(String ds, TimeZone tz) {
return ams(ds, tz);
}

/**
* 返回时间对象在一天中的毫秒数
*
Expand Down
35 changes: 31 additions & 4 deletions test/org/nutz/lang/TimesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ private static void AD(String b, String e, Date[] ds) {
assertEquals(e, Times.sDT(ds[1]));
}

/**
* For issue #524
*/
@Test
public void test_with_timezone() {
long ms0 = Times.ams("2013-09-14T12:33:14+08:00");
long ms1 = Times.ams("2013-09-14T12:33:14-08:00");
assertEquals(16 * 3600 * 1000, ms1 - ms0);
}

/**
* For issue #524
*/
@Test
public void test_sep_by_T() {
Date d0 = Times.D("2013-09-14 12:33:14");
Date d1 = Times.D("2013-09-14T12:33:14");
assertEquals(d0.getTime(), d1.getTime());
}

@Test
public void test_1940() {
Date d = Times.D("1940-8-15");
Expand All @@ -22,7 +42,8 @@ public void test_1940() {
@Test
public void test_d() {
Date d = new Date(System.currentTimeMillis());
assertEquals(Times.now().getTime() / 1000, Times.D(Times.sDT(d)).getTime() / 1000);
assertEquals(Times.now().getTime() / 1000, Times.D(Times.sDT(d))
.getTime() / 1000);
}

@Test
Expand All @@ -35,13 +56,19 @@ public void test_ztask_weeks() {
AD("2012-02-12 00:00:00", "2012-02-18 23:59:59", Times.week(base, 1));
AD("2012-02-19 00:00:00", "2012-02-25 23:59:59", Times.week(base, 2));

AD("2012-01-22 00:00:00", "2012-02-11 23:59:59", Times.weeks(base, -2, 0));
AD("2012-01-22 00:00:00", "2012-02-25 23:59:59", Times.weeks(base, -2, 2));
AD("2012-01-22 00:00:00",
"2012-02-11 23:59:59",
Times.weeks(base, -2, 0));
AD("2012-01-22 00:00:00",
"2012-02-25 23:59:59",
Times.weeks(base, -2, 2));

// 测测跨年
base = Times.D("2012-01-04 17:35:12").getTime();

AD("2011-12-25 00:00:00", "2012-01-14 23:59:59", Times.weeks(base, 1, -1));
AD("2011-12-25 00:00:00",
"2012-01-14 23:59:59",
Times.weeks(base, 1, -1));
}

}

0 comments on commit bccb01a

Please sign in to comment.