-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathDateTimeTest.java
More file actions
150 lines (122 loc) · 4.95 KB
/
DateTimeTest.java
File metadata and controls
150 lines (122 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.sqlite;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringTokenizer;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/*
* Created by Alexander Galanin <al@galanin.nnov.ru>
*/
public class DateTimeTest
{
// Mon May 23 06:06:21.123 MSK 2016 = Mon May 23 03:06:21.123 GMT 2016
private static final long DATETIME_UNIX = 1463972781L;
private static final long DATETIME_MILLISECONDS = 123;
private static final long DATETIME_UNIX_HIPRECISION = DATETIME_UNIX * 1000 + DATETIME_MILLISECONDS;
private static final Date DATETIME = new Date(DATETIME_UNIX * 1000);
private static final Date DATETIME_HIPRECISION = new Date(DATETIME_UNIX_HIPRECISION);
private Connection conn;
private PreparedStatement stat;
@After
public void close() throws SQLException {
stat.close();
conn.close();
}
@Test
public void setDateUnix() throws SQLException {
SQLiteConfig config = new SQLiteConfig();
config.setReadOnly(true);
config.setDateClass("INTEGER");
config.setDatePrecision("SECONDS");
conn = DriverManager.getConnection("jdbc:sqlite:", config.toProperties());
stat = conn.prepareStatement("select strftime('%s', ?, 'unixepoch')");
stat.setDate(1, DATETIME);
ResultSet rs = stat.executeQuery();
assertTrue(rs.next());
assertEquals(rs.getLong(1), DATETIME_UNIX);
rs.close();
}
@Test
public void setDateJulian() throws SQLException {
SQLiteConfig config = new SQLiteConfig();
config.setReadOnly(true);
config.setDateClass("REAL");
conn = DriverManager.getConnection("jdbc:sqlite:", config.toProperties());
stat = conn.prepareStatement("select strftime('%s', ?)");
stat.setDate(1, DATETIME);
ResultSet rs = stat.executeQuery();
assertTrue(rs.next());
assertEquals(rs.getLong(1), DATETIME_UNIX);
rs.close();
}
/**
* Driver MUST format date/time in UTC because SQLite's internal date/time format is UTC.
*
* To test this in UTC timezone use java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("GMT+3"));
*/
@Test
public void setDateText() throws SQLException {
SQLiteConfig config = new SQLiteConfig();
config.setReadOnly(true);
config.setDateClass("TEXT");
conn = DriverManager.getConnection("jdbc:sqlite:", config.toProperties());
stat = conn.prepareStatement("select strftime('%s', ?)");
stat.setDate(1, DATETIME);
ResultSet rs = stat.executeQuery();
assertTrue(rs.next());
assertEquals(rs.getLong(1), DATETIME_UNIX);
rs.close();
}
@Test
public void getDateInt() throws SQLException {
SQLiteConfig config = new SQLiteConfig();
config.setReadOnly(true);
config.setDateClass("INTEGER");
config.setDatePrecision("SECONDS");
conn = DriverManager.getConnection("jdbc:sqlite:", config.toProperties());
stat = conn.prepareStatement("select " + String.valueOf(DATETIME_UNIX));
ResultSet rs = stat.executeQuery();
assertTrue(rs.next());
assertEquals(rs.getDate(1), DATETIME);
rs.close();
}
/**
* Driver MUST scan date/time in UTC because SQLite's internal date/time format is UTC.
*
* To test this in UTC timezone use java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("GMT+3"));
*/
@Test
public void getDateJulian() throws SQLException {
conn = DriverManager.getConnection("jdbc:sqlite:");
stat = conn.prepareStatement("select julianday(" + String.valueOf(DATETIME_UNIX) + ", 'unixepoch', '+' || (" + String.valueOf(DATETIME_MILLISECONDS) + " / 1000.0) || ' seconds')");
ResultSet rs = stat.executeQuery();
assertTrue(rs.next());
assertEquals(rs.getDate(1), DATETIME_HIPRECISION);
rs.close();
}
/**
* Driver MUST scan date/time in UTC because SQLite's internal date/time format is UTC.
*
* To test this in UTC timezone use java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("GMT+3"));
*/
@Test
public void getDateString() throws SQLException {
conn = DriverManager.getConnection("jdbc:sqlite:");
stat = conn.prepareStatement("select strftime('%Y-%m-%d %H:%M:%f', " + String.valueOf(DATETIME_UNIX) + ", 'unixepoch', '+' || (" + String.valueOf(DATETIME_MILLISECONDS) + " / 1000.0) || ' seconds')");
ResultSet rs = stat.executeQuery();
assertTrue(rs.next());
assertEquals(rs.getDate(1), DATETIME_HIPRECISION);
rs.close();
}
}