Skip to content

Commit

Permalink
Merge pull request #9890 from hawkfish/strptime-week
Browse files Browse the repository at this point in the history
Issue #9869: Strptime Week Start
  • Loading branch information
Mytherin committed Dec 6, 2023
2 parents b7364ad + 2dcecdc commit 3cadd21
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/function/scalar/strftime_format.cpp
Expand Up @@ -770,6 +770,7 @@ bool StrpTimeFormat::Parse(string_t str, ParseResult &result) const {
uint64_t weekno = 0;
uint64_t weekday = 0;
uint64_t yearday = 0;
bool has_weekday = false;

for (idx_t i = 0;; i++) {
D_ASSERT(i < literals.size());
Expand Down Expand Up @@ -947,6 +948,7 @@ bool StrpTimeFormat::Parse(string_t str, ParseResult &result) const {
error_position = start_pos;
return false;
}
has_weekday = true;
weekday = number;
break;
case StrTimeSpecifier::DAY_OF_YEAR_PADDED:
Expand Down Expand Up @@ -1110,7 +1112,9 @@ bool StrpTimeFormat::Parse(string_t str, ParseResult &result) const {
case StrTimeSpecifier::WEEK_NUMBER_PADDED_SUN_FIRST:
case StrTimeSpecifier::WEEK_NUMBER_PADDED_MON_FIRST: {
// Adjust weekday to be 0-based for the week type
weekday = (weekday + 7 - int(offset_specifier == StrTimeSpecifier::WEEK_NUMBER_PADDED_MON_FIRST)) % 7;
if (has_weekday) {
weekday = (weekday + 7 - int(offset_specifier == StrTimeSpecifier::WEEK_NUMBER_PADDED_MON_FIRST)) % 7;
}
// Get the start of week 1, move back 7 days and then weekno * 7 + weekday gives the date
const auto jan1 = Date::FromDate(result_data[0], 1, 1);
auto yeardate = Date::GetMondayOfCurrentWeek(jan1);
Expand Down
8 changes: 4 additions & 4 deletions test/sql/function/timestamp/test_strptime.test
Expand Up @@ -72,11 +72,11 @@ SELECT strptime('jun', '%b')
----
1900-06-01 00:00:00

# First Sunday of Monday weeks is Jan 7
# First Monday of Monday weeks is Jan 1
query I
SELECT strptime('1', '%W')
----
1900-01-07 00:00:00
1900-01-01 00:00:00

# First Sunday of Sunday weeks is Jan 7
query I
Expand All @@ -90,9 +90,9 @@ SELECT strptime('30', '%U'), strftime('1900-07-29'::DATE, '%U')
1900-07-29 00:00:00 30

query II
SELECT strptime('30', '%W'), strftime('1900-07-29'::DATE, '%W')
SELECT strptime('30', '%W'), strftime('1900-07-23'::DATE, '%W')
----
1900-07-29 00:00:00 30
1900-07-23 00:00:00 30

# Ignored
query I
Expand Down
8 changes: 4 additions & 4 deletions test/sql/function/timestamp/test_try_strptime.test
Expand Up @@ -72,11 +72,11 @@ SELECT try_strptime('jun', '%b')
----
1900-06-01 00:00:00

# First Sunday of Monday weeks is Jan 7
# First Monday of Monday weeks is Jan 7
query I
SELECT try_strptime('1', '%W')
----
1900-01-07 00:00:00
1900-01-01 00:00:00

# First Sunday of Sunday weeks is Jan 7
query I
Expand All @@ -90,9 +90,9 @@ SELECT try_strptime('30', '%U'), strftime('1900-07-29'::DATE, '%U')
1900-07-29 00:00:00 30

query II
SELECT try_strptime('30', '%W'), strftime('1900-07-29'::DATE, '%W')
SELECT try_strptime('30', '%W'), strftime('1900-07-23'::DATE, '%W')
----
1900-07-29 00:00:00 30
1900-07-23 00:00:00 30

# Ignored
query I
Expand Down

0 comments on commit 3cadd21

Please sign in to comment.