Skip to content

Commit

Permalink
[no-issue]: Move datetime to tsutils (#4266)
Browse files Browse the repository at this point in the history
  • Loading branch information
kael89 authored Nov 24, 2022
1 parent 8d4e8b9 commit 1c176d2
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/central-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@tupaia/database": "1.0.0",
"@tupaia/dhis-api": "1.0.0",
"@tupaia/expression-parser": "1.0.0",
"@tupaia/tsutils": "1.0.0",
"@tupaia/utils": "1.0.0",
"adm-zip": "^0.5.5",
"api-error-handler": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/central-server/src/apiV2/surveyResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/

import keyBy from 'lodash.keyby';
import { getTimezoneNameFromTimestamp } from '@tupaia/tsutils';
import {
getTimezoneNameFromTimestamp,
ValidationError,
MultiValidationError,
ObjectValidator,
Expand Down
5 changes: 5 additions & 0 deletions packages/tsutils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
"dependencies": {
"@tupaia/utils": "1.0.0",
"cookie": "^0.5.0",
"moment": "^2.24.0",
"moment-timezone": "^0.5.25",
"puppeteer": "^15.4.0",
"yup": "^0.32.9"
},
"devDependencies": {
"@types/moment-timezone": "0.5.13"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Tupaia
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd
* Copyright (c) 2017 - 2022 Beyond Essential Systems Pty Ltd
*/

import moment from 'moment';
Expand Down
22 changes: 22 additions & 0 deletions packages/tsutils/src/datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Tupaia
* Copyright (c) 2017 - 2022 Beyond Essential Systems Pty Ltd
*/

import moment from 'moment';
import momentTimezone from 'moment-timezone';

/**
* @returns utcOffset in format: "+05:00"
*/
const getUtcOffsetFromTimestamp = (timestamp: string) => moment.parseZone(timestamp).format('Z');

/**
* @returns timezone name in format: "Pacific/Fiji".
*/
export const getTimezoneNameFromTimestamp = (timestamp: string) =>
momentTimezone.tz
.names()
.find(name => getUtcOffsetFromTimestamp(timestamp) === momentTimezone.tz(name).format('Z'));

export const utcMoment = (...args: Parameters<typeof moment['utc']>) => moment.utc(...args);
1 change: 1 addition & 0 deletions packages/tsutils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './datetime';
export * from './downloadPageAsPDF';
export * from './hashStringToInt';
export * from './typeGuards';
Expand Down
18 changes: 0 additions & 18 deletions packages/utils/src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@
*/

import moment from 'moment';
import momentTimezone from 'moment-timezone';

/**
* @param {string} timestamp timestamp to get utcOffset from
*
* @returns {string} utcOffset in format: "+05:00"
*/
const getUtcOffsetFromTimestamp = timestamp => moment.parseZone(timestamp).format('Z');

/**
* @param {string} utcOffset utcOffset to match to timezone name
*
* @returns {string} timezone name in format: "Pacific/Fiji".
*/
export const getTimezoneNameFromTimestamp = timestamp =>
momentTimezone.tz
.names()
.find(name => getUtcOffsetFromTimestamp(timestamp) === momentTimezone.tz(name).format('Z'));

/**
* @param {...any} args
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export { DEFAULT_BINARY_OPTIONS, DEFAULT_BINARY_OPTIONS_OBJECT } from './constan
export * from './compare';
export * from './authHeaderBuilders';
export * from './cypress';
export { getTimezoneNameFromTimestamp, utcMoment } from './datetime';
export { utcMoment } from './datetime';
export * from './legacyDhis';
export * from './errors';
export { Multilock } from './Multilock';
Expand Down
1 change: 1 addition & 0 deletions tupaia-packages.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
"cleanup",
"Codeship",
"ctes",
"datetime",
"debouncing",
"deleters",
"dhis",
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5111,6 +5111,7 @@ __metadata:
"@tupaia/database": 1.0.0
"@tupaia/dhis-api": 1.0.0
"@tupaia/expression-parser": 1.0.0
"@tupaia/tsutils": 1.0.0
"@tupaia/utils": 1.0.0
adm-zip: ^0.5.5
api-error-handler: ^1.0.0
Expand Down Expand Up @@ -5637,7 +5638,10 @@ __metadata:
resolution: "@tupaia/tsutils@workspace:packages/tsutils"
dependencies:
"@tupaia/utils": 1.0.0
"@types/moment-timezone": 0.5.13
cookie: ^0.5.0
moment: ^2.24.0
moment-timezone: ^0.5.25
puppeteer: ^15.4.0
yup: ^0.32.9
languageName: unknown
Expand Down Expand Up @@ -6355,6 +6359,15 @@ __metadata:
languageName: node
linkType: hard

"@types/moment-timezone@npm:0.5.13":
version: 0.5.13
resolution: "@types/moment-timezone@npm:0.5.13"
dependencies:
moment: ">=2.14.0"
checksum: 35bd6414b790663e4879e38b4a344526ebe87b3f795929f9bc38f91f7caa373c65dd19a0e836182319f929d0f7fcff9b629efc6dbcef23d386bf0c87597f5b49
languageName: node
linkType: hard

"@types/multer@npm:^1.4.7":
version: 1.4.7
resolution: "@types/multer@npm:1.4.7"
Expand Down Expand Up @@ -24123,6 +24136,13 @@ __metadata:
languageName: node
linkType: hard

"moment@npm:>=2.14.0":
version: 2.29.4
resolution: "moment@npm:2.29.4"
checksum: 0ec3f9c2bcba38dc2451b1daed5daded747f17610b92427bebe1d08d48d8b7bdd8d9197500b072d14e326dd0ccf3e326b9e3d07c5895d3d49e39b6803b76e80e
languageName: node
linkType: hard

"moment@npm:^2.19.3, moment@npm:^2.29.1":
version: 2.29.1
resolution: "moment@npm:2.29.1"
Expand Down

0 comments on commit 1c176d2

Please sign in to comment.