Skip to content

Commit

Permalink
Add dateutil (#48)
Browse files Browse the repository at this point in the history
Change-Id: I783d4ba2af19114dafd921c4468d4dbb640964c1
  • Loading branch information
zhoney authored Jun 15, 2020
1 parent c2c5ba4 commit 034e1a2
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.7.6</version>
<version>1.7.7</version>

<name>hugegraph-common</name>
<url>https://github.com/hugegraph/hugegraph-common</url>
Expand Down Expand Up @@ -260,7 +260,7 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
<Implementation-Version>1.7.6.0</Implementation-Version>
<Implementation-Version>1.7.7.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/com/baidu/hugegraph/util/DateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.util;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.baidu.hugegraph.date.SafeDateFormat;
import com.google.common.collect.ImmutableMap;

public final class DateUtil {

public static final Date DATE_ZERO = new Date(0L);

private static final Map<String, String> VALID_DFS = ImmutableMap.of(
"^\\d{4}-\\d{1,2}-\\d{1,2}",
"yyyy-MM-dd",
"^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}",
"yyyy-MM-dd HH:mm:ss",
"^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}\\.\\d{1,3}",
"yyyy-MM-dd HH:mm:ss.SSS"
);

private static final Map<String, SafeDateFormat> DATE_FORMATS =
new ConcurrentHashMap<>();

public static Date parse(String value) {
for (Map.Entry<String, String> entry : VALID_DFS.entrySet()) {
if (value.matches(entry.getKey())) {
try {
return parse(value, entry.getValue());
} catch (ParseException e) {
throw new IllegalArgumentException(String.format(
"%s, expect format: %s",
e.getMessage(), entry.getValue()));
}
}
}
throw new IllegalArgumentException(String.format(
"Expected date format is: %s, but got '%s'",
VALID_DFS.values(), value));
}

public static Date parse(String value, String df) throws ParseException {
SafeDateFormat dateFormat = getDateFormat(df);
return dateFormat.parse(value);
}

public static Date now() {
return new Date();
}

private static SafeDateFormat getDateFormat(String df) {
SafeDateFormat dateFormat = DATE_FORMATS.get(df);
if (dateFormat == null) {
dateFormat = new SafeDateFormat(df);
/*
* Specify whether or not date/time parsing is to be lenient.
* With lenient parsing, the parser may use heuristics to interpret
* inputs that do not precisely match this object's format.
* With strict parsing, inputs must match this object's format.
*/
dateFormat.setLenient(false);
SafeDateFormat previous = DATE_FORMATS.putIfAbsent(df, dateFormat);
if (previous != null) {
dateFormat = previous;
}
}
return dateFormat;
}

public static Object toPattern(String df) {
SafeDateFormat dateFormat = getDateFormat(df);
return dateFormat.toPattern();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public class CommonVersion {

// The second parameter of Version.of() is for all-in-one JAR
public static final Version VERSION = Version.of(CommonVersion.class,
"1.7.6");
"1.7.7");
}
3 changes: 3 additions & 0 deletions src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.baidu.hugegraph.unit.rest.RestResultTest;
import com.baidu.hugegraph.unit.util.BytesTest;
import com.baidu.hugegraph.unit.util.CollectionUtilTest;
import com.baidu.hugegraph.unit.util.DateUtilTest;
import com.baidu.hugegraph.unit.util.HashUtilTest;
import com.baidu.hugegraph.unit.util.InsertionOrderUtilTest;
import com.baidu.hugegraph.unit.util.LongEncodingTest;
Expand All @@ -57,6 +58,7 @@
import com.baidu.hugegraph.unit.util.TimeUtilTest;
import com.baidu.hugegraph.unit.util.VersionUtilTest;
import com.baidu.hugegraph.unit.version.VersionTest;
import com.baidu.hugegraph.util.DateUtil;

@RunWith(Suite.class)
@Suite.SuiteClasses({
Expand Down Expand Up @@ -90,6 +92,7 @@
VersionUtilTest.class,
LongEncodingTest.class,
OrderLimitMapTest.class,
DateUtilTest.class,

ExtraParamTest.class,
LicenseCreateParamTest.class,
Expand Down
75 changes: 75 additions & 0 deletions src/test/java/com/baidu/hugegraph/unit/util/DateUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.unit.util;

import java.util.Date;

import org.junit.Test;

import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.unit.BaseUnitTest;
import com.baidu.hugegraph.util.DateUtil;

public class DateUtilTest extends BaseUnitTest {

@Test
public void testParse() {
Date date1 = DateUtil.parse("2020-06-12 12:00:00");
Date date2 = DateUtil.parse("2020-06-13");
Assert.assertTrue(date1.before(date2));

Assert.assertThrows(IllegalArgumentException.class, () -> {
DateUtil.parse("2018-");
}, e -> {
Assert.assertContains("Expected date format is:",
e.getMessage());
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
DateUtil.parse("2018-15-07 12:00:00");
}, e -> {
Assert.assertContains(", expect format: ", e.getMessage());
});
}

@Test
public void testNow() {
Date date1 = DateUtil.now();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore
}
Date date2 = DateUtil.now();
Assert.assertTrue(date1.before(date2));
}

@Test
public void testToPattern() {
Object pattern = DateUtil.toPattern("yyyyMMdd HH:mm:ss.SSS");
Assert.assertEquals("yyyyMMdd HH:mm:ss.SSS", pattern);

Assert.assertThrows(IllegalArgumentException.class, () -> {
DateUtil.toPattern("iyyyyMMdd");
}, e -> {
Assert.assertContains("Illegal pattern character 'i'", e.getMessage());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testClasses() throws IOException {
@SuppressWarnings("unchecked")
List<ClassInfo> classes = IteratorUtils.toList(ReflectionUtil.classes(
"com.baidu.hugegraph.util"));
Assert.assertEquals(15, classes.size());
Assert.assertEquals(16, classes.size());
classes.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
Assert.assertEquals("com.baidu.hugegraph.util.Bytes",
classes.get(0).getName());
Expand All @@ -100,7 +100,7 @@ public void testClasses() throws IOException {
Assert.assertEquals("com.baidu.hugegraph.util.CollectionUtil",
classes.get(2).getName());
Assert.assertEquals("com.baidu.hugegraph.util.VersionUtil",
classes.get(14).getName());
classes.get(15).getName());
}

@Test
Expand Down

0 comments on commit 034e1a2

Please sign in to comment.