@naturalcycles/time-lib
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -0,1 +1,8 @@ | ||
# [3.2.0](https://github.com/NaturalCycles/time-lib/compare/v3.1.0...v3.2.0) (2020-12-18) | ||
### Features | ||
* .week() plugin for arabic week (dow=6, doy=12) ([939e3b7](https://github.com/NaturalCycles/time-lib/commit/939e3b7954691ad603cc485ff5298e9e4b6b3803)) | ||
# [3.1.0](https://github.com/NaturalCycles/time-lib/compare/v3.0.5...v3.1.0) (2020-12-08) | ||
@@ -2,0 +9,0 @@ |
@@ -19,2 +19,3 @@ // import dayjsLib = require('dayjs') | ||
dayjs.extend(weekOfYearPlugin); | ||
// dayjs.extend(require('dayjs/plugin/weekOfYear')) | ||
// Set en-gb by default, to have e.g Monday as fdow | ||
@@ -21,0 +22,0 @@ require(`dayjs/locale/en-gb`); |
@@ -0,4 +1,5 @@ | ||
const MILLIS_IN_DAY = 86400000; | ||
export const weekOfYearPlugin = (_opt, dayjsClass, dayjs) => { | ||
dayjsClass.prototype.week = function () { | ||
const weekStart = this.$locale().weekStart || 0; | ||
const weekStart = this.$locale().weekStart || 0; // default to Sunday | ||
if (weekStart === 1) { | ||
@@ -14,3 +15,3 @@ // ISO implementation | ||
// Calculate full weeks to nearest Thursday | ||
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7); | ||
return Math.ceil(((d.getTime() - yearStart.getTime()) / MILLIS_IN_DAY + 1) / 7); | ||
} | ||
@@ -30,6 +31,37 @@ else if (weekStart === 0) { | ||
} | ||
else { | ||
throw new Error(`.week() plugin for weekStart=${weekStart} is not supported yet:(`); | ||
else if (weekStart === 6) { | ||
// Loosely based on: https://gist.github.com/IamSilviu/5899269 | ||
const d = new Date(Date.UTC(this.year(), this.month(), this.date())); | ||
const firstDayOfYear = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)); | ||
const firstDay = firstDayOfYear.getUTCDay() || 7; | ||
let firstSatOffset = -firstDay - 1; | ||
if (firstDay >= 6) | ||
firstSatOffset += 7; | ||
const firstSatOfYear = new Date(Date.UTC(this.year(), 0, 1 + firstSatOffset)); | ||
const firstDayOfNextYear = new Date(Date.UTC(d.getUTCFullYear() + 1, 0, 1)); | ||
const firstDayNext = firstDayOfNextYear.getUTCDay() || 7; | ||
let nextYearOffset = -firstDayNext - 1; | ||
if (firstDayNext >= 6) | ||
nextYearOffset += 7; | ||
const firstSatOfNextYear = new Date(Date.UTC(this.year() + 1, 0, 1 + nextYearOffset)); | ||
const daysSinceFirstSat = (d.valueOf() - firstSatOfYear.valueOf()) / MILLIS_IN_DAY; | ||
const weekNum = Math.floor(daysSinceFirstSat / 7) + 1 || 1; | ||
// if (daysSinceFirstSat === 0 && d.getUTCDate() !== 1) weekNum = 2 | ||
// console.log({ | ||
// d, | ||
// firstDayOfYear, | ||
// firstSatOfYear, | ||
// firstSatOffset, | ||
// daysSinceFirstSat, | ||
// nextYearOffset, | ||
// firstSatOfNextYear, | ||
// weekNum, | ||
// }) | ||
// It's next year's week already | ||
if (d.valueOf() >= firstSatOfNextYear.valueOf()) | ||
return 1; | ||
return weekNum; | ||
} | ||
throw new Error(`.week() plugin for weekStart=${weekStart} is not supported yet:(`); | ||
}; | ||
}; |
@@ -23,2 +23,3 @@ "use strict"; | ||
dayjs.extend(weekOfYear_1.weekOfYearPlugin); | ||
// dayjs.extend(require('dayjs/plugin/weekOfYear')) | ||
// Set en-gb by default, to have e.g Monday as fdow | ||
@@ -25,0 +26,0 @@ require(`dayjs/locale/en-gb`); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.weekOfYearPlugin = void 0; | ||
const MILLIS_IN_DAY = 86400000; | ||
const weekOfYearPlugin = (_opt, dayjsClass, dayjs) => { | ||
dayjsClass.prototype.week = function () { | ||
const weekStart = this.$locale().weekStart || 0; | ||
const weekStart = this.$locale().weekStart || 0; // default to Sunday | ||
if (weekStart === 1) { | ||
@@ -17,3 +18,3 @@ // ISO implementation | ||
// Calculate full weeks to nearest Thursday | ||
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7); | ||
return Math.ceil(((d.getTime() - yearStart.getTime()) / MILLIS_IN_DAY + 1) / 7); | ||
} | ||
@@ -33,5 +34,36 @@ else if (weekStart === 0) { | ||
} | ||
else { | ||
throw new Error(`.week() plugin for weekStart=${weekStart} is not supported yet:(`); | ||
else if (weekStart === 6) { | ||
// Loosely based on: https://gist.github.com/IamSilviu/5899269 | ||
const d = new Date(Date.UTC(this.year(), this.month(), this.date())); | ||
const firstDayOfYear = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)); | ||
const firstDay = firstDayOfYear.getUTCDay() || 7; | ||
let firstSatOffset = -firstDay - 1; | ||
if (firstDay >= 6) | ||
firstSatOffset += 7; | ||
const firstSatOfYear = new Date(Date.UTC(this.year(), 0, 1 + firstSatOffset)); | ||
const firstDayOfNextYear = new Date(Date.UTC(d.getUTCFullYear() + 1, 0, 1)); | ||
const firstDayNext = firstDayOfNextYear.getUTCDay() || 7; | ||
let nextYearOffset = -firstDayNext - 1; | ||
if (firstDayNext >= 6) | ||
nextYearOffset += 7; | ||
const firstSatOfNextYear = new Date(Date.UTC(this.year() + 1, 0, 1 + nextYearOffset)); | ||
const daysSinceFirstSat = (d.valueOf() - firstSatOfYear.valueOf()) / MILLIS_IN_DAY; | ||
const weekNum = Math.floor(daysSinceFirstSat / 7) + 1 || 1; | ||
// if (daysSinceFirstSat === 0 && d.getUTCDate() !== 1) weekNum = 2 | ||
// console.log({ | ||
// d, | ||
// firstDayOfYear, | ||
// firstSatOfYear, | ||
// firstSatOffset, | ||
// daysSinceFirstSat, | ||
// nextYearOffset, | ||
// firstSatOfNextYear, | ||
// weekNum, | ||
// }) | ||
// It's next year's week already | ||
if (d.valueOf() >= firstSatOfNextYear.valueOf()) | ||
return 1; | ||
return weekNum; | ||
} | ||
throw new Error(`.week() plugin for weekStart=${weekStart} is not supported yet:(`); | ||
}; | ||
@@ -38,0 +70,0 @@ }; |
@@ -138,2 +138,3 @@ export declare type ConfigType = string | number | Date | IDayjs; | ||
weekStart?: number; | ||
yearStart?: number; | ||
weekdaysShort?: string[]; | ||
@@ -140,0 +141,0 @@ monthsShort?: string[]; |
@@ -43,3 +43,3 @@ { | ||
}, | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Date/time related API, based on Day.js", | ||
@@ -46,0 +46,0 @@ "keywords": [ |
@@ -22,2 +22,3 @@ // import dayjsLib = require('dayjs') | ||
dayjs.extend(weekOfYearPlugin) | ||
// dayjs.extend(require('dayjs/plugin/weekOfYear')) | ||
@@ -24,0 +25,0 @@ // Set en-gb by default, to have e.g Monday as fdow |
import type { IDayjs, PluginFunc } from '../types' | ||
const MILLIS_IN_DAY = 86400000 | ||
export const weekOfYearPlugin: PluginFunc = (_opt, dayjsClass, dayjs) => { | ||
dayjsClass.prototype.week = function (this: IDayjs) { | ||
const weekStart = this.$locale().weekStart || 0 | ||
const weekStart = this.$locale().weekStart || 0 // default to Sunday | ||
@@ -17,3 +19,3 @@ if (weekStart === 1) { | ||
// Calculate full weeks to nearest Thursday | ||
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7) | ||
return Math.ceil(((d.getTime() - yearStart.getTime()) / MILLIS_IN_DAY + 1) / 7) | ||
} else if (weekStart === 0) { | ||
@@ -31,6 +33,42 @@ // US implementation | ||
return Math.ceil(diffInWeek) | ||
} else { | ||
throw new Error(`.week() plugin for weekStart=${weekStart} is not supported yet:(`) | ||
} else if (weekStart === 6) { | ||
// Loosely based on: https://gist.github.com/IamSilviu/5899269 | ||
const d = new Date(Date.UTC(this.year(), this.month(), this.date())) | ||
const firstDayOfYear = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)) | ||
const firstDay = firstDayOfYear.getUTCDay() || 7 | ||
let firstSatOffset = -firstDay - 1 | ||
if (firstDay >= 6) firstSatOffset += 7 | ||
const firstSatOfYear = new Date(Date.UTC(this.year(), 0, 1 + firstSatOffset)) | ||
const firstDayOfNextYear = new Date(Date.UTC(d.getUTCFullYear() + 1, 0, 1)) | ||
const firstDayNext = firstDayOfNextYear.getUTCDay() || 7 | ||
let nextYearOffset = -firstDayNext - 1 | ||
if (firstDayNext >= 6) nextYearOffset += 7 | ||
const firstSatOfNextYear = new Date(Date.UTC(this.year() + 1, 0, 1 + nextYearOffset)) | ||
const daysSinceFirstSat = (d.valueOf() - firstSatOfYear.valueOf()) / MILLIS_IN_DAY | ||
const weekNum = Math.floor(daysSinceFirstSat / 7) + 1 || 1 | ||
// if (daysSinceFirstSat === 0 && d.getUTCDate() !== 1) weekNum = 2 | ||
// console.log({ | ||
// d, | ||
// firstDayOfYear, | ||
// firstSatOfYear, | ||
// firstSatOffset, | ||
// daysSinceFirstSat, | ||
// nextYearOffset, | ||
// firstSatOfNextYear, | ||
// weekNum, | ||
// }) | ||
// It's next year's week already | ||
if (d.valueOf() >= firstSatOfNextYear.valueOf()) return 1 | ||
return weekNum | ||
} | ||
throw new Error(`.week() plugin for weekStart=${weekStart} is not supported yet:(`) | ||
} | ||
} |
@@ -223,2 +223,3 @@ export type ConfigType = string | number | Date | IDayjs | ||
weekStart?: number | ||
yearStart?: number | ||
weekdaysShort?: string[] | ||
@@ -225,0 +226,0 @@ monthsShort?: string[] |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54026
906