-
Notifications
You must be signed in to change notification settings - Fork 819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setTimestamp use timestamptz db type #3220
base: master
Are you sure you want to change the base?
Changes from all commits
b0efea6
7dc2793
84c4ecf
17a61bd
3efa08e
1c807cb
5a6d151
ec64af1
0b8243b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,11 +68,13 @@ | |
import java.sql.Time; | ||
import java.sql.Timestamp; | ||
import java.sql.Types; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.OffsetDateTime; | ||
import java.time.OffsetTime; | ||
import java.time.ZonedDateTime; | ||
import java.util.ArrayList; | ||
import java.util.Calendar; | ||
import java.util.Map; | ||
|
@@ -682,6 +684,10 @@ public void setObject(@Positive int parameterIndex, @Nullable Object in, | |
} else if (in instanceof LocalDateTime) { | ||
setTimestamp(parameterIndex, (LocalDateTime) in); | ||
break; | ||
} else if (in instanceof Instant) { | ||
tmpts = Timestamp.from( (Instant) in ); | ||
} else if (in instanceof ZonedDateTime) { | ||
tmpts = Timestamp.valueOf( ((ZonedDateTime) in).toLocalDateTime()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I might be wrong with my assumptions here of course. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your assumption is correct, so I changed it to use OffsetDateTime type so that time zone information is not lost but stored into the timestamptz. Anyway, if you use OffsetDateTime to fetch data back form the db, the time zone info is lost as illustrated by the test case TimestamptzTest.test_ZonedDateTime_StoreAndSelect(). NOTE: in our application we always store dates in UTC time zone in the db to avoid any problem. If we need to store information about the time zone to use for some business logic that is stored into a separate fields independent to any stored timestamptz. |
||
} else { | ||
tmpts = getTimestampUtils().toTimestamp(getDefaultCalendar(), in.toString().getBytes()); | ||
} | ||
|
@@ -693,6 +699,10 @@ public void setObject(@Positive int parameterIndex, @Nullable Object in, | |
setTimestamp(parameterIndex, (OffsetDateTime) in); | ||
} else if (in instanceof PGTimestamp) { | ||
setObject(parameterIndex, in); | ||
} else if (in instanceof Instant) { | ||
setTimestamp(parameterIndex, Timestamp.from( (Instant) in )); | ||
} else if (in instanceof ZonedDateTime) { | ||
setTimestamp(parameterIndex, Timestamp.valueOf(((ZonedDateTime) in).toLocalDateTime())); | ||
} else { | ||
throw new PSQLException( | ||
GT.tr("Cannot cast an instance of {0} to type {1}", | ||
|
@@ -1035,6 +1045,10 @@ public void setObject(@Positive int parameterIndex, @Nullable Object x) throws S | |
setTime(parameterIndex, (Time) x); | ||
} else if (x instanceof Timestamp) { | ||
setTimestamp(parameterIndex, (Timestamp) x); | ||
} else if (x instanceof Instant ) { | ||
setTimestamp(parameterIndex, Timestamp.from((Instant) x )); | ||
} else if (x instanceof ZonedDateTime ) { | ||
setTimestamp(parameterIndex, ((ZonedDateTime) x).toOffsetDateTime()); | ||
} else if (x instanceof Boolean) { | ||
setBoolean(parameterIndex, (Boolean) x); | ||
} else if (x instanceof Byte) { | ||
|
@@ -1506,6 +1520,10 @@ public void setTimestamp(@Positive int i, @Nullable Timestamp t, | |
if (cal == null) { | ||
cal = getDefaultCalendar(); | ||
} | ||
|
||
if (connection.isSqlTimestamptzAlways()) { | ||
oid = Oid.TIMESTAMPTZ; | ||
} | ||
bindString(i, getTimestampUtils().toString(cal, t), oid); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/time stamp/timestamp