What is @google-cloud/precise-date?
The @google-cloud/precise-date npm package provides a way to handle high-precision dates and times in JavaScript, particularly useful when dealing with databases like Google Cloud Spanner that require precise time specifications down to the nanosecond.
What are @google-cloud/precise-date's main functionalities?
Creating precise dates
This feature allows the creation of date objects that include nanosecond precision. It is particularly useful for applications that need to maintain high accuracy in time stamps, such as logging events in a distributed system or transactions in financial services.
const {PreciseDate} = require('@google-cloud/precise-date');
const preciseDate = new PreciseDate('2019-01-01T12:00:00.000000123Z');
console.log(preciseDate.toISOString());
Comparing precise dates
This feature allows for the comparison of two PreciseDate objects, returning -1, 0, or 1 depending on whether the first date is earlier, the same as, or later than the second date. This is useful for sorting or managing time-based data accurately.
const {PreciseDate} = require('@google-cloud/precise-date');
const date1 = new PreciseDate('2020-05-18T12:00:00.000000123Z');
const date2 = new PreciseDate('2020-05-18T12:00:00.000000124Z');
console.log(date1.compare(date2));
Other packages similar to @google-cloud/precise-date
moment
Moment.js is a popular date handling library that provides functions for parsing, validating, manipulating, and formatting dates. While it offers broad functionality for handling dates and times, it does not support nanosecond precision like @google-cloud/precise-date.
date-fns
date-fns is a modular date utility library. Similar to Moment.js, it offers a variety of date manipulation and formatting options but lacks support for nanosecond precision, making @google-cloud/precise-date more suitable for applications requiring extremely precise time measurements.
@google-cloud/precise-date
A simple utility for precise-dateing functions and classes.
Installing the package
It's unlikely you will need to install this package directly, as it will be
installed as a dependency when you install other @google-cloud
packages.
$ npm install --save @google-cloud/precise-date
Using the package
PreciseDate
extends the native Date
object, so you can use it in place of
that or when you need nanosecond precision.
const {PreciseDate} = require('@google-cloud/precise-date');
const date = new PreciseDate('1547253035381101032');
date.toISOString();
date.toFullTimeString();
API
PreciseDate([time])
Returns a new date
instance.
time
Type: string
BigInt
Object<string, number>
[number, number]
date = new PreciseDate('2019-02-08T10:34:29.481145231Z');
date = new PreciseDate('1549622069481320032');
date = new PreciseDate(1549622069481320032n);
date = new PreciseDate({seconds: 1549622069, nanos: 481320032});
date = new PreciseDate([1549622069, 481320032]);
PreciseDate.parseFull(time)
Similar to Date.parse()
, but this accepts the same nanosecond time options as the PreciseDate
constructor and returns a string representing the nanoseconds in the specified date according to universal time.
PreciseDate.parseFull('2019-02-08T10:34:29.481145231Z');
PreciseDate.fullUTCString(...dateFields)
Similar to Date.UTC()
, but also accepts microsecond and nanosecond parameters. Returns a string that represents the number of nanoseconds since January 1, 1970, 00:00:00 UTC.
dateFields
Type: ...number
PreciseDate.fullUTCString(2019, 1, 8, 10, 34, 29, 481, 145, 231);
PreciseDate.fullUTC(...dateFields)
Like PreciseDate.fullUTCString()
but returns a native BigInt
instead of a string. Requires Node >= 10.7.
dateFields
Type: ...number
PreciseDate.fullUTC(2019, 1, 8, 10, 34, 29, 481, 145, 231);
date
PreciseDate
instance.
date.getFullTimeString()
Returns a string of the specified date represented in nanoseconds according to universal time.
date.getFullTime()
Like date.getFullTimeString()
but returns a native BigInt
instead of a string. Requires Node >= 10.7.
date.getMicroseconds()
Returns the microseconds in the specified date according to universal time.
date.getNanoseconds()
Returns the nanoseconds in the specified date according to universal time.
date.setMicroseconds(microseconds)
Sets the microseconds for a specified date according to universal time. Returns a string representing the nanoseconds in the specified date according to universal time.
microseconds
Type: number
date.setNanoseconds(nanoseconds)
Sets the nanoseconds for a specified date according to universal time. Returns a string representing the nanoseconds in the specified date according to universal time.
nanoseconds
Type: number
date.setFullTime(time)
Sets the time to the number of supplied nanoseconds since January 1, 1970, 00:00:00 UTC. Returns a string representing the nanoseconds in the specified date according to universal time (effectively, the value of the argument).
time
Type: number
string
BigInt
date.toStruct()
Returns an object representing the specified date according to universal time.
Refer to google.protobuf.Timestamp
for more information about this format.
const {seconds, nanos} = date.toStruct();
date.toTuple()
Like date.toStruct()
but returns the seconds
and nanos
as a tuple.
const [seconds, nanos] = date.toTuple();
Versioning
This library follows Semantic Versioning.
Contributing
Contributions welcome! See the Contributing Guide.
License
Apache Version 2.0
See LICENSE