Skip to content

Commit

Permalink
Support truncate to weekYear
Browse files Browse the repository at this point in the history
This will return the Monday of week 1 of the week based year.
  • Loading branch information
sherfert committed Mar 16, 2018
1 parent dc6d566 commit 5141e7e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ private static TemporalUnit unit( String unit )
return ChronoUnit.DECADES;
case "year":
return ChronoUnit.YEARS;
case "weekYear":
return IsoFields.WEEK_BASED_YEARS;
case "quarter":
return IsoFields.QUARTER_YEARS;
case "month":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ else if ( unit == ChronoUnit.YEARS )
{
return value.with( TemporalAdjusters.firstDayOfYear() );
}
else if ( unit == IsoFields.WEEK_BASED_YEARS)
{
return value.with( IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1 ).with( ChronoField.DAY_OF_WEEK, 1 );
}
else if ( unit == IsoFields.QUARTER_YEARS )
{
return value.with( IsoFields.DAY_OF_QUARTER, 1 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ Feature "TemporalTruncateAcceptance": Scenario "Should truncate to decade"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to decade with time zone"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to year"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to year with time zone"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to weekYear"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to weekYear with time zone"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to quarter"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to quarter with time zone"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to month"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ Feature "TemporalTruncateAcceptance": Scenario "Should truncate to decade"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to decade with time zone"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to year"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to year with time zone"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to weekYear"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to weekYear with time zone"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to quarter"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to quarter with time zone"
Feature "TemporalTruncateAcceptance": Scenario "Should truncate to month"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,43 @@ Feature: TemporalTruncateAcceptance
| '1984-01-01T00:00+01:00[Europe/Stockholm]' |
And no side effects

Scenario: Should truncate to weekYear
Given an empty graph
When executing query:
"""
UNWIND [datetime({year:1984, month:1, day:1, hour:12, minute:31, second:14, nanosecond: 645876123, timezone: '+01:00'}),
localdatetime({year:1984, month:1, day:1, hour:12, minute:31, second:14, nanosecond: 645876123}),
date({year:1984, month:2, day:1})] as d
RETURN datetime.truncate('weekYear', d) as d1,
datetime.truncate('weekYear', d, {day:5}) as d2,
localdatetime.truncate('weekYear', d) as d3,
localdatetime.truncate('weekYear', d, {day:5}) as d4,
date.truncate('weekYear', d) as d5,
date.truncate('weekYear', d, {day:5}) as d6
"""
Then the result should be, in order:
| d1 | d2 | d3 | d4 | d5 | d6 |
| '1983-01-03T00:00+01:00' | '1983-01-05T00:00+01:00' | '1983-01-03T00:00' | '1983-01-05T00:00' |'1983-01-03' | '1983-01-05' |
| '1983-01-03T00:00Z' | '1983-01-05T00:00Z' | '1983-01-03T00:00' | '1983-01-05T00:00' |'1983-01-03' | '1983-01-05' |
| '1984-01-02T00:00Z' | '1984-01-05T00:00Z' | '1984-01-02T00:00' | '1984-01-05T00:00' |'1984-01-02' | '1984-01-05' |
And no side effects

Scenario: Should truncate to weekYear with time zone
Given an empty graph
When executing query:
"""
UNWIND [datetime({year:1984, month:1, day:1, hour:12, minute:31, second:14, nanosecond: 645876123, timezone: '-01:00'}),
localdatetime({year:1984, month:1, day:1, hour:12, minute:31, second:14, nanosecond: 645876123}),
date({year:1984, month:2, day:1})] as d
RETURN datetime.truncate('weekYear', d, {timezone:'Europe/Stockholm'}) as d1
"""
Then the result should be, in order:
| d1 |
| '1983-01-03T00:00+01:00[Europe/Stockholm]' |
| '1983-01-03T00:00+01:00[Europe/Stockholm]' |
| '1984-01-02T00:00+01:00[Europe/Stockholm]' |
And no side effects

Scenario: Should truncate to quarter
Given an empty graph
When executing query:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ class TemporalAcceptanceTest extends ExecutionEngineFunSuite with QueryStatistic
Seq("time({hour: 12, minute: 30, second: 40, timezone:'+01:00'})", "localtime({hour: 12, minute: 30, second: 40})"))
}

test("should not truncate to weekYear with wrong receiver") {
shouldNotTruncate[UnsupportedTemporalTypeException](Seq("time", "localtime"), "weekYear",
Seq("datetime({year:1984, month: 2, day:11, hour: 12, minute: 30, second: 40, timezone:'+01:00'})"))
}

test("should not truncate to weekYear with wrong argument") {
shouldNotTruncate[IllegalArgumentException](Seq("datetime", "localdatetime", "date"), "weekYear",
Seq("time({hour: 12, minute: 30, second: 40, timezone:'+01:00'})", "localtime({hour: 12, minute: 30, second: 40})"))
}

test("should not truncate to quarter with wrong receiver") {
shouldNotTruncate[UnsupportedTemporalTypeException](Seq("time", "localtime"), "quarter",
Seq("datetime({year:1984, month: 2, day:11, hour: 12, minute: 30, second: 40, timezone:'+01:00'})"))
Expand Down

0 comments on commit 5141e7e

Please sign in to comment.