Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unspecified leap years #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/bitmask.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const PATTERN = /^[0-9xXdDmMyY]{8}$/
const YYYYMMDD = 'YYYYMMDD'.split('')
const MAXDAYS = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

const { floor, pow, max, min } = Math
const { floor, pow, max, min, abs } = Math


/**
Expand Down Expand Up @@ -173,6 +173,9 @@ class Bitmask {
}

break
default:
if (!this.test(Bitmask.MONTH) && month === 1 && day === 29)
year = this.lastLeapYear(year)
}

if (month === 1 && day > 28 && !leap(year)) {
Expand Down Expand Up @@ -263,6 +266,24 @@ class Bitmask {
toString(symbol = 'X') {
return this.masks(['YYYY', 'MM', 'DD'], symbol).join('-')
}

lastLeapYear(year) {
if (leap(year))
return year
const values = [pad(year)]
const [minYear] = this.min(values)
const yearReg = this.masks(values, '.')
let [newYear] = this.max(values)

while (!(leap(newYear) &&
pad(newYear).match(yearReg)) &&
newYear >= minYear) {
newYear--
}
if (newYear < minYear)
return year
return newYear
}
}

Bitmask.prototype.is = Bitmask.prototype.test
Expand All @@ -274,6 +295,16 @@ function leap(year) {
return true
}

function pad(number) {
let k = abs(number)

if (k < 10) return `000${k}`
if (k < 100) return `00${k}`
if (k < 1000) return `0${k}`

return `${k}`
}

Bitmask.DAY = Bitmask.D = Bitmask.compute('yyyymmxx')
Bitmask.MONTH = Bitmask.M = Bitmask.compute('yyyyxxdd')
Bitmask.YEAR = Bitmask.Y = Bitmask.compute('xxxxmmdd')
Expand Down
21 changes: 20 additions & 1 deletion src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,26 @@ class Date extends global.Date {
args[4] = args[4] + obj.offset
}

args = [ExtDateTime.UTC(...args)]
let UTCDateTime = ExtDateTime.UTC(...args)

const mask = new Bitmask(obj.unspecified)
if (mask.test(Bitmask.YEAR) > 0 &&
!mask.test(Bitmask.MONTH) &&
!mask.test(Bitmask.DAY)) {
let date = new global.Date(UTCDateTime),
year = date.getUTCFullYear(),
day = date.getUTCDate()
const leapYear = mask.lastLeapYear(abs(year))
if (leapYear !== abs(year)) {
args[0] = leapYear * (year < 0 ? -1 : 1)
let newUTCDateTime = ExtDateTime.UTC(...args),
newDate = new global.Date(newUTCDateTime),
newDay = newDate.getUTCDate()
if (newDay !== day) UTCDateTime = newUTCDateTime
}
}

args = [UTCDateTime]
}

({ uncertain, approximate, unspecified } = obj)
Expand Down
25 changes: 25 additions & 0 deletions test/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,28 @@ describe('Date', () => {
expect(new Date([2014, 3, 1]).format('en-US')).to.eql('4/1/2014'))
})
})

describe('Leap year', () => {
it('YYXX-02-29', () => {
let date = new Date({
values: [2100, 1, 29],
unspecified: 12
})

expect(date.edtf).to.eql('21XX-02-29')

date = new Date('21XX-02-29')

expect(date.edtf).to.eql('21XX-02-29')
})

it('.next()', () => {
let date = new Date('201X-02-28')
expect(date.next().edtf).to.eql('201X-02-29')
})

it('.prev()', () => {
let date = new Date('201X-03-01')
expect(date.prev().edtf).to.eql('201X-02-29')
})
})