{"version":3,"file":"parser.cjs","sources":["../../../src/datetime/parser.ts"],"sourcesContent":["/* eslint-disable id-blacklist, no-restricted-imports */\nimport { lowerCase } from 'lodash';\nimport moment from 'moment-timezone';\n\nimport { DateTimeOptions, getTimeZone } from './common';\nimport { parse, isValid } from './datemath';\nimport { systemDateFormats } from './formats';\nimport { DateTimeInput, DateTime, isDateTime, dateTime, toUtc, dateTimeForTimeZone } from './moment_wrapper';\n\n/**\n * The type that describes options that can be passed when parsing a date and time value.\n * @public\n */\nexport interface DateTimeOptionsWhenParsing extends DateTimeOptions {\n  /**\n   * If the input is a Grafana quick date, e.g. now-6h, then you can specify this to control\n   * whether the last part of the date and time value is included or excluded.\n   *\n   * Example: now-6h and the current time is 12:20:00 if roundUp is set to true\n   * the returned DateTime value will be 06:00:00.\n   */\n  roundUp?: boolean;\n  fiscalYearStartMonth?: number;\n}\n\ntype DateTimeParser<T extends DateTimeOptions = DateTimeOptions> = (value: DateTimeInput, options?: T) => DateTime;\n\n/**\n * Helper function to parse a number, text or Date to a DateTime value. If a timeZone is supplied the incoming value\n * is parsed with that timeZone as a base. The only exception to this is if the passed value is in a UTC-based\n * format. Then it will use UTC as the base. If no format is specified the current system format will be assumed.\n *\n * It can also parse the Grafana quick date and time format, e.g. now-6h will be parsed as Date.now() - 6 hours and\n * returned as a valid DateTime value.\n *\n * If no options are supplied, then default values are used. For more details please see {@link DateTimeOptions}.\n *\n * @param value - should be a parsable date and time value\n * @param options\n *\n * @public\n */\nexport const dateTimeParse: DateTimeParser<DateTimeOptionsWhenParsing> = (value, options?): DateTime => {\n  if (isDateTime(value)) {\n    return value;\n  }\n\n  if (typeof value === 'string') {\n    return parseString(value, options);\n  }\n\n  return parseOthers(value, options);\n};\n\nconst parseString = (value: string, options?: DateTimeOptionsWhenParsing): DateTime => {\n  if (value.indexOf('now') !== -1) {\n    if (!isValid(value)) {\n      return dateTime();\n    }\n\n    const parsed = parse(value, options?.roundUp, options?.timeZone, options?.fiscalYearStartMonth);\n    return parsed || dateTime();\n  }\n\n  let timeZone = getTimeZone(options);\n  let format = options?.format ?? systemDateFormats.fullDate;\n  if (value.endsWith('Z')) {\n    // This is a special case when we have an ISO date string\n    // In this case we want to force the format to be ISO and the timeZone to be UTC\n    // This logic is needed for initial load when parsing the URL params\n    format = 'YYYY-MM-DDTHH:mm:ss.SSSZ';\n    timeZone = 'utc';\n  }\n\n  const zone = moment.tz.zone(timeZone);\n\n  if (zone && zone.name) {\n    return dateTimeForTimeZone(zone.name, value, format);\n  }\n\n  switch (lowerCase(timeZone)) {\n    case 'utc':\n      return toUtc(value, format);\n    default:\n      return dateTime(value, format);\n  }\n};\n\nconst parseOthers = (value: DateTimeInput, options?: DateTimeOptionsWhenParsing): DateTime => {\n  const date = value;\n  const timeZone = getTimeZone(options);\n  const zone = moment.tz.zone(timeZone);\n\n  if (zone && zone.name) {\n    return dateTimeForTimeZone(zone.name, date);\n  }\n\n  switch (lowerCase(timeZone)) {\n    case 'utc':\n      return toUtc(date);\n    default:\n      return dateTime(date);\n  }\n};\n"],"names":["isDateTime","isValid","dateTime","parse","getTimeZone","systemDateFormats","moment","dateTimeForTimeZone","lowerCase","toUtc"],"mappings":";;;;;;;;;;;;;;;;AA0CO,MAAM,aAAA,GAA4D,CAAC,KAAA,EAAO,OAAA,KAAuB;AACtG,EAAA,IAAIA,yBAAA,CAAW,KAAK,CAAA,EAAG;AACrB,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,IAAI,OAAO,UAAU,QAAA,EAAU;AAC7B,IAAA,OAAO,WAAA,CAAY,OAAO,OAAO,CAAA;AAAA,EACnC;AAEA,EAAA,OAAO,WAAA,CAAY,OAAO,OAAO,CAAA;AACnC;AAEA,MAAM,WAAA,GAAc,CAAC,KAAA,EAAe,OAAA,KAAmD;AAtDvF,EAAA,IAAA,EAAA;AAuDE,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,KAAM,CAAA,CAAA,EAAI;AAC/B,IAAA,IAAI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