-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added new date wrapper (#1627)
- Loading branch information
1 parent
a84b384
commit 30e21d1
Showing
21 changed files
with
435 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@fuel-ts/account": minor | ||
"@fuel-ts/utils": minor | ||
--- | ||
|
||
Add the DateTime class, which allows for the conversion between common date time formats. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { DateTime } from 'fuels'; | ||
|
||
/** | ||
* @group node | ||
*/ | ||
describe(__filename, () => { | ||
it('should be able to be created from multiple sources', () => { | ||
// #region create-from-multiple-sources | ||
// #import { DateTime }; | ||
|
||
const tai64: DateTime = DateTime.fromTai64('4611686020108779339'); | ||
const unixSeconds: DateTime = DateTime.fromUnixSeconds(1681391398); | ||
const unixMilliseconds: DateTime = DateTime.fromUnixMilliseconds(1681391398000); | ||
// #endregion create-from-multiple-sources | ||
|
||
expect(tai64).toBeDefined(); | ||
expect(tai64.toTai64()).toBe('4611686020108779339'); | ||
expect(unixSeconds).toBeDefined(); | ||
expect(unixSeconds.toUnixSeconds()).toBe(1681391398); | ||
expect(unixMilliseconds).toBeDefined(); | ||
expect(unixMilliseconds.toUnixMilliseconds()).toBe(1681391398000); | ||
}); | ||
|
||
it('should be able to create fromTai64 and convert toTai64', () => { | ||
// #region from-tai-64-and-to-tai-64 | ||
// #import { DateTime }; | ||
|
||
const date: DateTime = DateTime.fromTai64('4611686020108779339'); | ||
// #context console.log(date.toIso); // "4611686020108779339" | ||
|
||
const tai64: string = date.toTai64(); | ||
// #context console.log(tai64); // "4611686020108779339" | ||
// #endregion from-tai-64-and-to-tai-64 | ||
|
||
expect(date).toBeDefined(); | ||
expect(date.toISOString()).toEqual('2023-04-13T13:09:58.000Z'); | ||
expect(tai64).toEqual('4611686020108779339'); | ||
}); | ||
|
||
it('should be able to create fromUnixMilliseconds and convert toUnixMilliseconds', () => { | ||
// #region from-unix-milliseconds-and-to-unix-milliseconds | ||
// #import { DateTime }; | ||
|
||
const date: DateTime = DateTime.fromUnixMilliseconds(1681391398000); | ||
|
||
const unixMilliseconds: number = date.toUnixMilliseconds(); | ||
// #context console.log(unixMilliseconds); // 1681391398000 | ||
// #endregion from-unix-milliseconds-and-to-unix-milliseconds | ||
|
||
expect(date).toBeDefined(); | ||
expect(unixMilliseconds).toEqual(1681391398000); | ||
}); | ||
|
||
it('should be able to create fromUnixSeconds and convert toUnixSeconds', () => { | ||
// #region from-unix-seconds-and-to-unix-seconds | ||
// #import { DateTime }; | ||
|
||
const date: DateTime = DateTime.fromUnixSeconds(1681391398); | ||
|
||
const unixSeconds: number = date.toUnixSeconds(); | ||
// #context console.log(unixSeconds); // 1681391398 | ||
// #endregion from-unix-seconds-and-to-unix-seconds | ||
|
||
expect(date).toBeDefined(); | ||
expect(unixSeconds).toEqual(1681391398); | ||
}); | ||
|
||
/** | ||
* Utility methods | ||
*/ | ||
it('should extend the Date class', () => { | ||
// #region date-object-methods | ||
// #import { DateTime }; | ||
|
||
const dateTime: DateTime = DateTime.fromUnixMilliseconds(1681391398000); | ||
|
||
// Extends the Date object | ||
const date: Date = dateTime; | ||
|
||
// Date object methods | ||
date.getTime(); // 1681391398000 | ||
date.toISOString(); // 2023-04-13T13:09:58.000Z | ||
date.toDateString(); // Thu Apr 13 2023 | ||
// #endregion date-object-methods | ||
|
||
expect(dateTime).toBeDefined(); | ||
expect(date).toBeInstanceOf(Date); | ||
expect(date.getTime()).toEqual(1681391398000); | ||
expect(date.toISOString()).toEqual('2023-04-13T13:09:58.000Z'); | ||
expect(date.toDateString()).toEqual('Thu Apr 13 2023'); | ||
expect(date.toISOString()).toEqual(dateTime.toISOString()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,3 +295,5 @@ AbstractAddress | |
ContractFactory | ||
ScriptTransactionRequest | ||
CDN | ||
DateTime | ||
Atomique |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# DateTime | ||
|
||
To allow for easier manipulation of date and time, the SDK exports the `DateTime` class which is a wrapper around the [built-in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) `Date` class. Below we will go over the methods of instantiation, utility functions and time formats. | ||
|
||
Internally the transactions and other time/date assets are encoded using the [`TAI64`](#tai-format) format. We return a `DateTime` class, to allow of easier conversion and formatting between the two formats. | ||
|
||
## Instantiating a `DateTime` | ||
|
||
We have a host of static method for **instantiation** of our `DateTime` class. | ||
|
||
<<< @/../../docs-snippets/src/guide/types/date-time.test.ts#create-from-multiple-sources{ts:line-numbers} | ||
|
||
### TAI64 | ||
|
||
`fromTai64` is a _static_ method, that allows the creation of `DateTime` class from a `TAI64` string. | ||
|
||
`toTai64` is an _instance_ method, that allows the conversion of a `DateTime` class to a `TAI64` string. | ||
|
||
<<< @/../../docs-snippets/src/guide/types/date-time.test.ts#from-tai-64-and-to-tai-64{ts:line-numbers} | ||
|
||
### UNIX | ||
|
||
`fromUnixMilliseconds` is a _static_ method, that allows the creation of `DateTime` class from a UNIX Milliseconds number. | ||
|
||
`toUnixMilliseconds` is an _instance_ method, that allows the conversion of a `DateTime` class to a `UNIX` number in milliseconds. | ||
|
||
<<< @/../../docs-snippets/src/guide/types/date-time.test.ts#from-unix-milliseconds-and-to-unix-milliseconds{ts:line-numbers} | ||
|
||
`fromUnixSeconds` is a _static_ method, that allows the creation of `DateTime` class from a UNIX Seconds number. | ||
|
||
`toUnixSeconds` is an _instance_ method, that allows the conversion of a `DateTime` class to a `UNIX` number in seconds. | ||
|
||
<<< @/../../docs-snippets/src/guide/types/date-time.test.ts#from-unix-seconds-and-to-unix-seconds{ts:line-numbers} | ||
|
||
### Date | ||
|
||
The `DateTime` class extends the functionality of the `Date` object, so all method are available for your usages. | ||
|
||
<<< @/../../docs-snippets/src/guide/types/date-time.test.ts#date-object-methods{ts:line-numbers} | ||
|
||
## Formats | ||
|
||
Here we will go over the different date/time formats that we use in the SDK. Internally the blockchain uses the `TAI64` format, but we also support the `UNIX` format for ease of use. | ||
|
||
### UNIX Format | ||
|
||
UNIX time is the number of seconds that have elapsed since **00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970**, minus leap seconds. Every day is treated as if it contains exactly 86400 seconds, so leap seconds are ignored. | ||
|
||
### TAI Format | ||
|
||
TAI stands for _Temps Atomique International_ and is the current international real-time standard [Source](https://cr.yp.to/libtai/tai64.html). | ||
|
||
We use `TAI64` is a 64-bit integer representing the number of nanoseconds since the epoch. | ||
|
||
- the TAI second beginning exactly _(2^62 - s) seconds_ before the beginning of 1970 TAI, if s is between 0 inclusive and 2^62 exclusive; or | ||
|
||
- the TAI second beginning exactly _(2^62 + s) seconds_ after the beginning of 1970 TAI, if s is between -2^62 inclusive and 0 exclusive. | ||
|
||
[Source](https://cr.yp.to/libtai/tai64.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
packages/account/src/providers/transaction-summary/date.test.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.