Skip to content

Commit

Permalink
Mapping addition to parse unix timestamp millisecond input GH-371
Browse files Browse the repository at this point in the history
Merge pull request #372 from kendraio/parseUnixTimestamp-millisecond-input-GH-371
  • Loading branch information
lukestanley committed Jul 11, 2023
2 parents 64f92e6 + c61e6b1 commit 8df9b79
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
6 changes: 5 additions & 1 deletion docs/workflow/blocks/mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,13 @@ Parses a Unix timestamp and returns an ISO 8601 date string.

.. code-block:: javascript
parseUnixTimestamp(`0`)
parseUnixTimestamp(`0`) // Seconds
Output: "1970-01-01T01:00:00.000+01:00"

.. code-block:: javascript
parseUnixTimestamp(`1589756000000`, `ms`) // Milliseconds
Output: "2020-05-19T03:00:00.000+01:00"

16 changes: 16 additions & 0 deletions src/app/blocks/mapping-block/mapping-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,21 @@ describe('MappingUtil', () => {
expect(mappingUtility(data, expr1)).toBe(expected1);
expect(mappingUtility(data, expr2)).toBe(expected2);
});

it('should parse Unix millisecond timestamp and return date string', () => {
const data = { data: { timestamp: 1589756000000, nothing: 0 } };
const expr1 = "parseUnixTimestamp(data.timestamp, 'ms')";
const expected1 = DateTime.fromMillis(data.data.timestamp).toISO();

expect(mappingUtility(data, expr1)).toBe(expected1);
});

it('should parse Unix millisecond timestamp and return date string with format argument', () => {
const data = { data: { timestamp: 1589756000000, nothing: 0 } };
const expr1 = "parseUnixTimestamp(data.timestamp, 'milliseconds')";
const expected1 = DateTime.fromMillis(data.data.timestamp).toISO();

expect(mappingUtility(data, expr1)).toBe(expected1);
});
});

8 changes: 4 additions & 4 deletions src/app/blocks/mapping-block/mapping-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,16 @@ const search = decorate({
_signature: [{types: [TYPE_NUMBER, TYPE_STRING, TYPE_NULL]}]
},
parseUnixTimestamp: {
_func: ([n]) => {
if (isNull(n)) {
return null;
_func: ([n, format]) => {
if (format === 'ms' || format === 'milliseconds') {
n = n / 1000;
}
if (isNumber(n)) {
return DateTime.fromSeconds(n).toISO();
}
return null;
},
_signature: [{ types: [TYPE_NUMBER, TYPE_NULL] }]
_signature: [{ types: [TYPE_NUMBER, TYPE_NULL] }, { types: [TYPE_NUMBER, TYPE_STRING], optional: true }]
},
});

Expand Down

1 comment on commit 8df9b79

@vercel
Copy link

@vercel vercel bot commented on 8df9b79 Jul 11, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

kendraio-app – ./

kendraio-app-git-develop-kendraio.vercel.app
kendraio-app-kendraio.vercel.app

Please sign in to comment.