Skip to content

Commit 9e70d16

Browse files
committed
Add timezones
1 parent d358ee3 commit 9e70d16

File tree

3 files changed

+68
-60
lines changed

3 files changed

+68
-60
lines changed

driver-core/src/main/com/mongodb/client/model/expressions/DateExpression.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020
* Expresses a date value.
2121
*/
2222
public interface DateExpression extends Expression {
23-
IntegerExpression year();
24-
IntegerExpression month();
25-
IntegerExpression dayOfMonth();
26-
IntegerExpression dayOfWeek();
27-
IntegerExpression dayOfYear();
28-
IntegerExpression hour();
29-
IntegerExpression minute();
30-
IntegerExpression second();
31-
IntegerExpression week();
32-
IntegerExpression millisecond();
23+
IntegerExpression year(StringExpression timezone);
24+
IntegerExpression month(StringExpression timezone);
25+
IntegerExpression dayOfMonth(StringExpression timezone);
26+
IntegerExpression dayOfWeek(StringExpression timezone);
27+
IntegerExpression dayOfYear(StringExpression timezone);
28+
IntegerExpression hour(StringExpression timezone);
29+
IntegerExpression minute(StringExpression timezone);
30+
IntegerExpression second(StringExpression timezone);
31+
IntegerExpression week(StringExpression timezone);
32+
IntegerExpression millisecond(StringExpression timezone);
3333

34-
StringExpression dateToString();
35-
StringExpression dateToString(StringExpression format, StringExpression timezone);
34+
StringExpression dateToString(StringExpression timezone);
35+
StringExpression dateToString(StringExpression timezone, StringExpression format);
3636

3737
}

driver-core/src/main/com/mongodb/client/model/expressions/MqlExpression.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -334,64 +334,71 @@ public IntegerExpression min(final IntegerExpression i) {
334334

335335
/** @see DateExpression */
336336

337+
private MqlExpression<Expression> usingTimezone(final String name, final StringExpression timezone) {
338+
return new MqlExpression<>((cr) -> astDoc(name, new BsonDocument()
339+
.append("date", this.toBsonValue(cr))
340+
.append("timezone", extractBsonValue(cr, timezone))));
341+
}
342+
337343
@Override
338-
public IntegerExpression year() {
339-
return new MqlExpression<>(ast("$year"));
344+
public IntegerExpression year(final StringExpression timezone) {
345+
return usingTimezone("$year", timezone);
340346
}
341347

342348
@Override
343-
public IntegerExpression month() {
344-
return new MqlExpression<>(ast("$month"));
349+
public IntegerExpression month(final StringExpression timezone) {
350+
return usingTimezone("$month", timezone);
345351
}
346352

347353
@Override
348-
public IntegerExpression dayOfMonth() {
349-
return new MqlExpression<>(ast("$dayOfMonth"));
354+
public IntegerExpression dayOfMonth(final StringExpression timezone) {
355+
return usingTimezone("$dayOfMonth", timezone);
350356
}
351357

352358
@Override
353-
public IntegerExpression dayOfWeek() {
354-
return new MqlExpression<>(ast("$dayOfWeek"));
359+
public IntegerExpression dayOfWeek(final StringExpression timezone) {
360+
return usingTimezone("$dayOfWeek", timezone);
355361
}
356362

357363
@Override
358-
public IntegerExpression dayOfYear() {
359-
return new MqlExpression<>(ast("$dayOfYear"));
364+
public IntegerExpression dayOfYear(final StringExpression timezone) {
365+
return usingTimezone("$dayOfYear", timezone);
360366
}
361367

362368
@Override
363-
public IntegerExpression hour() {
364-
return new MqlExpression<>(ast("$hour"));
369+
public IntegerExpression hour(final StringExpression timezone) {
370+
return usingTimezone("$hour", timezone);
365371
}
366372

367373
@Override
368-
public IntegerExpression minute() {
369-
return new MqlExpression<>(ast("$minute"));
374+
public IntegerExpression minute(final StringExpression timezone) {
375+
return usingTimezone("$minute", timezone);
370376
}
371377

372378
@Override
373-
public IntegerExpression second() {
374-
return new MqlExpression<>(ast("$second"));
379+
public IntegerExpression second(final StringExpression timezone) {
380+
return usingTimezone("$second", timezone);
375381
}
376382

377383
@Override
378-
public IntegerExpression week() {
379-
return new MqlExpression<>(ast("$week"));
384+
public IntegerExpression week(final StringExpression timezone) {
385+
return usingTimezone("$week", timezone);
380386
}
381387

382388
@Override
383-
public IntegerExpression millisecond() {
384-
return new MqlExpression<>(ast("$millisecond"));
389+
public IntegerExpression millisecond(final StringExpression timezone) {
390+
return usingTimezone("$millisecond", timezone);
385391
}
386392

387393
@Override
388-
public StringExpression dateToString() {
394+
public StringExpression dateToString(final StringExpression timezone) {
389395
return newMqlExpression((cr) -> astDoc("$dateToString", new BsonDocument()
390-
.append("date", this.toBsonValue(cr))));
396+
.append("date", this.toBsonValue(cr))
397+
.append("timezone", extractBsonValue(cr, timezone))));
391398
}
392399

393400
@Override
394-
public StringExpression dateToString(final StringExpression format, final StringExpression timezone) {
401+
public StringExpression dateToString(final StringExpression timezone, final StringExpression format) {
395402
return newMqlExpression((cr) -> astDoc("$dateToString", new BsonDocument()
396403
.append("date", this.toBsonValue(cr))
397404
.append("format", extractBsonValue(cr, format))

driver-core/src/test/functional/com/mongodb/client/model/expressions/DateExpressionsFunctionalTest.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ class DateExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest {
3131
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/#date-expression-operators
3232

3333
private final ZonedDateTime utcDateTime = ZonedDateTime.ofInstant(Instant.parse("2007-12-03T10:15:30.005Z"), ZoneId.of(ZoneOffset.UTC.getId()));
34+
private final StringExpression utc = of("UTC");
35+
private final DateExpression utcDateEx = of(utcDateTime.toInstant());
3436

3537
@Test
3638
public void literalsTest() {
3739
assertExpression(
38-
utcDateTime.toInstant(),
39-
of(utcDateTime.toInstant()),
40+
utcDateTime.toInstant(), utcDateEx,
4041
"{'$date': '2007-12-03T10:15:30.005Z'}");
4142
}
4243

@@ -45,107 +46,107 @@ public void yearTest() {
4546
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/year/
4647
assertExpression(
4748
utcDateTime.get(ChronoField.YEAR),
48-
of(utcDateTime.toInstant()).year(),
49-
"{'$year': {'$date': '2007-12-03T10:15:30.005Z'}}");
49+
utcDateEx.year(utc),
50+
"{'$year': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
5051
}
5152

5253
@Test
5354
public void monthTest() {
5455
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/month/
5556
assertExpression(
5657
utcDateTime.get(ChronoField.MONTH_OF_YEAR),
57-
of(utcDateTime.toInstant()).month(),
58-
"{'$month': {'$date': '2007-12-03T10:15:30.005Z'}}");
58+
utcDateEx.month(utc),
59+
"{'$month': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
5960
}
6061

6162
@Test
6263
public void dayOfMonthTest() {
6364
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfMonth/
6465
assertExpression(
6566
utcDateTime.get(ChronoField.DAY_OF_MONTH),
66-
of(utcDateTime.toInstant()).dayOfMonth(),
67-
"{'$dayOfMonth': {'$date': '2007-12-03T10:15:30.005Z'}}");
67+
utcDateEx.dayOfMonth(utc),
68+
"{'$dayOfMonth': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
6869
}
6970

7071
@Test
7172
public void dayOfWeekTest() {
7273
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/
7374
assertExpression(
7475
utcDateTime.get(ChronoField.DAY_OF_WEEK) + 1,
75-
of(utcDateTime.toInstant()).dayOfWeek(),
76-
"{'$dayOfWeek': {'$date': '2007-12-03T10:15:30.005Z'}}");
76+
utcDateEx.dayOfWeek(utc),
77+
"{'$dayOfWeek': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
7778
}
7879

7980
@Test
8081
public void dayOfYearTest() {
8182
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfYear/
8283
assertExpression(
8384
utcDateTime.get(ChronoField.DAY_OF_YEAR),
84-
of(utcDateTime.toInstant()).dayOfYear(),
85-
"{'$dayOfYear': {'$date': '2007-12-03T10:15:30.005Z'}}");
85+
utcDateEx.dayOfYear(utc),
86+
"{'$dayOfYear': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
8687
}
8788

8889
@Test
8990
public void hourTest() {
9091
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/hour/
9192
assertExpression(
9293
utcDateTime.get(ChronoField.HOUR_OF_DAY),
93-
of(utcDateTime.toInstant()).hour(),
94-
"{'$hour': {'$date': '2007-12-03T10:15:30.005Z'}}");
94+
utcDateEx.hour(utc),
95+
"{'$hour': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
9596
}
9697

9798
@Test
9899
public void minuteTest() {
99100
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/minute/
100101
assertExpression(
101102
utcDateTime.get(ChronoField.MINUTE_OF_HOUR),
102-
of(utcDateTime.toInstant()).minute(),
103-
"{'$minute': {'$date': '2007-12-03T10:15:30.005Z'}}");
103+
utcDateEx.minute(utc),
104+
"{'$minute': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
104105
}
105106

106107
@Test
107108
public void secondTest() {
108109
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/second/
109110
assertExpression(
110111
utcDateTime.get(ChronoField.SECOND_OF_MINUTE),
111-
of(utcDateTime.toInstant()).second(),
112-
"{'$second': {'$date': '2007-12-03T10:15:30.005Z'}}");
112+
utcDateEx.second(utc),
113+
"{'$second': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
113114
}
114115

115116
@Test
116117
public void weekTest() {
117118
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/week/
118119
assertExpression(
119120
48,
120-
of(utcDateTime.toInstant()).week(),
121-
"{'$week': {'$date': '2007-12-03T10:15:30.005Z'}}");
121+
utcDateEx.week(utc),
122+
"{'$week': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
122123
}
123124

124125
@Test
125126
public void millisecondTest() {
126127
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/millisecond/
127128
assertExpression(
128129
utcDateTime.get(ChronoField.MILLI_OF_SECOND),
129-
of(utcDateTime.toInstant()).millisecond(),
130-
"{'$millisecond': {'$date': '2007-12-03T10:15:30.005Z'}}");
130+
utcDateEx.millisecond(utc),
131+
"{'$millisecond': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, 'timezone': 'UTC'}}");
131132
}
132133

133134
@Test
134135
public void dateToStringTest() {
135136
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateToString/
136137
assertExpression(
137138
utcDateTime.toInstant().toString(),
138-
of(utcDateTime.toInstant()).dateToString());
139+
utcDateEx.dateToString(utc));
139140
// with parameters
140141
assertExpression(
141142
utcDateTime.withZoneSameInstant(ZoneId.of("America/New_York")).format(ISO_LOCAL_DATE_TIME),
142-
of(utcDateTime.toInstant()).dateToString(of("%Y-%m-%dT%H:%M:%S.%L"), of("America/New_York")),
143+
utcDateEx.dateToString(of("America/New_York"), of("%Y-%m-%dT%H:%M:%S.%L")),
143144
"{'$dateToString': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, "
144145
+ "'format': '%Y-%m-%dT%H:%M:%S.%L', "
145146
+ "'timezone': 'America/New_York'}}");
146147
assertExpression(
147148
utcDateTime.withZoneSameInstant(ZoneId.of("+04:30")).format(ISO_LOCAL_DATE_TIME),
148-
of(utcDateTime.toInstant()).dateToString(of("%Y-%m-%dT%H:%M:%S.%L"), of("+04:30")),
149+
utcDateEx.dateToString(of("+04:30"), of("%Y-%m-%dT%H:%M:%S.%L")),
149150
"{'$dateToString': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, "
150151
+ "'format': '%Y-%m-%dT%H:%M:%S.%L', "
151152
+ "'timezone': '+04:30'}}");

0 commit comments

Comments
 (0)