connec/yaml-js

why use Date.UTC ?

Closed this issue · 4 comments

image

It's confusing

The YAML 1.1 spec for timestamps says that, when unspecified, the timezone will be UTC:

If the time zone is omitted, the timestamp is assumed to be specified in UTC. The time part may be omitted altogether, resulting in a date format. In such a case, the time part is assumed to be 00:00:00Z (start of day, UTC).

This is different from the behaviour of Date, which will use the system timezone when unspecified.

If you want to be unambiguous you can include the timezone in the YAML:

image

I realised my timestamp formatting was wrong above 🤦‍♂️ It should be 2019-02-02 19:51:00 +8:

image


You could override the default handling if you'd rather have it load in the system TZ, but that will make your documents less portable:

const yaml = require('yaml-js')

// Create a custom "constructor" with the desired handling of timestamps
class MyConstructor extends yaml.constructor.Constructor {}
MyConstructor.add_constructor('tag:yaml.org,2002:timestamp', node => new Date(node.value))

// Create a custom "loader" to use that constructor
const MyLoader = yaml.loader.make_loader(
  undefined, undefined, undefined, undefined, undefined,
  MyConstructor
)

// Load documents using that constructor
console.log(yaml.load('2019-02-02 19:51:00', MyLoader))
// > {date: Sat Feb 02 2019 19:51:00 <system tz>}

Thanks for help

I think you could provide a setting that let us set the default time zone that we want,

cause in many cases the YAML plain text that we use to format might not have a specific time zone, and it might be difficult that we modify the plain text in some reasons.

For example, we have millions of YAML files that their dates didn't have a specific time zone, and it's a huge work that we modify those YAML files.