"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.time = void 0;
const moment_1 = __importDefault(require("moment"));
require("moment-timezone");
require("moment-jdateformatparser");
const parseTimestamp = (dateTime, format, timezone) => {
    if (!dateTime || !format) {
        return null;
    }
    try {
        const momentFormatString = (0, moment_1.default)().toMomentFormatString(format);
        return timezone ? moment_1.default.tz(dateTime, momentFormatString, timezone) : (0, moment_1.default)(dateTime, momentFormatString);
    }
    catch (e) {
        return null;
    }
};
const time = () => ({
    nowISO8601(t) {
        return (0, moment_1.default)().toISOString();
    },
    nowEpochSeconds() {
        return (0, moment_1.default)().unix();
    },
    nowEpochMilliSeconds() {
        return (0, moment_1.default)().valueOf();
    },
    nowFormatted(format, timezone = null) {
        try {
            if (timezone) {
                return (0, moment_1.default)()
                    .tz(timezone)
                    .formatWithJDF(format);
            }
            return (0, moment_1.default)().formatWithJDF(format);
        }
        catch (e) {
            return null;
        }
    },
    parseFormattedToEpochMilliSeconds(dateTime, format, timezone) {
        const timestamp = parseTimestamp(dateTime, format, timezone);
        return timestamp ? timestamp.valueOf() : null;
    },
    parseISO8601ToEpochMilliSeconds(dateTime) {
        const timestamp = parseTimestamp(dateTime, 'YYYY-MM-DDTHH:mm:ss.SZ');
        return timestamp ? timestamp.valueOf() : null;
    },
    epochMilliSecondsToSeconds(milliseconds) {
        try {
            return Math.floor(milliseconds / 1000);
        }
        catch (e) {
            return null;
        }
    },
    epochMilliSecondsToISO8601(dateTime) {
        try {
            return (0, moment_1.default)(dateTime).toISOString();
        }
        catch (e) {
            return null;
        }
    },
    epochMilliSecondsToFormatted(timestamp, format, timezone = 'UTC') {
        try {
            return (0, moment_1.default)(timestamp)
                .tz(timezone)
                .formatWithJDF(format);
        }
        catch (e) {
            return null;
        }
    },
});
exports.time = time;
//# sourceMappingURL=time.js.map