Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function to wrap block_timestamp in PlainDateTime object #498

Merged
merged 4 commits into from
Apr 12, 2021

Conversation

ashutoshvarma
Copy link
Contributor

Summary

assemblyscript-temporal is tc39-temporal based featureful datetime assemblyscipt library.

This PR integrates the above library with near-sdk-as.

Changes

This PR introduces a new namespace datetime which has block_datetime() function.

block_datetime() - Current block date time. Returns PlainDateTime object initializes with current block timestamp.

Example

import { datetime } from "near-sdk-as";
import { PlainDateTime } from "assemblyscript-temporal";

const blk_time = datetime.block_datetime();
// log(blk_time.toString());  // 2021-04-11T12:52:12.3425805

const blk_time_after_1hr = blk_time.add({ hour: 1 });
// log(blk_time.toString());  // 2021-04-11T13:52:12.3425805

Additional Notes
Temporarily we need to use this ugly import to access UTC getter methods on Date.
https://github.com/ashutoshvarma/near-sdk-as/blob/3070ae69852e5376236ae1bc7cef6f74a3bc1ac8/sdk-core/assembly/datetime.ts#L6

But once this PR AssemblyScript/assemblyscript#1768 is merged, then it can be safely removed.

so that users won't need to know how it is initialized.

Signed-off-by: Ashutosh Varma <ashutoshvarma11@live.com>
@ashutoshvarma
Copy link
Contributor Author

This is weird 🤔
For some reason, a full temporal library is included in every wat.

FAIL __tests__/empty.spec.ts
info Visit https://yarnpkg.com/en/docs/cli/workspaces for documentation about this command.
  ● empty wat › should be mostly empty
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

    expect(received).toBe(expected) // Object.is equality

    - Expected  -   3
    + Received  + 702

      (module
    -  (memory $0 0)
    -  (table $0 1 funcref)
    -  (export "memory" (memory $0))
    +  (type $i32_i32_=>_none (func (param i32 i32)))
    +  (type $none_=>_none (func))
    +  (type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
    +  (type $i32_i32_i32_=>_none (func (param i32 i32 i32)))
    +  (type $i32_=>_none (func (param i32)))
    +  (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
    +  (type $i32_=>_i32 (func (param i32) (result i32)))
    +  (type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
    +  (import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
    +  (memory $0 1)
    +  (data (i32.const 12) ",\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\1c\00\00\00I\00n\00v\00a\00l\00i\00d\00 \00l\00e\00n\00g\00t\00h\00")
    +  (data (i32.const 60) ",\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s\00\00\00")
    +  (data (i32.const 108) "<\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00(\00\00\00A\00l\00l\00o\00c\00a\00t\00i\00o\00n\00 \00t\00o\00o\00 \00l\00a\00r\00g\00e\00\00\00\00\00")
    +  (data (i32.const 172) "<\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00s\00t\00u\00b\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
    +  (table $0 1 funcref)
    +  (global $~lib/assemblyscript-temporal/constants/MILLIS_PER_DAY i32 (i32.const 86400000))
    +  (global $~lib/assemblyscript-temporal/constants/MILLIS_PER_HOUR i32 (i32.const 3600000))
    +  (global $~lib/assemblyscript-temporal/constants/MILLIS_PER_MINUTE i32 (i32.const 60000))
    +  (global $~lib/assemblyscript-temporal/constants/MILLIS_PER_SECOND i32 (i32.const 1000))
    +  (global $~lib/assemblyscript-temporal/constants/MICROS_PER_SECOND i32 (i32.const 1000000))
    +  (global $~lib/assemblyscript-temporal/constants/NANOS_PER_SECOND i32 (i32.const 1000000000))
    +  (global $~lib/assemblyscript-temporal/utils/YEAR_MIN i32 (i32.const -271821))
    +  (global $~lib/assemblyscript-temporal/utils/YEAR_MAX i32 (i32.const 275760))
    +  (global $~lib/assemblyscript-temporal/utils/__null (mut i32) (i32.const 0))
    +  (global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
    +  (global $~lib/rt/stub/offset (mut i32) (i32.const 0))
    +  (global $~lib/ASC_SHRINK_LEVEL i32 (i32.const 0))
    +  (global $~lib/assemblyscript-regex/parser/node/emptyNodeArray (mut i32) (i32.const 0))
    +  (global $~lib/assemblyscript-regex/parser/node/_id (mut i32) (i32.const 0))
    +  (global $~lib/assemblyscript-regex/nfa/matcher/_flags (mut i32) (i32.const 0))
    +  (global $~lib/assemblyscript-regex/nfa/matcher/LOWERCASE_LETTERS (mut i32) (i32.const 0))
    +  (global $~lib/assemblyscript-regex/nfa/matcher/UPPERCASE_LETTERS (mut i32) (i32.const 0))
    +  (global $~lib/assemblyscript-regex/nfa/matcher/UPPER_LOWER_OFFSET i32 (i32.const 32))
    +  (global $~lib/assemblyscript-regex/nfa/nfa/MatchResult.Match i32 (i32.const 0))
    +  (global $~lib/assemblyscript-regex/nfa/nfa/MatchResult.Fail i32 (i32.const 1))
    +  (global $~lib/assemblyscript-regex/nfa/nfa/MatchResult.Ignore i32 (i32.const 2))
    +  (global $~lib/assemblyscript-regex/nfa/nfa/_stateId (mut i32) (i32.const 0))
    +  (global $~lib/assemblyscript-regex/parser/walker/QUANTIFIER_LIMIT i32 (i32.const 1000))
    +  (global $~lib/assemblyscript-regex/regexp/gm (mut i32) (i32.const 0))
    +  (global $../node_modules/assemblyscript-temporal/assembly/constants/MILLIS_PER_DAY i32 (i32.const 86400000))
    +  (global $../node_modules/assemblyscript-temporal/assembly/constants/MILLIS_PER_HOUR i32 (i32.const 3600000))

sdk/package.json Outdated Show resolved Hide resolved
@ashutoshvarma
Copy link
Contributor Author

ashutoshvarma commented Apr 12, 2021

empty.spec.ts is failing due to global variables exposed in assemblyscript-temporal and assemblyscript-regex.
Here are the PRs which decorate the global variables with @lazy to fix this

Once these are merged, the test should pass, for now, it is disabled with xit

@ashutoshvarma ashutoshvarma marked this pull request as ready for review April 12, 2021 21:16
Copy link
Contributor

@willemneal willemneal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a small nit in the comment.

sdk-core/assembly/datetime.ts Outdated Show resolved Hide resolved
Co-authored-by: Willem Wyndham <willem@cs.umd.edu>
@willemneal willemneal merged commit 1fcfda9 into near:master Apr 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants