UNPKG

@grafana/ui

Version:
1 lines 1.68 kB
{"version":3,"file":"adjustDateForReactCalendar.mjs","sources":["../../../../../src/components/DateTimePickers/utils/adjustDateForReactCalendar.ts"],"sourcesContent":["import { getZone } from '@grafana/data';\n\n/**\n * React calendar doesn't support showing dates in other time zones, so attempting to show\n * values near midnight in another time zone than your browsers may end up showing the wrong date\n *\n * This function adjusts a date by \"moving\" the time to appear as if it's local.\n * e.g. make 5 PM New York \"look like\" 5 PM in the user's local browser time.\n * See also https://github.com/wojtekmaj/react-calendar/issues/511#issuecomment-835333976\n */\nexport function adjustDateForReactCalendar(date: Date, timeZone: string): Date {\n const zone = getZone(timeZone);\n if (!zone) {\n return date;\n }\n\n // get utc offset for timezone preference\n const timezonePrefOffset = zone.utcOffset(date.getTime());\n\n // get utc offset for local timezone\n const localOffset = date.getTimezoneOffset();\n\n // calculate difference between timezone preference and local timezone\n const diff = timezonePrefOffset - localOffset;\n\n const newDate = new Date(date.getTime() - diff * 1000 * 60);\n return newDate;\n}\n"],"names":[],"mappings":";;;AAUO,SAAS,0BAAA,CAA2B,MAAY,QAAA,EAAwB;AAC7E,EAAA,MAAM,IAAA,GAAO,QAAQ,QAAQ,CAAA;AAC7B,EAAA,IAAI,CAAC,IAAA,EAAM;AACT,IAAA,OAAO,IAAA;AAAA,EACT;AAGA,EAAA,MAAM,kBAAA,GAAqB,IAAA,CAAK,SAAA,CAAU,IAAA,CAAK,SAAS,CAAA;AAGxD,EAAA,MAAM,WAAA,GAAc,KAAK,iBAAA,EAAkB;AAG3C,EAAA,MAAM,OAAO,kBAAA,GAAqB,WAAA;AAElC,EAAA,MAAM,OAAA,GAAU,IAAI,IAAA,CAAK,IAAA,CAAK,SAAQ,GAAI,IAAA,GAAO,MAAO,EAAE,CAAA;AAC1D,EAAA,OAAO,OAAA;AACT;;;;"}