Skip to content

Commit

Permalink
Merge pull request #251 from jakubroztocil/bug/202-fix-3-digit-year
Browse files Browse the repository at this point in the history
Support 3-digit years
  • Loading branch information
davidgoli authored Aug 13, 2018
2 parents b254783 + ea3061c commit e7e861c
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ script:
- TZ=America/Vancouver yarn test
- TZ=America/Los_Angeles yarn test
- TZ=Africa/Nairobi yarn test
- TZ=Asia/Manila yarn test
- TZ=Asia/Tokyo yarn test
sudo: false
29 changes: 20 additions & 9 deletions dist/es5/rrule.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/es5/rrule.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/es5/rrule.min.js

Large diffs are not rendered by default.

17 changes: 6 additions & 11 deletions dist/es6/dateutil.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/es6/dateutil.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/es6/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export declare const isArray: (arg: any) => arg is any[];
export declare const range: (start: number, end?: number) => number[];
export declare const clone: <T>(array: T[]) => T[];
export declare const repeat: <T>(value: T | T[], times: number) => (T | T[])[];
export declare function padStart(str: string, targetLength: number, padString?: string): string;
/**
* Python like split
*/
Expand Down
12 changes: 12 additions & 0 deletions dist/es6/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/es6/helpers.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions src/dateutil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { divmod, pymod, empty, includes } from './helpers'
import { divmod, pymod, empty, includes, padStart } from './helpers'

type Datelike = Pick<Date, 'getTime'>

Expand Down Expand Up @@ -153,26 +153,22 @@ export namespace dateutil {
}

export const timeToUntilString = function (time: number) {
let comp
const date = new Date(time)
const comps = [
date.getUTCFullYear(),
return [
padStart(date.getUTCFullYear().toString(), 4, '0'),
date.getUTCMonth() + 1,
date.getUTCDate(),
'T',
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
'Z'
]

for (let i = 0; i < comps.length; i++) {
comp = comps[i]
if (!/[TZ]/.test(comp.toString()) && comp < 10) {
comps[i] = '0' + String(comp)
}
}
return comps.join('')
].map(value => value.toString())
.map(value =>
/[TZ]/.test(value) ?
value :
padStart(value, 2, '0')
).join('')
}

export const untilStringToDate = function (until: string) {
Expand Down
14 changes: 14 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ export const repeat = function<T>(value: T | T[], times: number): (T | T[])[] {
return array
}

export function padStart (str: string, targetLength: number, padString: string = ' ') {
targetLength = targetLength >> 0
if (str.length > targetLength) {
return String(str)
}

targetLength = targetLength - str.length
if (targetLength > padString.length) {
padString += repeat(padString, targetLength / padString.length)
}

return padString.slice(0,targetLength) + String(str)
}

/**
* Python like split
*/
Expand Down
15 changes: 15 additions & 0 deletions test/rrule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3677,4 +3677,19 @@ describe('RRule', function () {
new Date('2018-11-09T12:00:00.000Z')
])
})

it('handles 3-digit years properly (#202)', () => {
const rrule = new RRule({
count: 1,
dtstart: new Date(Date.UTC(990, 0, 1, 0, 0, 0))
})
const ruleString = rrule.toString()
const rrule2 = RRule.fromString(ruleString)

expect(ruleString).to.equal('COUNT=1;DTSTART=09900101T000000Z')
expect(rrule2.count()).to.equal(1)
expect(rrule2.all()).to.deep.equal([
new Date(Date.UTC(990, 0, 1, 0, 0, 0))
])
})
})

0 comments on commit e7e861c

Please sign in to comment.