10000 Add support for simple negative HMS intervals · mst-appear/postgresql-async@71514d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71514d5

Browse files
committed
Add support for simple negative HMS intervals
1 parent 944aa87 commit 71514d5

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLIntervalEncoderDecoder.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ object PostgreSQLIntervalEncoderDecoder extends ColumnEncoderDecoder {
5454
private def postgresHMSBuilder(builder : PeriodFormatterBuilder) = builder
5555
// .printZeroAlways // really all-or-nothing
5656
.rejectSignedValues(true) // XXX: sign should apply to all
57-
.appendHours .appendSeparator(":")
58-
.appendMinutes.appendSeparator(":")
57+
.appendHours .appendSuffix(":")
58+
.appendMinutes.appendSuffix(":")
5959
.appendSecondsWithOptionalMillis
6060

61-
private val secsParser = new PeriodFormatterBuilder()
62-
.appendSecondsWithOptionalMillis
61+
private val hmsParser =
62+
postgresHMSBuilder(new PeriodFormatterBuilder())
6363
.toFormatter
6464

6565
private val postgresParser =
@@ -100,8 +100,8 @@ object PostgreSQLIntervalEncoderDecoder extends ColumnEncoderDecoder {
100100
else {
101101
/* try to guess based on what comes after the first number */
102102
val i = value.indexWhere(!_.isDigit, if ("-+".contains(value(0))) 1 else 0)
103-
if (i <= 0 || value(i).equals('.')) /* just a number */
104-
secsParser /* postgres treats this as seconds */
103+
if (i < 0 || ":.".contains(value(i))) /* simple HMS (to support group negation) */
104+
hmsParser
105105
else if (value(i).equals('-')) /* sql_standard: Y-M */
106106
sqlParser
107107
else if (value(i).equals(' ') && i+1 < value.length && value(i+1).isDigit) /* sql_standard: D H:M:S */
@@ -110,7 +110,9 @@ object PostgreSQLIntervalEncoderDecoder extends ColumnEncoderDecoder {
110110
postgresParser
111111
}
112112
)
113-
if (value.endsWith(" ago")) /* only really applies to postgres_verbose, but shouldn't hurt */
113+
if ((format eq hmsParser) && value(0).equals('-'))
114+
format.parsePeriod(value.substring(1)).negated
115+
else if (value.endsWith(" ago")) /* only really applies to postgres_verbose, but shouldn't hurt */
114116
format.parsePeriod(value.stripSuffix(" ago")).negated
115117
else
116118
format.parsePeriod(value)

postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/column/IntervalSpec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class IntervalSpec extends Specification {
4040
Seq("@ 1 year 2 mons -3 days 4 hours 5 mins 6 secs ago", "P-1Y-2M3DT-4H-5M-6S") forall {
4141
both(_) === "P-1Y-2M3DT-4H-5M-6S"
4242
}
43+
both("-1.234") === "PT-1.234S"
44+
both("-4:05:06") === "PT-4H-5M-6S"
4345
}
4446

4547
"parse and encode example intervals" in {

0 commit comments

Comments
 (0)
0