duckdb 0.6.2-dev901.0 → 0.6.2-dev904.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "duckdb",
3
3
  "main": "./lib/duckdb.js",
4
4
  "types": "./lib/duckdb.d.ts",
5
- "version": "0.6.2-dev901.0",
5
+ "version": "0.6.2-dev904.0",
6
6
  "description": "DuckDB node.js API",
7
7
  "gypfile": true,
8
8
  "dependencies": {
@@ -502,32 +502,51 @@ int32_t Date::ExtractISODayOfTheWeek(date_t date) {
502
502
  }
503
503
  }
504
504
 
505
- static int32_t GetISOYearWeek(int32_t &year, int32_t month, int32_t day) {
506
- auto day_of_the_year =
507
- (Date::IsLeapYear(year) ? Date::CUMULATIVE_LEAP_DAYS[month] : Date::CUMULATIVE_DAYS[month]) + day;
508
- // get the first day of the first week of the year
509
- // the first week is the week that has the 4th of January in it
510
- const auto weekday_of_the_fourth = Date::ExtractISODayOfTheWeek(Date::FromDate(year, 1, 4));
511
- // if fourth is monday, then fourth is the first day
512
- // if fourth is tuesday, third is the first day
513
- // if fourth is wednesday, second is the first day
514
- // if fourth is thursday, first is the first day
515
- // if fourth is friday - sunday, day is in the previous year
516
- // (day is 0-based, weekday is 1-based)
517
- const auto first_day_of_the_first_isoweek = 4 - weekday_of_the_fourth;
518
- if (day_of_the_year < first_day_of_the_first_isoweek) {
519
- // day is part of last year (13th month)
520
- --year;
521
- return GetISOYearWeek(year, 12, day);
522
- } else {
523
- return ((day_of_the_year - first_day_of_the_first_isoweek) / 7) + 1;
505
+ template <typename T>
506
+ static T PythonDivMod(const T &x, const T &y, T &r) {
507
+ // D_ASSERT(y > 0);
508
+ T quo = x / y;
509
+ r = x - quo * y;
510
+ if (r < 0) {
511
+ --quo;
512
+ r += y;
524
513
  }
514
+ // D_ASSERT(0 <= r && r < y);
515
+ return quo;
525
516
  }
526
517
 
527
- void Date::ExtractISOYearWeek(date_t date, int32_t &year, int32_t &week) {
518
+ static date_t GetISOWeekOne(int32_t year) {
519
+ const auto first_day = Date::FromDate(year, 1, 1); /* ord of 1/1 */
520
+ /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */
521
+ const auto first_weekday = Date::ExtractISODayOfTheWeek(first_day) - 1;
522
+ /* ordinal of closest Monday at or before 1/1 */
523
+ auto week1_monday = first_day - first_weekday;
524
+
525
+ if (first_weekday > 3) { /* if 1/1 was Fri, Sat, Sun */
526
+ week1_monday += 7;
527
+ }
528
+
529
+ return week1_monday;
530
+ }
531
+
532
+ static int32_t GetISOYearWeek(const date_t date, int32_t &year) {
528
533
  int32_t month, day;
529
534
  Date::Convert(date, year, month, day);
530
- week = GetISOYearWeek(year, month - 1, day - 1);
535
+ auto week1_monday = GetISOWeekOne(year);
536
+ auto week = PythonDivMod((date.days - week1_monday.days), 7, day);
537
+ if (week < 0) {
538
+ week1_monday = GetISOWeekOne(--year);
539
+ week = PythonDivMod((date.days - week1_monday.days), 7, day);
540
+ } else if (week >= 52 && date >= GetISOWeekOne(year + 1)) {
541
+ ++year;
542
+ week = 0;
543
+ }
544
+
545
+ return week + 1;
546
+ }
547
+
548
+ void Date::ExtractISOYearWeek(date_t date, int32_t &year, int32_t &week) {
549
+ week = GetISOYearWeek(date, year);
531
550
  }
532
551
 
533
552
  int32_t Date::ExtractISOWeekNumber(date_t date) {
@@ -1,8 +1,8 @@
1
1
  #ifndef DUCKDB_VERSION
2
- #define DUCKDB_VERSION "0.6.2-dev901"
2
+ #define DUCKDB_VERSION "0.6.2-dev904"
3
3
  #endif
4
4
  #ifndef DUCKDB_SOURCE_ID
5
- #define DUCKDB_SOURCE_ID "f805b1f98a"
5
+ #define DUCKDB_SOURCE_ID "b844a10312"
6
6
  #endif
7
7
  #include "duckdb/function/table/system_functions.hpp"
8
8
  #include "duckdb/main/database.hpp"