提交 00a1b0e2 authored 作者: Thomas Mueller Graf's avatar Thomas Mueller Graf

Formatting

上级 7e8fa541
/* /*
* Copyright 2004-2016 H2 Group. Multiple-Licensed under the MPL 2.0, * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html). * and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: Daniel Gredler * Initial Developer: Daniel Gredler
*/ */
...@@ -26,8 +26,9 @@ public class ToDateParser { ...@@ -26,8 +26,9 @@ public class ToDateParser {
/** /**
* @param input the input date with the date-time info * @param input the input date with the date-time info
* @param format the format of date-time info * @param format the format of date-time info
* @param functionName one of [TO_DATE, TO_TIMESTAMP] (both share the same code) * @param functionName one of [TO_DATE, TO_TIMESTAMP] (both share the same
* code)
*/ */
private ToDateParser(ConfigParam functionName, String input, String format) { private ToDateParser(ConfigParam functionName, String input, String format) {
// reset calendar - default oracle behaviour // reset calendar - default oracle behaviour
...@@ -72,7 +73,8 @@ public class ToDateParser { ...@@ -72,7 +73,8 @@ public class ToDateParser {
private Timestamp getResultingTimestamp() { private Timestamp getResultingTimestamp() {
Calendar cal = (Calendar) getResultCalendar().clone(); Calendar cal = (Calendar) getResultCalendar().clone();
int nanosToSet = nanos == null ? cal.get(Calendar.MILLISECOND) * 1000000 : nanos.intValue(); int nanosToSet = nanos == null ?
cal.get(Calendar.MILLISECOND) * 1000000 : nanos.intValue();
cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.MILLISECOND, 0);
Timestamp ts = new Timestamp(cal.getTimeInMillis()); Timestamp ts = new Timestamp(cal.getTimeInMillis());
ts.setNanos(nanosToSet); ts.setNanos(nanosToSet);
...@@ -114,7 +116,8 @@ public class ToDateParser { ...@@ -114,7 +116,8 @@ public class ToDateParser {
private static ToDateParser parse(ToDateParser p) { private static ToDateParser parse(ToDateParser p) {
while (p.hasToParseData()) { while (p.hasToParseData()) {
List<ToDateTokenizer.FormatTokenEnum> tokenList = ToDateTokenizer.FormatTokenEnum.getTokensInQuestion(p.getFormatStr()); List<ToDateTokenizer.FormatTokenEnum> tokenList =
ToDateTokenizer.FormatTokenEnum.getTokensInQuestion(p.getFormatStr());
if (tokenList.isEmpty()) { if (tokenList.isEmpty()) {
p.removeFirstChar(); p.removeFirstChar();
continue; continue;
...@@ -154,7 +157,8 @@ public class ToDateParser { ...@@ -154,7 +157,8 @@ public class ToDateParser {
int currentFormatPos = orgFormatLen - formatStr.length(); int currentFormatPos = orgFormatLen - formatStr.length();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(format("\n %s('%s', '%s')", functionName, unmodifiedInputStr, unmodifiedFormatStr)); sb.append(format("\n %s('%s', '%s')", functionName,
unmodifiedInputStr, unmodifiedFormatStr));
sb.append(format("\n %s^%s , %s^ <-- Parsing failed at this point", sb.append(format("\n %s^%s , %s^ <-- Parsing failed at this point",
format("%" + (functionName.name().length() + currentInputPos) + "s", ""), format("%" + (functionName.name().length() + currentInputPos) + "s", ""),
restInputLen <= 0 ? "" : format("%" + restInputLen + "s", ""), restInputLen <= 0 ? "" : format("%" + restInputLen + "s", ""),
......
...@@ -5,17 +5,14 @@ ...@@ -5,17 +5,14 @@
*/ */
package org.h2.samples; package org.h2.samples;
import org.h2.tools.DeleteDbFiles;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.Statement; import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale;
import java.util.TimeZone; import org.h2.tools.DeleteDbFiles;
/** /**
* A very simple class that shows how to load the driver, create a database, * A very simple class that shows how to load the driver, create a database,
...@@ -37,11 +34,20 @@ public class ToDate { ...@@ -37,11 +34,20 @@ public class ToDate {
Connection conn = DriverManager.getConnection("jdbc:h2:~/test"); Connection conn = DriverManager.getConnection("jdbc:h2:~/test");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table ToDateTest(id int primary key, start_date datetime, end_date datetime)"); stat.execute("create table ToDateTest(id int primary key, " +
stat.execute("insert into ToDateTest values(1, TO_DATE('2015-11-13', 'yyyy-MM-DD'), TO_DATE('2015-12-15', 'YYYY-MM-DD'))"); "start_date datetime, end_date datetime)");
stat.execute("insert into ToDateTest values(2, TO_DATE('2015-12-12 00:00:00', 'yyyy-MM-DD HH24:MI:ss'), TO_DATE('2015-12-16 15:00:00', 'YYYY-MM-DD HH24:MI:ss'))"); stat.execute("insert into ToDateTest values(1, " +
stat.execute("insert into ToDateTest values(3, TO_DATE('2015-12-12 08:00 A.M.', 'yyyy-MM-DD HH:MI AM'), TO_DATE('2015-12-17 08:00 P.M.', 'YYYY-MM-DD HH:MI AM'))"); "TO_DATE('2015-11-13', 'yyyy-MM-DD'), " +
stat.execute("insert into ToDateTest values(4, TO_DATE(substr('2015-12-12 08:00 A.M.', 1, 10), 'yyyy-MM-DD'), TO_DATE('2015-12-17 08:00 P.M.', 'YYYY-MM-DD HH:MI AM'))"); "TO_DATE('2015-12-15', 'YYYY-MM-DD'))");
stat.execute("insert into ToDateTest values(2, " +
"TO_DATE('2015-12-12 00:00:00', 'yyyy-MM-DD HH24:MI:ss'), " +
"TO_DATE('2015-12-16 15:00:00', 'YYYY-MM-DD HH24:MI:ss'))");
stat.execute("insert into ToDateTest values(3, " +
"TO_DATE('2015-12-12 08:00 A.M.', 'yyyy-MM-DD HH:MI AM'), " +
"TO_DATE('2015-12-17 08:00 P.M.', 'YYYY-MM-DD HH:MI AM'))");
stat.execute("insert into ToDateTest values(4, " +
"TO_DATE(substr('2015-12-12 08:00 A.M.', 1, 10), 'yyyy-MM-DD'), " +
"TO_DATE('2015-12-17 08:00 P.M.', 'YYYY-MM-DD HH:MI AM'))");
ResultSet rs = stat.executeQuery("select * from ToDateTest"); ResultSet rs = stat.executeQuery("select * from ToDateTest");
while (rs.next()) { while (rs.next()) {
......
...@@ -1337,7 +1337,8 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -1337,7 +1337,8 @@ public class TestFunctions extends TestBase implements AggregateFunction {
assertEquals(date, ToDateParser.toDate("979", "YYY")); assertEquals(date, ToDateParser.toDate("979", "YYY"));
assertEquals(date, ToDateParser.toDate("979", "IYY")); assertEquals(date, ToDateParser.toDate("979", "IYY"));
// Gregorian calendar does not have a year 0. 0 = 0001 BC, -1 = 0002 BC, ... so we adjust // Gregorian calendar does not have a year 0.
// 0 = 0001 BC, -1 = 0002 BC, ... so we adjust
date = new SimpleDateFormat("yyy").parse("-99"); date = new SimpleDateFormat("yyy").parse("-99");
setMonth(date, month); setMonth(date, month);
assertEquals(date, ToDateParser.toDate("0100 BC", "YYYY BC")); assertEquals(date, ToDateParser.toDate("0100 BC", "YYYY BC"));
...@@ -1346,7 +1347,8 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -1346,7 +1347,8 @@ public class TestFunctions extends TestBase implements AggregateFunction {
assertEquals(date, ToDateParser.toDate("-0100", "SYYYY")); assertEquals(date, ToDateParser.toDate("-0100", "SYYYY"));
assertEquals(date, ToDateParser.toDate("-0100", "YYYY")); assertEquals(date, ToDateParser.toDate("-0100", "YYYY"));
// Gregorian calendar does not have a year 0. 0 = 0001 BC, -1 = 0002 BC, ... so we adjust // Gregorian calendar does not have a year 0.
// 0 = 0001 BC, -1 = 0002 BC, ... so we adjust
date = new SimpleDateFormat("y").parse("0"); date = new SimpleDateFormat("y").parse("0");
setMonth(date, month); setMonth(date, month);
assertEquals(date, ToDateParser.toDate("01 BC", "YY BC")); assertEquals(date, ToDateParser.toDate("01 BC", "YY BC"));
......
...@@ -308,9 +308,9 @@ public class BuildBase { ...@@ -308,9 +308,9 @@ public class BuildBase {
protected int execScript(String script, StringList args) { protected int execScript(String script, StringList args) {
if (isWindows()) { if (isWindows()) {
// Under windows, we use the "cmd" command interpreter since it will // Under windows, we use the "cmd" command interpreter since it will
// search the path for us without us having to hard-code an extension // search the path for us without us having to hard-code an
// for the script we want. (Sometimes we don't know if the extension // extension for the script we want. (Sometimes we don't know if the
// will be .bat or .cmd) // extension will be .bat or .cmd)
StringList newArgs = new StringList(); StringList newArgs = new StringList();
newArgs.add("/C"); newArgs.add("/C");
newArgs.add(script); newArgs.add(script);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论