diff --git a/README.md b/README.md index 07d44dc..e81912f 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,6 @@ Low-level time zone listing and date converting. Intended for adding time zone s ## Synopsis - ```js const { listTimeZones, findTimeZone, getZonedTime, getUnixTime @@ -197,7 +196,7 @@ See the function [parseZonedTime](#parseZonedTime) for more information. Date pickers usually supply the date, which the user selected and the time zone is implied from user settings. The time zone should be set to such date before it is returned from the editing control. ```js -const { setTimeZone } = require('timezone-support') +const { findTimeZone, setTimeZone } = require('timezone-support') const berlin = findTimeZone('Europe/Berlin') // Time object with the date parts without time zone diff --git a/package.json b/package.json index 77b06db..27736b2 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,8 @@ ] }, "files": [ - "dist" + "dist", + "src" ], "jest": { "roots": [ diff --git a/src/convert/convert.js b/src/convert/convert.js index af8c1c9..22013a6 100644 --- a/src/convert/convert.js +++ b/src/convert/convert.js @@ -1,3 +1,5 @@ +import { getUnixTimeFromUTC, getUTCTime, getLocalTime } from './utc-date' + function findTransitionIndex (unixTime, timeZone) { const { untils } = timeZone for (let i = 0, length = untils.length; i < length; ++i) { @@ -14,32 +16,6 @@ function getTransition (unixTime, timeZone) { return { abbreviation, offset } } -function getUnixTimeFromUTC ({ year, month, day, hours, minutes, seconds = 0, milliseconds = 0 }) { - return Date.UTC(year, month - 1, day, hours, minutes, seconds, milliseconds) -} - -function getUTCTime (date) { - const year = date.getUTCFullYear() - const month = date.getUTCMonth() + 1 - const day = date.getUTCDate() - const hours = date.getUTCHours() - const minutes = date.getUTCMinutes() - const seconds = date.getUTCSeconds() || 0 - const milliseconds = date.getUTCMilliseconds() || 0 - return { year, month, day, hours, minutes, seconds, milliseconds } -} - -function getLocalTime (date) { - const year = date.getFullYear() - const month = date.getMonth() + 1 - const day = date.getDate() - const hours = date.getHours() - const minutes = date.getMinutes() - const seconds = date.getSeconds() - const milliseconds = date.getMilliseconds() - return { year, month, day, hours, minutes, seconds, milliseconds } -} - function setTimeZone (time, timeZone, options) { if (time instanceof Date) { const { useUTC } = options || {} diff --git a/src/convert/utc-date.js b/src/convert/utc-date.js new file mode 100644 index 0000000..638ce80 --- /dev/null +++ b/src/convert/utc-date.js @@ -0,0 +1,28 @@ + +function getUnixTimeFromUTC ({ year, month, day, hours, minutes, seconds = 0, milliseconds = 0 }) { + return Date.UTC(year, month - 1, day, hours, minutes, seconds, milliseconds) +} + +function getUTCTime (date) { + const year = date.getUTCFullYear() + const month = date.getUTCMonth() + 1 + const day = date.getUTCDate() + const hours = date.getUTCHours() + const minutes = date.getUTCMinutes() + const seconds = date.getUTCSeconds() || 0 + const milliseconds = date.getUTCMilliseconds() || 0 + return { year, month, day, hours, minutes, seconds, milliseconds } +} + +function getLocalTime (date) { + const year = date.getFullYear() + const month = date.getMonth() + 1 + const day = date.getDate() + const hours = date.getHours() + const minutes = date.getMinutes() + const seconds = date.getSeconds() + const milliseconds = date.getMilliseconds() + return { year, month, day, hours, minutes, seconds, milliseconds } +} + +export { getUnixTimeFromUTC, getUTCTime, getLocalTime }