Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/datetime.js
"use strict"; | |||||
const { getDateTime } = require("../../../utils/get-datetime.js"); | const { getDateTime } = require("../../../utils/get-datetime.js"); | ||||
module.exports = { | module.exports = { | ||||
name: "datetime", | name: "datetime", | ||||
extends: "int", | extends: "int", | ||||
has_index: function(params) { | has_index(params) { | ||||
if (params.indexed) { | if (params.indexed) { | ||||
return 1; | return 1; | ||||
} else return false; | } | ||||
return false; | |||||
}, | }, | ||||
get_description: function() { | get_description() { | ||||
return "Timestamp - amount of miliseconds since epoch."; | return "Timestamp - amount of miliseconds since epoch."; | ||||
}, | }, | ||||
is_proper_value: function(context, params, datetime) { | is_proper_value(context, params, datetime) { | ||||
if (Number.isInteger(datetime)) { | if (Number.isInteger(datetime)) { | ||||
return Promise.resolve(); | return Promise.resolve(); | ||||
} | } | ||||
return Promise.reject( | return Promise.reject( | ||||
`Value '${datetime}' is not datetime format. Only timestamps are accepted.` | `Value '${datetime}' is not datetime format. Only timestamps are accepted.` | ||||
); | ); | ||||
}, | }, | ||||
encode: function(context, params, value_in_code) { | encode(context, params, value_in_code) { | ||||
const parsed_datetime = parseInt(value_in_code); | const parsed_datetime = parseInt(value_in_code, 10); | ||||
return parsed_datetime; | return parsed_datetime; | ||||
}, | }, | ||||
format: function(context, params, decoded_value, format) { | format(context, params, decoded_value, format) { | ||||
if (decoded_value === null || decoded_value === undefined) { | if (decoded_value === null || decoded_value === undefined) { | ||||
return Promise.resolve(decoded_value); | return Promise.resolve(decoded_value); | ||||
} | } | ||||
if (format === undefined) { | if (format === undefined) { | ||||
return decoded_value; | return decoded_value; | ||||
} | } | ||||
if (format === "human_readable") { | if (format === "human_readable") { | ||||
const date = new Date(decoded_value); | const date = new Date(decoded_value); | ||||
return getDateTime(date); | return getDateTime(date); | ||||
} | } | ||||
return decoded_value[format] ? decoded_value[format] : decoded_value; | return decoded_value[format] ? decoded_value[format] : decoded_value; | ||||
}, | }, | ||||
}; | }; |