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

Support 3-digit years #251

Merged
merged 6 commits into from
Aug 13, 2018
Merged
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
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))
])
})
})