@opentripplanner/core-utils 4.9.0 → 4.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/time.js +25 -2
- package/esm/time.js.map +1 -1
- package/lib/time.js +28 -1
- package/lib/time.js.map +1 -1
- package/package.json +1 -1
- package/src/time.js +26 -1
package/esm/time.js
CHANGED
|
@@ -52,12 +52,27 @@ function formatDurationLikeMoment(seconds, showSeconds) {
|
|
|
52
52
|
locale: locale
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Breaks up a duration in seconds into hours, minutes, and seconds.
|
|
57
|
+
* @param {number} seconds The number of seconds to break up
|
|
58
|
+
* @returns an object with fields with the corresponding, hours, minutes, seconds.
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
export function toHoursMinutesSeconds(seconds) {
|
|
63
|
+
var hours = Math.floor(seconds / 3600);
|
|
64
|
+
var minutes = Math.floor((seconds - hours * 3600) / 60);
|
|
65
|
+
return {
|
|
66
|
+
hours: hours,
|
|
67
|
+
minutes: minutes,
|
|
68
|
+
seconds: seconds - hours * 3600 - minutes * 60
|
|
69
|
+
};
|
|
70
|
+
}
|
|
55
71
|
/**
|
|
56
72
|
* @param {[type]} config the OTP config object found in store
|
|
57
73
|
* @return {string} the config-defined time formatter or HH:mm (24-hr time)
|
|
58
74
|
*/
|
|
59
75
|
|
|
60
|
-
|
|
61
76
|
export function getTimeFormat(config) {
|
|
62
77
|
var _config$dateTime;
|
|
63
78
|
|
|
@@ -96,6 +111,14 @@ export function formatDurationWithSeconds(seconds, region) {
|
|
|
96
111
|
code: region
|
|
97
112
|
});
|
|
98
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Offsets a time according to the provided time options
|
|
116
|
+
* and returns the result.
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
export function offsetTime(ms, options) {
|
|
120
|
+
return ms + ((options === null || options === void 0 ? void 0 : options.offset) || 0);
|
|
121
|
+
}
|
|
99
122
|
/**
|
|
100
123
|
* Formats a time value for display in narrative
|
|
101
124
|
* TODO: internationalization/timezone
|
|
@@ -104,7 +127,7 @@ export function formatDurationWithSeconds(seconds, region) {
|
|
|
104
127
|
*/
|
|
105
128
|
|
|
106
129
|
export function formatTime(ms, options) {
|
|
107
|
-
return format(ms
|
|
130
|
+
return format(offsetTime(ms, options), (options === null || options === void 0 ? void 0 : options.format) || OTP_API_TIME_FORMAT);
|
|
108
131
|
}
|
|
109
132
|
/**
|
|
110
133
|
* Formats a seconds after midnight value for display in narrative
|
package/esm/time.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/time.js"],"names":["startOfDay","add","format","formatDuration","dateFnsFormatDuration","utcToZonedTime","OTP_API_DATE_FORMAT","OTP_API_DATE_FORMAT_DATE_FNS","OTP_API_TIME_FORMAT","formatDurationLikeMoment","seconds","showSeconds","localize","enabled","code","hours","Math","floor","minutes","secondsLeftOver","specLookup","xHours","xMinutes","xSeconds","locale","formatDistance","spec","val","undefined","getTimeFormat","config","dateTime","timeFormat","getDateFormat","dateFormat","getLongDateFormat","longDateFormat","formatDurationWithSeconds","region","formatTime","ms","options","offset","formatSecondsAfterMidnight","time","Date","getUserTimezone","fallbackTimezone","process","env","NODE_ENV","TZ","Intl","DateTimeFormat","resolvedOptions","timeZone","getCurrentTime","timezone","now","getCurrentDate"],"mappings":"AAAA,SACEA,UADF,EAEEC,GAFF,EAGEC,MAHF,EAIEC,cAAc,IAAIC,qBAJpB,QAKO,UALP;AAMA,SAASC,cAAT,QAA+B,aAA/B,C,CAEA;AACA;;AACA,OAAO,IAAMC,mBAAmB,GAAG,YAA5B,C,CACP;AACA;;AACA,OAAO,IAAMC,4BAA4B,GAAG,YAArC;AACP,OAAO,IAAMC,mBAAmB,GAAG,OAA5B;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,wBAAT,CACEC,O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AACA,OAAO,SAASmC,0BAAT,CAAoCjC,OAApC,EAA6CsB,UAA7C,EAAyD;AAC9D,MAAMY,IAAI,GAAG3C,GAAG,CAACD,UAAU,CAAC,IAAI6C,IAAJ,EAAD,CAAX,EAAyB;AAAEnC,IAAAA,OAAO,EAAPA;AAAF,GAAzB,CAAhB;AACA,SAAOR,MAAM,CAAC0C,IAAD,EAAOZ,UAAP,CAAb;AACD;AAED;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASc,eAAT,GAA6D;AAAA;;AAAA,MAApCC,gBAAoC,uEAAjB,eAAiB;AAClE,MAAIC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,MAA7B,EAAqC,OAAOF,OAAO,CAACC,GAAR,CAAYE,EAAnB;AACrC,SAAO,UAAAC,IAAI,UAAJ,sCAAMC,cAAN,GAAuBC,eAAvB,GAAyCC,QAAzC,KAAqDR,gBAA5D;AACD;AAED;AACA;AACA;AACA;;AACA,OAAO,SAASS,cAAT,GAAsD;AAAA,MAA9BC,QAA8B,uEAAnBX,eAAe,EAAI;AAC3D,SAAO5C,MAAM,CAACG,cAAc,CAACwC,IAAI,CAACa,GAAL,EAAD,EAAaD,QAAb,CAAf,EAAuCjD,mBAAvC,CAAb;AACD;AAED;AACA;AACA;AACA;;AACA,OAAO,SAASmD,cAAT,GAAsD;AAAA,MAA9BF,QAA8B,uEAAnBX,eAAe,EAAI;AAC3D,SAAO5C,MAAM,CACXG,cAAc,CAACwC,IAAI,CAACa,GAAL,EAAD,EAAaD,QAAb,CADH,EAEXlD,4BAFW,CAAb;AAID","sourcesContent":["import {\n startOfDay,\n add,\n format,\n formatDuration as dateFnsFormatDuration\n} from \"date-fns\";\nimport { utcToZonedTime } from \"date-fns-tz\";\n\n// special constants for making sure the following date format is always sent to\n// OTP regardless of whatever the user has configured as the display format\nexport const OTP_API_DATE_FORMAT = \"YYYY-MM-DD\";\n// Date-Fns uses a different string format than moment.js\n// see https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md\nexport const OTP_API_DATE_FORMAT_DATE_FNS = \"yyyy-MM-dd\";\nexport const OTP_API_TIME_FORMAT = \"HH:mm\";\n\n/**\n * To ease the transition away from moment.js, this method uses date-fns to format durations\n * the way moment.js did.\n * @param {number} seconds The number of seconds to format\n * @param {boolean} showSeconds Whether to render seconds or not\n * @param {boolean} localize If true, will create output like moment.js using date-fns locale.\n * Otherwise, uses date-fns default\n * @returns Formatted duration\n */\nfunction formatDurationLikeMoment(\n seconds,\n showSeconds,\n localize = { enabled: true, code: \"en-US\" }\n) {\n // date-fns doesn't do this automatically\n if ((!showSeconds && seconds < 60) || seconds === 0) {\n return \"0 min\";\n }\n\n const hours = Math.floor(seconds / 3600);\n const minutes = Math.floor((seconds - hours * 3600) / 60);\n const secondsLeftOver = showSeconds\n ? seconds - hours * 3600 - minutes * 60\n : 0;\n const specLookup = {\n xHours: \"hr\",\n xMinutes: \"min\",\n xSeconds: \"sec\"\n };\n const locale = localize\n ? {\n // Maintain backwards compatibility when called with localize=true\n code: localize?.code || \"en-US\",\n formatDistance: (spec, val) => {\n return `${val} ${specLookup[spec]}`;\n }\n }\n : undefined;\n\n return dateFnsFormatDuration(\n {\n hours,\n minutes,\n seconds: secondsLeftOver\n },\n {\n format: [\"hours\", \"minutes\", \"seconds\"],\n locale\n }\n );\n}\n/**\n * @param {[type]} config the OTP config object found in store\n * @return {string} the config-defined time formatter or HH:mm (24-hr time)\n */\nexport function getTimeFormat(config) {\n return config?.dateTime?.timeFormat || OTP_API_TIME_FORMAT;\n}\n\nexport function getDateFormat(config) {\n return config?.dateTime?.dateFormat || OTP_API_DATE_FORMAT;\n}\n\nexport function getLongDateFormat(config) {\n return config?.dateTime?.longDateFormat || \"D MMMM YYYY\";\n}\n\n/**\n * Formats an elapsed time duration for display in narrative.\n * TODO: internationalization\n * @param {number} seconds duration in seconds\n * @returns {string} formatted text representation\n */\nexport function formatDuration(seconds) {\n return formatDurationLikeMoment(seconds, false);\n}\n\n/**\n * Formats an elapsed time in seconds, minutes, hours duration for display in narrative\n * TODO: internationalization\n * @param {number} seconds duration in seconds\n * @returns {string} formatted text representation\n */\nexport function formatDurationWithSeconds(seconds, region) {\n return formatDurationLikeMoment(seconds, { enabled: true, code: region });\n}\n/**\n * Formats a time value for display in narrative\n * TODO: internationalization/timezone\n * @param {number} ms epoch time value in milliseconds\n * @returns {string} formatted text representation\n */\nexport function formatTime(ms, options) {\n return format(\n ms + (options?.offset || 0),\n options?.format || OTP_API_TIME_FORMAT\n );\n}\n\n/**\n * Formats a seconds after midnight value for display in narrative\n * @param {number} seconds time since midnight in seconds\n * @param {string} timeFormat A valid date-fns time format\n * @return {string} formatted text representation\n */\nexport function formatSecondsAfterMidnight(seconds, timeFormat) {\n const time = add(startOfDay(new Date()), { seconds });\n return format(time, timeFormat);\n}\n\n/**\n * Uses Intl.DateTimeFormat() api to get the user's time zone. In a test\n * environment, pulls timezone information from an env variable. Default to\n * GMT+0 if the Intl API is unavailable.\n */\nexport function getUserTimezone(fallbackTimezone = \"Etc/Greenwich\") {\n if (process.env.NODE_ENV === \"test\") return process.env.TZ;\n return Intl?.DateTimeFormat().resolvedOptions().timeZone || fallbackTimezone;\n}\n\n/**\n * Formats current time for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentTime(timezone = getUserTimezone()) {\n return format(utcToZonedTime(Date.now(), timezone), OTP_API_TIME_FORMAT);\n}\n\n/**\n * Formats current date for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentDate(timezone = getUserTimezone()) {\n return format(\n utcToZonedTime(Date.now(), timezone),\n OTP_API_DATE_FORMAT_DATE_FNS\n );\n}\n"],"file":"time.js"}
|
|
1
|
+
{"version":3,"sources":["../src/time.js"],"names":["startOfDay","add","format","formatDuration","dateFnsFormatDuration","utcToZonedTime","OTP_API_DATE_FORMAT","OTP_API_DATE_FORMAT_DATE_FNS","OTP_API_TIME_FORMAT","formatDurationLikeMoment","seconds","showSeconds","localize","enabled","code","hours","Math","floor","minutes","secondsLeftOver","specLookup","xHours","xMinutes","xSeconds","locale","formatDistance","spec","val","undefined","toHoursMinutesSeconds","getTimeFormat","config","dateTime","timeFormat","getDateFormat","dateFormat","getLongDateFormat","longDateFormat","formatDurationWithSeconds","region","offsetTime","ms","options","offset","formatTime","formatSecondsAfterMidnight","time","Date","getUserTimezone","fallbackTimezone","process","env","NODE_ENV","TZ","Intl","DateTimeFormat","resolvedOptions","timeZone","getCurrentTime","timezone","now","getCurrentDate"],"mappings":"AAAA,SACEA,UADF,EAEEC,GAFF,EAGEC,MAHF,EAIEC,cAAc,IAAIC,qBAJpB,QAKO,UALP;AAMA,SAASC,cAAT,QAA+B,aAA/B,C,CAEA;AACA;;AACA,OAAO,IAAMC,mBAAmB,GAAG,YAA5B,C,CACP;AACA;;AACA,OAAO,IAAMC,4BAA4B,GAAG,YAArC;AACP,OAAO,IAAMC,mBAAmB,GAAG,OAA5B;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,wBAAT,CACEC,OADF,EAEEC,WAFF,EAIE;AAAA,MADAC,QACA,uEADW;AAAEC,IAAAA,OAAO,EAAE,IAAX;AAAiBC,IAAAA,IAAI,EAAE;AAAvB,GACX;;AACA;AACA,MAAK,CAACH,WAAD,IAAgBD,OAAO,GAAG,EAA3B,IAAkCA,OAAO,KAAK,CAAlD,EAAqD;AACnD,WAAO,OAAP;AACD;;AAED,MAAMK,KAAK,GAAGC,IAAI,CAACC,KAAL,CAAWP,OAAO,GAAG,IAArB,CAAd;AACA,MAAMQ,OAAO,GAAGF,IAAI,CAACC,KAAL,CAAW,CAACP,OAAO,GAAGK,KAAK,GAAG,IAAnB,IAA2B,EAAtC,CAAhB;AACA,MAAMI,eAAe,GAAGR,WAAW,GAC/BD,OAAO,GAAGK,KAAK,GAAG,IAAlB,GAAyBG,OAAO,GAAG,EADJ,GAE/B,CAFJ;AAGA,MAAME,UAAU,GAAG;AACjBC,IAAAA,M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ACA,OAAO,SAASqC,0BAAT,CAAoCnC,OAApC,EAA6CuB,UAA7C,EAAyD;AAC9D,MAAMa,IAAI,GAAG7C,GAAG,CAACD,UAAU,CAAC,IAAI+C,IAAJ,EAAD,CAAX,EAAyB;AAAErC,IAAAA,OAAO,EAAPA;AAAF,GAAzB,CAAhB;AACA,SAAOR,MAAM,CAAC4C,IAAD,EAAOb,UAAP,CAAb;AACD;AAED;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASe,eAAT,GAA6D;AAAA;;AAAA,MAApCC,gBAAoC,uEAAjB,eAAiB;AAClE,MAAIC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,MAA7B,EAAqC,OAAOF,OAAO,CAACC,GAAR,CAAYE,EAAnB;AACrC,SAAO,UAAAC,IAAI,UAAJ,sCAAMC,cAAN,GAAuBC,eAAvB,GAAyCC,QAAzC,KAAqDR,gBAA5D;AACD;AAED;AACA;AACA;AACA;;AACA,OAAO,SAASS,cAAT,GAAsD;AAAA,MAA9BC,QAA8B,uEAAnBX,eAAe,EAAI;AAC3D,SAAO9C,MAAM,CAACG,cAAc,CAAC0C,IAAI,CAACa,GAAL,EAAD,EAAaD,QAAb,CAAf,EAAuCnD,mBAAvC,CAAb;AACD;AAED;AACA;AACA;AACA;;AACA,OAAO,SAASqD,cAAT,GAAsD;AAAA,MAA9BF,QAA8B,uEAAnBX,eAAe,EAAI;AAC3D,SAAO9C,MAAM,CACXG,cAAc,CAAC0C,IAAI,CAACa,GAAL,EAAD,EAAaD,QAAb,CADH,EAEXpD,4BAFW,CAAb;AAID","sourcesContent":["import {\n startOfDay,\n add,\n format,\n formatDuration as dateFnsFormatDuration\n} from \"date-fns\";\nimport { utcToZonedTime } from \"date-fns-tz\";\n\n// special constants for making sure the following date format is always sent to\n// OTP regardless of whatever the user has configured as the display format\nexport const OTP_API_DATE_FORMAT = \"YYYY-MM-DD\";\n// Date-Fns uses a different string format than moment.js\n// see https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md\nexport const OTP_API_DATE_FORMAT_DATE_FNS = \"yyyy-MM-dd\";\nexport const OTP_API_TIME_FORMAT = \"HH:mm\";\n\n/**\n * To ease the transition away from moment.js, this method uses date-fns to format durations\n * the way moment.js did.\n * @param {number} seconds The number of seconds to format\n * @param {boolean} showSeconds Whether to render seconds or not\n * @param {boolean} localize If true, will create output like moment.js using date-fns locale.\n * Otherwise, uses date-fns default\n * @returns Formatted duration\n */\nfunction formatDurationLikeMoment(\n seconds,\n showSeconds,\n localize = { enabled: true, code: \"en-US\" }\n) {\n // date-fns doesn't do this automatically\n if ((!showSeconds && seconds < 60) || seconds === 0) {\n return \"0 min\";\n }\n\n const hours = Math.floor(seconds / 3600);\n const minutes = Math.floor((seconds - hours * 3600) / 60);\n const secondsLeftOver = showSeconds\n ? seconds - hours * 3600 - minutes * 60\n : 0;\n const specLookup = {\n xHours: \"hr\",\n xMinutes: \"min\",\n xSeconds: \"sec\"\n };\n const locale = localize\n ? {\n // Maintain backwards compatibility when called with localize=true\n code: localize?.code || \"en-US\",\n formatDistance: (spec, val) => {\n return `${val} ${specLookup[spec]}`;\n }\n }\n : undefined;\n\n return dateFnsFormatDuration(\n {\n hours,\n minutes,\n seconds: secondsLeftOver\n },\n {\n format: [\"hours\", \"minutes\", \"seconds\"],\n locale\n }\n );\n}\n\n/**\n * Breaks up a duration in seconds into hours, minutes, and seconds.\n * @param {number} seconds The number of seconds to break up\n * @returns an object with fields with the corresponding, hours, minutes, seconds.\n */\nexport function toHoursMinutesSeconds(seconds) {\n const hours = Math.floor(seconds / 3600);\n const minutes = Math.floor((seconds - hours * 3600) / 60);\n return {\n hours,\n minutes,\n seconds: seconds - hours * 3600 - minutes * 60\n };\n}\n\n/**\n * @param {[type]} config the OTP config object found in store\n * @return {string} the config-defined time formatter or HH:mm (24-hr time)\n */\nexport function getTimeFormat(config) {\n return config?.dateTime?.timeFormat || OTP_API_TIME_FORMAT;\n}\n\nexport function getDateFormat(config) {\n return config?.dateTime?.dateFormat || OTP_API_DATE_FORMAT;\n}\n\nexport function getLongDateFormat(config) {\n return config?.dateTime?.longDateFormat || \"D MMMM YYYY\";\n}\n\n/**\n * Formats an elapsed time duration for display in narrative.\n * TODO: internationalization\n * @param {number} seconds duration in seconds\n * @returns {string} formatted text representation\n */\nexport function formatDuration(seconds) {\n return formatDurationLikeMoment(seconds, false);\n}\n\n/**\n * Formats an elapsed time in seconds, minutes, hours duration for display in narrative\n * TODO: internationalization\n * @param {number} seconds duration in seconds\n * @returns {string} formatted text representation\n */\nexport function formatDurationWithSeconds(seconds, region) {\n return formatDurationLikeMoment(seconds, { enabled: true, code: region });\n}\n\n/**\n * Offsets a time according to the provided time options\n * and returns the result.\n */\nexport function offsetTime(ms, options) {\n return ms + (options?.offset || 0);\n}\n\n/**\n * Formats a time value for display in narrative\n * TODO: internationalization/timezone\n * @param {number} ms epoch time value in milliseconds\n * @returns {string} formatted text representation\n */\nexport function formatTime(ms, options) {\n return format(\n offsetTime(ms, options),\n options?.format || OTP_API_TIME_FORMAT\n );\n}\n\n/**\n * Formats a seconds after midnight value for display in narrative\n * @param {number} seconds time since midnight in seconds\n * @param {string} timeFormat A valid date-fns time format\n * @return {string} formatted text representation\n */\nexport function formatSecondsAfterMidnight(seconds, timeFormat) {\n const time = add(startOfDay(new Date()), { seconds });\n return format(time, timeFormat);\n}\n\n/**\n * Uses Intl.DateTimeFormat() api to get the user's time zone. In a test\n * environment, pulls timezone information from an env variable. Default to\n * GMT+0 if the Intl API is unavailable.\n */\nexport function getUserTimezone(fallbackTimezone = \"Etc/Greenwich\") {\n if (process.env.NODE_ENV === \"test\") return process.env.TZ;\n return Intl?.DateTimeFormat().resolvedOptions().timeZone || fallbackTimezone;\n}\n\n/**\n * Formats current time for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentTime(timezone = getUserTimezone()) {\n return format(utcToZonedTime(Date.now(), timezone), OTP_API_TIME_FORMAT);\n}\n\n/**\n * Formats current date for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentDate(timezone = getUserTimezone()) {\n return format(\n utcToZonedTime(Date.now(), timezone),\n OTP_API_DATE_FORMAT_DATE_FNS\n );\n}\n"],"file":"time.js"}
|
package/lib/time.js
CHANGED
|
@@ -3,11 +3,13 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.toHoursMinutesSeconds = toHoursMinutesSeconds;
|
|
6
7
|
exports.getTimeFormat = getTimeFormat;
|
|
7
8
|
exports.getDateFormat = getDateFormat;
|
|
8
9
|
exports.getLongDateFormat = getLongDateFormat;
|
|
9
10
|
exports.formatDuration = formatDuration;
|
|
10
11
|
exports.formatDurationWithSeconds = formatDurationWithSeconds;
|
|
12
|
+
exports.offsetTime = offsetTime;
|
|
11
13
|
exports.formatTime = formatTime;
|
|
12
14
|
exports.formatSecondsAfterMidnight = formatSecondsAfterMidnight;
|
|
13
15
|
exports.getUserTimezone = getUserTimezone;
|
|
@@ -73,6 +75,22 @@ function formatDurationLikeMoment(seconds, showSeconds, localize = {
|
|
|
73
75
|
locale
|
|
74
76
|
});
|
|
75
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Breaks up a duration in seconds into hours, minutes, and seconds.
|
|
80
|
+
* @param {number} seconds The number of seconds to break up
|
|
81
|
+
* @returns an object with fields with the corresponding, hours, minutes, seconds.
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
function toHoursMinutesSeconds(seconds) {
|
|
86
|
+
const hours = Math.floor(seconds / 3600);
|
|
87
|
+
const minutes = Math.floor((seconds - hours * 3600) / 60);
|
|
88
|
+
return {
|
|
89
|
+
hours,
|
|
90
|
+
minutes,
|
|
91
|
+
seconds: seconds - hours * 3600 - minutes * 60
|
|
92
|
+
};
|
|
93
|
+
}
|
|
76
94
|
/**
|
|
77
95
|
* @param {[type]} config the OTP config object found in store
|
|
78
96
|
* @return {string} the config-defined time formatter or HH:mm (24-hr time)
|
|
@@ -121,6 +139,15 @@ function formatDurationWithSeconds(seconds, region) {
|
|
|
121
139
|
code: region
|
|
122
140
|
});
|
|
123
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Offsets a time according to the provided time options
|
|
144
|
+
* and returns the result.
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
function offsetTime(ms, options) {
|
|
149
|
+
return ms + ((options === null || options === void 0 ? void 0 : options.offset) || 0);
|
|
150
|
+
}
|
|
124
151
|
/**
|
|
125
152
|
* Formats a time value for display in narrative
|
|
126
153
|
* TODO: internationalization/timezone
|
|
@@ -130,7 +157,7 @@ function formatDurationWithSeconds(seconds, region) {
|
|
|
130
157
|
|
|
131
158
|
|
|
132
159
|
function formatTime(ms, options) {
|
|
133
|
-
return (0, _dateFns.format)(ms
|
|
160
|
+
return (0, _dateFns.format)(offsetTime(ms, options), (options === null || options === void 0 ? void 0 : options.format) || OTP_API_TIME_FORMAT);
|
|
134
161
|
}
|
|
135
162
|
/**
|
|
136
163
|
* Formats a seconds after midnight value for display in narrative
|
package/lib/time.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/time.js"],"names":["OTP_API_DATE_FORMAT","OTP_API_DATE_FORMAT_DATE_FNS","OTP_API_TIME_FORMAT","formatDurationLikeMoment","seconds","showSeconds","localize","enabled","code","hours","Math","floor","minutes","secondsLeftOver","specLookup","xHours","xMinutes","xSeconds","locale","formatDistance","spec","val","undefined","format","getTimeFormat","config","dateTime","timeFormat","getDateFormat","dateFormat","getLongDateFormat","longDateFormat","formatDuration","formatDurationWithSeconds","region","formatTime","ms","options","offset","formatSecondsAfterMidnight","time","Date","getUserTimezone","fallbackTimezone","process","env","NODE_ENV","TZ","Intl","DateTimeFormat","resolvedOptions","timeZone","getCurrentTime","timezone","now","getCurrentDate"],"mappings":";;;;;;;;;;;;;;;;;AAAA;;AAMA;;AAEA;AACA;AACO,M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AAO,WAAP,YAAAA,OAAO,CAAEd,MAAT,KAAmBrB,mBAFd,CAAP;AAID;AAED;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASqC,0BAAT,CAAoCnC,OAApC,EAA6CuB,UAA7C,EAAyD;AAC9D,QAAMa,IAAI,GAAG,kBAAI,yBAAW,IAAIC,IAAJ,EAAX,CAAJ,EAA4B;AAAErC,IAAAA;AAAF,GAA5B,CAAb;AACA,SAAO,qBAAOoC,IAAP,EAAab,UAAb,CAAP;AACD;AAED;AACA;AACA;AACA;AACA;;;AACO,SAASe,eAAT,CAAyBC,gBAAgB,GAAG,eAA5C,EAA6D;AAAA;;AAClE,MAAIC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,MAA7B,EAAqC,OAAOF,OAAO,CAACC,GAAR,CAAYE,EAAnB;AACrC,SAAO,UAAAC,IAAI,UAAJ,sCAAMC,cAAN,GAAuBC,eAAvB,GAAyCC,QAAzC,KAAqDR,gBAA5D;AACD;AAED;AACA;AACA;AACA;;;AACO,SAASS,cAAT,CAAwBC,QAAQ,GAAGX,eAAe,EAAlD,EAAsD;AAC3D,SAAO,qBAAO,+BAAeD,IAAI,CAACa,GAAL,EAAf,EAA2BD,QAA3B,CAAP,EAA6CnD,mBAA7C,CAAP;AACD;AAED;AACA;AACA;AACA;;;AACO,SAASqD,cAAT,CAAwBF,QAAQ,GAAGX,eAAe,EAAlD,EAAsD;AAC3D,SAAO,qBACL,+BAAeD,IAAI,CAACa,GAAL,EAAf,EAA2BD,QAA3B,CADK,EAELpD,4BAFK,CAAP;AAID","sourcesContent":["import {\n startOfDay,\n add,\n format,\n formatDuration as dateFnsFormatDuration\n} from \"date-fns\";\nimport { utcToZonedTime } from \"date-fns-tz\";\n\n// special constants for making sure the following date format is always sent to\n// OTP regardless of whatever the user has configured as the display format\nexport const OTP_API_DATE_FORMAT = \"YYYY-MM-DD\";\n// Date-Fns uses a different string format than moment.js\n// see https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md\nexport const OTP_API_DATE_FORMAT_DATE_FNS = \"yyyy-MM-dd\";\nexport const OTP_API_TIME_FORMAT = \"HH:mm\";\n\n/**\n * To ease the transition away from moment.js, this method uses date-fns to format durations\n * the way moment.js did.\n * @param {number} seconds The number of seconds to format\n * @param {boolean} showSeconds Whether to render seconds or not\n * @param {boolean} localize If true, will create output like moment.js using date-fns locale.\n * Otherwise, uses date-fns default\n * @returns Formatted duration\n */\nfunction formatDurationLikeMoment(\n seconds,\n showSeconds,\n localize = { enabled: true, code: \"en-US\" }\n) {\n // date-fns doesn't do this automatically\n if ((!showSeconds && seconds < 60) || seconds === 0) {\n return \"0 min\";\n }\n\n const hours = Math.floor(seconds / 3600);\n const minutes = Math.floor((seconds - hours * 3600) / 60);\n const secondsLeftOver = showSeconds\n ? seconds - hours * 3600 - minutes * 60\n : 0;\n const specLookup = {\n xHours: \"hr\",\n xMinutes: \"min\",\n xSeconds: \"sec\"\n };\n const locale = localize\n ? {\n // Maintain backwards compatibility when called with localize=true\n code: localize?.code || \"en-US\",\n formatDistance: (spec, val) => {\n return `${val} ${specLookup[spec]}`;\n }\n }\n : undefined;\n\n return dateFnsFormatDuration(\n {\n hours,\n minutes,\n seconds: secondsLeftOver\n },\n {\n format: [\"hours\", \"minutes\", \"seconds\"],\n locale\n }\n );\n}\n/**\n * @param {[type]} config the OTP config object found in store\n * @return {string} the config-defined time formatter or HH:mm (24-hr time)\n */\nexport function getTimeFormat(config) {\n return config?.dateTime?.timeFormat || OTP_API_TIME_FORMAT;\n}\n\nexport function getDateFormat(config) {\n return config?.dateTime?.dateFormat || OTP_API_DATE_FORMAT;\n}\n\nexport function getLongDateFormat(config) {\n return config?.dateTime?.longDateFormat || \"D MMMM YYYY\";\n}\n\n/**\n * Formats an elapsed time duration for display in narrative.\n * TODO: internationalization\n * @param {number} seconds duration in seconds\n * @returns {string} formatted text representation\n */\nexport function formatDuration(seconds) {\n return formatDurationLikeMoment(seconds, false);\n}\n\n/**\n * Formats an elapsed time in seconds, minutes, hours duration for display in narrative\n * TODO: internationalization\n * @param {number} seconds duration in seconds\n * @returns {string} formatted text representation\n */\nexport function formatDurationWithSeconds(seconds, region) {\n return formatDurationLikeMoment(seconds, { enabled: true, code: region });\n}\n/**\n * Formats a time value for display in narrative\n * TODO: internationalization/timezone\n * @param {number} ms epoch time value in milliseconds\n * @returns {string} formatted text representation\n */\nexport function formatTime(ms, options) {\n return format(\n ms + (options?.offset || 0),\n options?.format || OTP_API_TIME_FORMAT\n );\n}\n\n/**\n * Formats a seconds after midnight value for display in narrative\n * @param {number} seconds time since midnight in seconds\n * @param {string} timeFormat A valid date-fns time format\n * @return {string} formatted text representation\n */\nexport function formatSecondsAfterMidnight(seconds, timeFormat) {\n const time = add(startOfDay(new Date()), { seconds });\n return format(time, timeFormat);\n}\n\n/**\n * Uses Intl.DateTimeFormat() api to get the user's time zone. In a test\n * environment, pulls timezone information from an env variable. Default to\n * GMT+0 if the Intl API is unavailable.\n */\nexport function getUserTimezone(fallbackTimezone = \"Etc/Greenwich\") {\n if (process.env.NODE_ENV === \"test\") return process.env.TZ;\n return Intl?.DateTimeFormat().resolvedOptions().timeZone || fallbackTimezone;\n}\n\n/**\n * Formats current time for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentTime(timezone = getUserTimezone()) {\n return format(utcToZonedTime(Date.now(), timezone), OTP_API_TIME_FORMAT);\n}\n\n/**\n * Formats current date for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentDate(timezone = getUserTimezone()) {\n return format(\n utcToZonedTime(Date.now(), timezone),\n OTP_API_DATE_FORMAT_DATE_FNS\n );\n}\n"],"file":"time.js"}
|
|
1
|
+
{"version":3,"sources":["../src/time.js"],"names":["OTP_API_DATE_FORMAT","OTP_API_DATE_FORMAT_DATE_FNS","OTP_API_TIME_FORMAT","formatDurationLikeMoment","seconds","showSeconds","localize","enabled","code","hours","Math","floor","minutes","secondsLeftOver","specLookup","xHours","xMinutes","xSeconds","locale","formatDistance","spec","val","undefined","format","toHoursMinutesSeconds","getTimeFormat","config","dateTime","timeFormat","getDateFormat","dateFormat","getLongDateFormat","longDateFormat","formatDuration","formatDurationWithSeconds","region","offsetTime","ms","options","offset","formatTime","formatSecondsAfterMidnight","time","Date","getUserTimezone","fallbackTimezone","process","env","NODE_ENV","TZ","Intl","DateTimeFormat","resolvedOptions","timeZone","getCurrentTime","timezone","now","getCurrentDate"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;;AAMA;;AAEA;AACA;AACO,MAAMA,mBAAmB,GAAG,YAA5B,C,CACP;AACA;;;AACO,MAAMC,4BAA4B,GAAG,YAArC;;AACA,MAAMC,mBAAmB,GAAG,OAA5B;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;AACA,SAASC,wBAAT,CACEC,OADF,EAEEC,WAFF,EAGEC,QAAQ,GAAG;AAAEC,EAAAA,OAAO,EAAE,IAAX;AAAiBC,EAAAA,IAAI,EAAE;AAAvB,CAHb,EAIE;AACA;AACA,MAAK,CAACH,WAAD,IAAgBD,OAAO,GAAG,EAA3B,IAAkCA,OAAO,KAAK,CAAlD,EAAqD;AACnD,WAAO,OAAP;AACD;;AAED,QAAMK,KAAK,GAAGC,IAAI,CAACC,KAAL,CAAWP,OAAO,GAAG,IAArB,CAAd;AACA,QAAMQ,OAAO,GAAGF,IAAI,CAACC,KAAL,CAAW,CAACP,OAAO,GAAGK,KAAK,GAAG,IAAnB,IAA2B,EAAtC,CAAhB;AACA,QAAMI,eAAe,GAAGR,WAAW,GAC/BD,OAAO,GAAGK,KAAK,GAAG,IAAlB,GAAyBG,OAAO,GAAG,EADJ,GAE/B,CAFJ;AAGA,QAAME,UAAU,GAAG;AACjBC,IAAAA,MAAM,EAAE,IADS;AAEjBC,IAAAA,QAAQ,EAAE,KAFO;AAGjBC,IAAAA,QAAQ,EAAE;AAHO,GAAnB;AAKA,QAAMC,MAAM,GAAGZ,QAAQ,GACnB;AACE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eAAT,CAAyBC,gBAAgB,GAAG,eAA5C,EAA6D;AAAA;;AAClE,MAAIC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,MAA7B,EAAqC,OAAOF,OAAO,CAACC,GAAR,CAAYE,EAAnB;AACrC,SAAO,UAAAC,IAAI,UAAJ,sCAAMC,cAAN,GAAuBC,eAAvB,GAAyCC,QAAzC,KAAqDR,gBAA5D;AACD;AAED;AACA;AACA;AACA;;;AACO,SAASS,cAAT,CAAwBC,QAAQ,GAAGX,eAAe,EAAlD,EAAsD;AAC3D,SAAO,qBAAO,+BAAeD,IAAI,CAACa,GAAL,EAAf,EAA2BD,QAA3B,CAAP,EAA6CrD,mBAA7C,CAAP;AACD;AAED;AACA;AACA;AACA;;;AACO,SAASuD,cAAT,CAAwBF,QAAQ,GAAGX,eAAe,EAAlD,EAAsD;AAC3D,SAAO,qBACL,+BAAeD,IAAI,CAACa,GAAL,EAAf,EAA2BD,QAA3B,CADK,EAELtD,4BAFK,CAAP;AAID","sourcesContent":["import {\n startOfDay,\n add,\n format,\n formatDuration as dateFnsFormatDuration\n} from \"date-fns\";\nimport { utcToZonedTime } from \"date-fns-tz\";\n\n// special constants for making sure the following date format is always sent to\n// OTP regardless of whatever the user has configured as the display format\nexport const OTP_API_DATE_FORMAT = \"YYYY-MM-DD\";\n// Date-Fns uses a different string format than moment.js\n// see https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md\nexport const OTP_API_DATE_FORMAT_DATE_FNS = \"yyyy-MM-dd\";\nexport const OTP_API_TIME_FORMAT = \"HH:mm\";\n\n/**\n * To ease the transition away from moment.js, this method uses date-fns to format durations\n * the way moment.js did.\n * @param {number} seconds The number of seconds to format\n * @param {boolean} showSeconds Whether to render seconds or not\n * @param {boolean} localize If true, will create output like moment.js using date-fns locale.\n * Otherwise, uses date-fns default\n * @returns Formatted duration\n */\nfunction formatDurationLikeMoment(\n seconds,\n showSeconds,\n localize = { enabled: true, code: \"en-US\" }\n) {\n // date-fns doesn't do this automatically\n if ((!showSeconds && seconds < 60) || seconds === 0) {\n return \"0 min\";\n }\n\n const hours = Math.floor(seconds / 3600);\n const minutes = Math.floor((seconds - hours * 3600) / 60);\n const secondsLeftOver = showSeconds\n ? seconds - hours * 3600 - minutes * 60\n : 0;\n const specLookup = {\n xHours: \"hr\",\n xMinutes: \"min\",\n xSeconds: \"sec\"\n };\n const locale = localize\n ? {\n // Maintain backwards compatibility when called with localize=true\n code: localize?.code || \"en-US\",\n formatDistance: (spec, val) => {\n return `${val} ${specLookup[spec]}`;\n }\n }\n : undefined;\n\n return dateFnsFormatDuration(\n {\n hours,\n minutes,\n seconds: secondsLeftOver\n },\n {\n format: [\"hours\", \"minutes\", \"seconds\"],\n locale\n }\n );\n}\n\n/**\n * Breaks up a duration in seconds into hours, minutes, and seconds.\n * @param {number} seconds The number of seconds to break up\n * @returns an object with fields with the corresponding, hours, minutes, seconds.\n */\nexport function toHoursMinutesSeconds(seconds) {\n const hours = Math.floor(seconds / 3600);\n const minutes = Math.floor((seconds - hours * 3600) / 60);\n return {\n hours,\n minutes,\n seconds: seconds - hours * 3600 - minutes * 60\n };\n}\n\n/**\n * @param {[type]} config the OTP config object found in store\n * @return {string} the config-defined time formatter or HH:mm (24-hr time)\n */\nexport function getTimeFormat(config) {\n return config?.dateTime?.timeFormat || OTP_API_TIME_FORMAT;\n}\n\nexport function getDateFormat(config) {\n return config?.dateTime?.dateFormat || OTP_API_DATE_FORMAT;\n}\n\nexport function getLongDateFormat(config) {\n return config?.dateTime?.longDateFormat || \"D MMMM YYYY\";\n}\n\n/**\n * Formats an elapsed time duration for display in narrative.\n * TODO: internationalization\n * @param {number} seconds duration in seconds\n * @returns {string} formatted text representation\n */\nexport function formatDuration(seconds) {\n return formatDurationLikeMoment(seconds, false);\n}\n\n/**\n * Formats an elapsed time in seconds, minutes, hours duration for display in narrative\n * TODO: internationalization\n * @param {number} seconds duration in seconds\n * @returns {string} formatted text representation\n */\nexport function formatDurationWithSeconds(seconds, region) {\n return formatDurationLikeMoment(seconds, { enabled: true, code: region });\n}\n\n/**\n * Offsets a time according to the provided time options\n * and returns the result.\n */\nexport function offsetTime(ms, options) {\n return ms + (options?.offset || 0);\n}\n\n/**\n * Formats a time value for display in narrative\n * TODO: internationalization/timezone\n * @param {number} ms epoch time value in milliseconds\n * @returns {string} formatted text representation\n */\nexport function formatTime(ms, options) {\n return format(\n offsetTime(ms, options),\n options?.format || OTP_API_TIME_FORMAT\n );\n}\n\n/**\n * Formats a seconds after midnight value for display in narrative\n * @param {number} seconds time since midnight in seconds\n * @param {string} timeFormat A valid date-fns time format\n * @return {string} formatted text representation\n */\nexport function formatSecondsAfterMidnight(seconds, timeFormat) {\n const time = add(startOfDay(new Date()), { seconds });\n return format(time, timeFormat);\n}\n\n/**\n * Uses Intl.DateTimeFormat() api to get the user's time zone. In a test\n * environment, pulls timezone information from an env variable. Default to\n * GMT+0 if the Intl API is unavailable.\n */\nexport function getUserTimezone(fallbackTimezone = \"Etc/Greenwich\") {\n if (process.env.NODE_ENV === \"test\") return process.env.TZ;\n return Intl?.DateTimeFormat().resolvedOptions().timeZone || fallbackTimezone;\n}\n\n/**\n * Formats current time for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentTime(timezone = getUserTimezone()) {\n return format(utcToZonedTime(Date.now(), timezone), OTP_API_TIME_FORMAT);\n}\n\n/**\n * Formats current date for use in OTP query\n * The conversion to the user's timezone is needed for testing purposes.\n */\nexport function getCurrentDate(timezone = getUserTimezone()) {\n return format(\n utcToZonedTime(Date.now(), timezone),\n OTP_API_DATE_FORMAT_DATE_FNS\n );\n}\n"],"file":"time.js"}
|
package/package.json
CHANGED
package/src/time.js
CHANGED
|
@@ -65,6 +65,22 @@ function formatDurationLikeMoment(
|
|
|
65
65
|
}
|
|
66
66
|
);
|
|
67
67
|
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Breaks up a duration in seconds into hours, minutes, and seconds.
|
|
71
|
+
* @param {number} seconds The number of seconds to break up
|
|
72
|
+
* @returns an object with fields with the corresponding, hours, minutes, seconds.
|
|
73
|
+
*/
|
|
74
|
+
export function toHoursMinutesSeconds(seconds) {
|
|
75
|
+
const hours = Math.floor(seconds / 3600);
|
|
76
|
+
const minutes = Math.floor((seconds - hours * 3600) / 60);
|
|
77
|
+
return {
|
|
78
|
+
hours,
|
|
79
|
+
minutes,
|
|
80
|
+
seconds: seconds - hours * 3600 - minutes * 60
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
68
84
|
/**
|
|
69
85
|
* @param {[type]} config the OTP config object found in store
|
|
70
86
|
* @return {string} the config-defined time formatter or HH:mm (24-hr time)
|
|
@@ -100,6 +116,15 @@ export function formatDuration(seconds) {
|
|
|
100
116
|
export function formatDurationWithSeconds(seconds, region) {
|
|
101
117
|
return formatDurationLikeMoment(seconds, { enabled: true, code: region });
|
|
102
118
|
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Offsets a time according to the provided time options
|
|
122
|
+
* and returns the result.
|
|
123
|
+
*/
|
|
124
|
+
export function offsetTime(ms, options) {
|
|
125
|
+
return ms + (options?.offset || 0);
|
|
126
|
+
}
|
|
127
|
+
|
|
103
128
|
/**
|
|
104
129
|
* Formats a time value for display in narrative
|
|
105
130
|
* TODO: internationalization/timezone
|
|
@@ -108,7 +133,7 @@ export function formatDurationWithSeconds(seconds, region) {
|
|
|
108
133
|
*/
|
|
109
134
|
export function formatTime(ms, options) {
|
|
110
135
|
return format(
|
|
111
|
-
ms
|
|
136
|
+
offsetTime(ms, options),
|
|
112
137
|
options?.format || OTP_API_TIME_FORMAT
|
|
113
138
|
);
|
|
114
139
|
}
|