{"version":3,"sources":["../../src/utils/datetime.ts"],"sourcesContent":["export const datetimeExp = {\r\n  // https://regex101.com/r/v1YLhA/2\r\n  datetime: /^(?<dt>(?<year>\\d{4})(?:\\-(?<month>(?:1[0-2]|0[1-9]))(?:\\-(?<date>[0-2][0-9]|3[0-1]))?)?(T(?<hour>[0-1][0-9]|2[0-3])(?:\\:(?<minute>[0-5][0-9])(?:\\:(?<sec>[0-5][0-9])(?:\\.(?<milisecond>(?:\\d{3})+))?)?)?)?(?<tz>(Z)|((?:\\+|-)(?:(?:[0-1][0-9]|2[0-3])(?:\\:[0-5][0-9])?)))?)$/,\r\n\r\n  // https://regex101.com/r/yXu5MC/2\r\n  date: /^(?<year>\\d{4})(?:\\-(?<month>(?:1[0-2]|0[1-9]))(?:\\-(?<date>[0-2][0-9]|3[0-1]))?)?$/,\r\n\r\n  // https://regex101.com/r/hbiNMv/4\r\n  time: /^(?<hour>[0-1][0-9]|2[0-3])(?:\\:(?<minute>[0-5][0-9])(?:\\:(?<second>[0-5][0-9])(?:\\.(?<milisecond>(?:\\d{3})+))?)?)?$/\r\n}\r\n\r\nexport const datetimePlainExp = {\r\n  // https://regex101.com/r/0j7nlS/3\r\n  // Note: Since this expression starts with ^ and does not end with $, it will\r\n  // ignore any characters after the valid datetime format.\r\n  // Unlike regular datetime expressions, this won't complain about the invalid\r\n  // datetime format if the expression finds invalid characters after the\r\n  // valid datetime format. For example, 20200101000000000Zabc will be\r\n  // considered as a valid datetime format with 'abc' ignored.\r\n  // This is required because, putting $ at the end of the expression will\r\n  // cause it to pick up invalid groups from the datetime string.\r\n  datetime: /^(?<year>\\d{4})(?:(?<month>(?:1[0-2]|0[1-9]))(?:(?<date>[0-2][0-9]|3[0-1]))?)?(?:(?<hour>[0-1][0-9]|2[0-3])(?:(?<minute>[0-5][0-9])(?:(?<second>[0-5][0-9])(?:(?<milisecond>(?:\\d{3})+))?)?)?)?(?<tz>(Z)|((?:\\+|-)(?:(?:[0-1][0-9]|2[0-3])(?:[0-5][0-9])?)))?/,\r\n\r\n  // https://regex101.com/r/VDkmzU/2\r\n  date: /^(?<year>\\d{4})(?:(?<month>(?:1[0-2]|0[1-9]))(?:(?<date>[0-2][0-9]|3[0-1]))?)?$/,\r\n\r\n  // https://regex101.com/r/X5AA4A/3\r\n  time: /^(?<hour>[0-1][0-9]|2[0-3])(?:(?<minute>[0-5][0-9])(?:(?<second>[0-5][0-9])(?:(?<milisecond>(?:\\d{3})+))?)?)?$/\r\n}\r\n\r\n/**\r\n * Parses the value string and returns the datetime if the string represents\r\n * the ISO 8601 formatted datetime. Returns null when the invalid datetime\r\n * is found.\r\n */\r\nexport const parseDateTime = (value: string): Date | null => {\r\n\r\n  // If the first 6 characters contain '-', it is regular datetime format.\r\n  // Otherwise, it is plain datetime format (no separator)\r\n  const exp = /[\\-\\:]/.test(value.substring(0, 6)) ? datetimeExp.datetime : datetimePlainExp.datetime;\r\n\r\n  const match = exp.exec(value);\r\n\r\n  if (!match) {\r\n    return null;\r\n  }\r\n\r\n  const {\r\n    year, month, date,\r\n    hour, minute, second, milisecond, tz\r\n  } = match.groups || {};\r\n\r\n  const utc = tz ? tz : 'Z';\r\n  const dateStr = `${year}-${month || '01'}-${date || '01'}T${hour || '00'}:${minute || '00'}:${second || '00'}.${milisecond || '000'}${utc}`;\r\n\r\n  return new Date(dateStr);\r\n}\r\n\r\n/**\r\n * Parses the value string and returns the datetime if the string represents\r\n * the ISO 8601 formatted date. Returns null when the invalid date\r\n * is found.\r\n */\r\nexport const parseDate = (value: string): Date | null => {\r\n  const exp = /\\-/.test(value.substring(0, 5)) ? datetimeExp.date : datetimePlainExp.date;\r\n  const match = exp.exec(value);\r\n\r\n  if (!match) {\r\n    return null;\r\n  }\r\n\r\n  const { year, month, date } = match.groups || {};\r\n  const dateStr = `${year}-${month || '01'}-${date || '01'}T00:00:00.000Z`;\r\n\r\n  return new Date(dateStr);\r\n}\r\n\r\n/**\r\n * Parses the value string and returns the datetime if the string represents\r\n * the ISO 8601 formatted time. Returns null when the invalid time\r\n * is found.\r\n */\r\nexport const parseTime = (value: string): Date | null => {\r\n  const exp = /\\:/.test(value.substring(0,3)) ? datetimeExp.time : datetimePlainExp.time;\r\n  const match = exp.exec(value);\r\n\r\n  if (!match) {\r\n    return null;\r\n  }\r\n\r\n  const { hour, minute, second, milisecond } = match.groups || {};\r\n\r\n  const dateStr = `1900-01-01T${hour || '00'}:${minute || '00'}:${second || '00'}.${milisecond ? milisecond : '000'}Z`;\r\n  return new Date(dateStr);\r\n}\r\n\r\nexport const dateToDatetimeString = (date: Date | null, noSep = false, zuluTime = false) => {\r\n  if (date === null) return null\r\n  return date.toISOString()\r\n}\r\n\r\nexport const dateToDateString = (date: Date | null, noSep = false) => {\r\n  if (date === null) return null\r\n\r\n  // Convert the date to iso string and return the date part\r\n  return date.toISOString().split('T')[0]\r\n}\r\n\r\nexport const dateToTimeString = (date: Date | null, noSep = false) => {\r\n  if (date === null) return null\r\n\r\n  // Convert the date to iso string and return the time part\r\n  // without the timezone\r\n  return date.toISOString().split('T')[1].split('.')[0]\r\n}\r\n\r\nconst _ = (n: number, pad: number = 2) => {\r\n  return n.toLocaleString('en-US', { minimumIntegerDigits: pad, useGrouping: false })\r\n}\r\n\r\nexport const dateToSmartString = (date: Date | null, type: \"datetime\" | \"date\" | \"time\", noSep = false) => {\r\n  if (date === null) return null\r\n\r\n  switch (type) {\r\n    case \"datetime\":\r\n      return dateToDatetimeString(date, noSep)\r\n    case \"date\":\r\n      return dateToDateString(date, noSep)\r\n    case \"time\":\r\n      return dateToTimeString(date, noSep)\r\n  }\r\n}\r\n\r\nexport const dateToIOString = (date: Date | null, type: \"datetime\" | \"date\" | \"time\", noSep = false) => {\r\n  if (date === null) return \"N\"\r\n\r\n  switch (type) {\r\n    case \"datetime\":\r\n      return `dt\"${dateToDatetimeString(date, noSep)}\"`\r\n    case \"date\":\r\n      return `d\"${dateToDateString(date, noSep)}\"`\r\n    case \"time\":\r\n      return `t\"${dateToTimeString(date, no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