Expose xapi.parseDateTime().

This commit is contained in:
Julien Fontanet 2015-11-03 12:03:57 +01:00
parent dbc8ed9d4c
commit 0a5ce55e2b
3 changed files with 16 additions and 18 deletions

View File

@ -10,7 +10,8 @@ import {
import {
isHostRunning,
isVmHvm,
isVmRunning
isVmRunning,
parseDateTime
} from './xapi'
// ===================================================================
@ -28,25 +29,20 @@ function link (obj, prop, idField = '$id') {
return dynamicValue[idField]
}
// The JSON interface of XAPI format dates incorrectly.
const JSON_DATE_RE = /^(\d{4})(\d{2})(\d{2})T(.+)$/
function fixJsonDate (date) {
const matches = JSON_DATE_RE.exec(date)
if (!matches) {
return date
}
const [, year, month, day, time] = matches
return `${year}-${month}-${day}T${time}`
}
// Parse a string date time to a Unix timestamp (in seconds).
//
// If there are no data or if the timestamp is 0, returns null.
function toTimestamp (date) {
if (!date) {
return null
}
return Math.round(Date.parse(fixJsonDate(date)) / 1000)
const ms = parseDateTime(date).getTime()
if (!ms) {
return null
}
return Math.round(ms / 1000)
}
// ===================================================================

View File

@ -3,7 +3,7 @@ import got from 'got'
import JSON5 from 'json5'
import { BaseError } from 'make-error'
import { dateTimeFormat } from './xapi'
import { parseDateTime } from './xapi'
const RRD_STEP_SECONDS = 5
const RRD_STEP_MINUTES = 60
@ -57,7 +57,7 @@ function convertNanToNull (value) {
async function getServerTimestamp (xapi, host) {
const serverLocalTime = await xapi.call('host.get_servertime', host.$ref)
return Math.floor(dateTimeFormat.parse(serverLocalTime).getTime() / 1000)
return Math.floor(parseDateTime(serverLocalTime).getTime() / 1000)
}
// -------------------------------------------------------------------

View File

@ -81,7 +81,9 @@ const getNamespaceForType = (type) => typeToNamespace[type] || type
// Format a date (pseudo ISO 8601) from one XenServer get by
// xapi.call('host.get_servertime', host.ref) for example
export const dateTimeFormat = d3TimeFormat.utcFormat('%Y%m%dT%H:%M:%SZ')
export const formatDateTime = d3TimeFormat.utcFormat('%Y%m%dT%H:%M:%SZ')
export const parseDateTime = formatDateTime.parse
export const isHostRunning = (host) => {
const {$metrics: metrics} = host