Compare commits

...

1 Commits

Author SHA1 Message Date
Julien Fontanet
d274c34b6b fix(xapi): support ISO 8601 datetime format
Fixes #6082
2022-01-13 15:23:59 +01:00
3 changed files with 27 additions and 20 deletions

View File

@@ -15,16 +15,18 @@ exports.VDI_FORMAT_VHD = 'vhd'
// xapi.call('host.get_servertime', host.$ref) for example
exports.formatDateTime = utcFormat('%Y%m%dT%H:%M:%SZ')
const parseDateTimeHelper = utcParse('%Y%m%dT%H:%M:%SZ')
const dateTimeParsers = ['%Y%m%dT%H:%M:%SZ', '%Y-%m-%dT%H:%M:%S.%LZ'].map(utcParse)
exports.parseDateTime = function (str, defaultValue) {
const date = parseDateTimeHelper(str)
if (date === null) {
if (arguments.length > 1) {
return defaultValue
for (const parser of dateTimeParsers) {
const date = parser(str)
if (date !== null) {
return date.getTime()
}
throw new RangeError(`unable to parse XAPI datetime ${JSON.stringify(str)}`)
}
return date.getTime()
if (arguments.length > 1) {
return defaultValue
}
throw new RangeError(`unable to parse XAPI datetime ${JSON.stringify(str)}`)
}
const hasProps = o => {

View File

@@ -0,0 +1,17 @@
/* eslint-env jest */
const { parseDateTime } = require('./')
describe('parseDateTime()', () => {
it('parses legacy XAPI format', () => {
expect(parseDateTime('20220106T21:25:15Z')).toBe(1641504315000)
})
it('parses ISO 8601 with millisconds format', () => {
expect(parseDateTime('2022-01-06T17:32:35.000Z')).toBe(1641490355000)
})
it('throws when value cannot be parsed', () => {
expect(() => parseDateTime('foo bar')).toThrow('unable to parse XAPI datetime "foo bar"')
})
})

View File

@@ -4,7 +4,6 @@ import isEqual from 'lodash/isEqual.js'
import isPlainObject from 'lodash/isPlainObject.js'
import pickBy from 'lodash/pickBy.js'
import semver from 'semver'
import { utcParse } from 'd3-time-format'
import { camelToSnakeCase, forEach, isInteger, map, mapFilter, noop } from '../utils.mjs'
@@ -58,18 +57,7 @@ export const extractOpaqueRef = str => {
// -------------------------------------------------------------------
const parseDateTimeHelper = utcParse('%Y%m%dT%H:%M:%SZ')
export function parseDateTime(str, defaultValue) {
const date = parseDateTimeHelper(str)
if (date === null) {
if (arguments.length > 1) {
return defaultValue
}
throw new RangeError(`unable to parse XAPI datetime ${JSON.stringify(str)}`)
}
return date.getTime()
}
export { parseDateTime } from '@xen-orchestra/xapi'
// -------------------------------------------------------------------