diff --git a/README.md b/README.md index 339b4af..065bda0 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,10 @@ npm i greenwich ## Usage ```ts -import { timeParse, timeAdd } from 'greenwich'; +import { timeNow, timeSpanCreate, timeToISOString } from 'greenwich'; + +const t0 = timeNow(); +const t1 = timeAdd(t0, timeSpanCreate({ days: 10 })); + +console.log(timeToISOString(t1)); ``` diff --git a/package.json b/package.json index 5c305c4..67ee78f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "greenwich", - "version": "0.1.0", + "version": "0.1.1", "description": "TypeScript date and time utility library.", "author": "Mark Murray", "license": "MIT", diff --git a/src/__tests__/time-now.test.ts b/src/__tests__/time-now.test.ts new file mode 100644 index 0000000..ad1cad9 --- /dev/null +++ b/src/__tests__/time-now.test.ts @@ -0,0 +1,9 @@ +import { timeCompare, timeNow } from '..'; + +test('can get current time', async () => { + const t1 = timeNow(); + await new Promise((resolve) => setTimeout(resolve, 10)); + const t2 = timeNow(); + + expect(timeCompare(t1, t2)).toEqual(-1); +}); diff --git a/src/index.ts b/src/index.ts index 0c9a694..7e60730 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ export * from './aggregate'; export * from './compare'; export * from './difference'; export * from './format'; +export * from './now'; export * from './parse'; export * from './time'; export * from './truncate'; diff --git a/src/now.ts b/src/now.ts new file mode 100644 index 0000000..c55157c --- /dev/null +++ b/src/now.ts @@ -0,0 +1,16 @@ +import { timeCreate } from './time'; +import { Time } from './types'; + +export const timeNow = (): Time => { + const date = new Date(); + + return timeCreate({ + year: date.getUTCFullYear(), + month: date.getUTCMonth() + 1, + day: date.getUTCDate(), + hour: date.getUTCHours(), + minute: date.getUTCMinutes(), + second: date.getUTCSeconds(), + millisecond: date.getUTCMilliseconds(), + }); +};