{"version":3,"file":"DateTime.mjs","sources":["../../src/DateTime.ts"],"sourcesContent":["import {\n  NumberSerializer,\n  Serializer,\n  mapSerializer,\n} from '@metaplex-foundation/umi-serializers';\nimport { BigIntInput, createBigInt } from './BigInt';\n\n/**\n * Defines a string that can be parsed into a Date object.\n * For instance, `\"2020-01-01T00:00:00.000Z\"`.\n * @category Utils — DateTime\n */\nexport type DateTimeString = string;\n\n/**\n * Defines all the types that can be used to create a DateTime.\n * @category Utils — DateTime\n */\nexport type DateTimeInput = DateTimeString | BigIntInput | Date;\n\n/**\n * Defines a point in time via a Unix timestamp represented as a BigInt.\n * @category Utils — DateTime\n */\nexport type DateTime = bigint;\n\n/**\n * Creates a {@link DateTime} from a {@link DateTimeInput}.\n * @category Utils — DateTime\n */\nexport const dateTime = (value: DateTimeInput): DateTime => {\n  if (typeof value === 'string' || isDateObject(value)) {\n    const date = new Date(value);\n    const timestamp = Math.floor(date.getTime() / 1000);\n    return createBigInt(timestamp);\n  }\n\n  return createBigInt(value);\n};\n\n/**\n * Helper function to get the current time as a {@link DateTime}.\n * @category Utils — DateTime\n */\nexport const now = (): DateTime => dateTime(new Date(Date.now()));\n\n/**\n * Whether the given value is a Date object.\n * @category Utils — DateTime\n */\nconst isDateObject = (value: any): value is Date =>\n  Object.prototype.toString.call(value) === '[object Date]';\n\n/**\n * Formats a {@link DateTime} as a string.\n * @category Utils — DateTime\n */\nexport const formatDateTime = (\n  value: DateTime,\n  locales: Intl.LocalesArgument = 'en-US',\n  options: Intl.DateTimeFormatOptions = {\n    month: 'short',\n    day: 'numeric',\n    year: 'numeric',\n    hour: 'numeric',\n    minute: 'numeric',\n    timeZone: 'UTC',\n  }\n): string => {\n  const date = new Date(Number(value * 1000n));\n\n  return date.toLocaleDateString(locales, options);\n};\n\n/**\n * Converts a number serializer into a DateTime serializer.\n * @category Utils — DateTime\n */\nexport const mapDateTimeSerializer = (\n  serializer: NumberSerializer\n): Serializer<DateTimeInput, DateTime> =>\n  mapSerializer(\n    serializer as Serializer<number | bigint>,\n    (value: DateTimeInput): number | bigint => {\n      const date = dateTime(value);\n      return date > Number.MAX_SAFE_INTEGER ? date : Number(date);\n    },\n    (value: number | bigint): DateTime => dateTime(value)\n  );\n"],"names":["dateTime","value","isDateObject","date","Date","timestamp","Math","floor","getTime","createBigInt","now","Object","prototype","toString","call","formatDateTime","locales","options","month","day","year","hour","minute","timeZone","Number","toLocaleDateString","mapDateTimeSerializer","serializer","mapSerializer","MAX_SAFE_INTEGER"],"mappings":";;;AAOA;AACA;AACA;AACA;AACA;;AAeA;AACA;AACA;AACA;AACaA,MAAAA,QAAQ,GAAIC,KAAoB,IAAe;EAC1D,IAAI,OAAOA,KAAK,KAAK,QAAQ,IAAIC,YAAY,CAACD,KAAK,CAAC,EAAE;AACpD,IAAA,MAAME,IAAI,GAAG,IAAIC,IAAI,CAACH,KAAK,CAAC,CAAA;AAC5B,IAAA,MAAMI,SAAS,GAAGC,IAAI,CAACC,KAAK,CAACJ,IAAI,CAACK,OAAO,EAAE,GAAG,IAAI,CAAC,CAAA;IACnD,OAAOC,YAAY,CAACJ,SAAS,CAAC,CAAA;AAChC,GAAA;EAEA,OAAOI,YAAY,CAACR,KAAK,CAAC,CAAA;AAC5B,EAAC;;AAED;AACA;AACA;AACA;AACaS,MAAAA,GAAG,GAAG,MAAgBV,QAAQ,CAAC,IAAII,IAAI,CAACA,IAAI,CAACM,GAAG,EAAE,CAAC,EAAC;;AAEjE;AACA;AACA;AACA;AACA,MAAMR,YAAY,GAAID,KAAU,IAC9BU,MAAM,CAACC,SAAS,CAACC,QAAQ,CAACC,IAAI,CAACb,KAAK,CAAC,KAAK,eAAe,CAAA;;AAE3D;AACA;AACA;AACA;AACO,MAAMc,cAAc,GAAG,CAC5Bd,KAAe,EACfe,OAA6B,GAAG,OAAO,EACvCC,OAAmC,GAAG;AACpCC,EAAAA,KAAK,EAAE,OAAO;AACdC,EAAAA,GAAG,EAAE,SAAS;AACdC,EAAAA,IAAI,EAAE,SA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