UNPKG

@technobuddha/library

Version:
33 lines (32 loc) 1.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTimezone = void 0; var isDate_1 = __importDefault(require("lodash/isDate")); var constants_1 = require("../constants"); var padNumber_1 = __importDefault(require("../padNumber")); /** * Determine the correct timezone string for a specified date using a local timezone, or an offset in minutes * * @remarks the GMT flag overrides the Z flag if both are set * @param input The date, or a timezone offset in minutes * @param __namedParameters see {@link Options} * @default GMT false * @default Z true * @returns the timezone offset formatted like '±hh:mm' the string is prefixed by 'GMT' if the option is set. If the Z option is set 'Z' is returned for the * GMT+00:00 timezone */ function getTimezone(input, _a) { var _b = _a === void 0 ? {} : _a, _c = _b.GMT, GMT = _c === void 0 ? false : _c, _d = _b.Z, Z = _d === void 0 ? true : _d; var offset = isDate_1.default(input) ? input.getTimezoneOffset() : input; if (offset === 0) return GMT ? 'GMT' : Z ? 'Z' : '+00:00'; var n = Math.abs(offset) / 60; var h = Math.floor(n); var m = (n - h) * 60; return (GMT ? 'GMT' : constants_1.empty) + (offset > 0 ? '-' : '+') + padNumber_1.default(h, 2) + ":" + padNumber_1.default(m, 2); } exports.getTimezone = getTimezone; exports.default = getTimezone;