From f52a8a1549a24f9dff05f77ef87c46edee83f7ba Mon Sep 17 00:00:00 2001 From: teehee567 Date: Wed, 3 Apr 2024 15:59:46 +1100 Subject: [PATCH 1/3] feat: convert server time to utc --- src/utils/hoyolab.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/utils/hoyolab.ts b/src/utils/hoyolab.ts index 9b7d5a9..23e9af3 100644 --- a/src/utils/hoyolab.ts +++ b/src/utils/hoyolab.ts @@ -7,6 +7,54 @@ import { } from '../types/wish'; import { getLatestWishFromGenshinAccount, linkGenshinAccountToUser } from '../db/utils'; + +// last updated 3/04/2024 +/** + * converts a time returned by server into utc time: + * + * @param uid genshin uid + * @param date the time that should be converted into utc + * @returns utc taime + */ +const serverTimeToUTC = ( + uid: string, + date: string, +): Date => { + const prefix = uid.length === 9 ? uid.substring(0, 1) : uid.substring(0, 2); + // convert to utc date, or js will take in as local + let utcDate = new Date(date + 'Z'); + switch (prefix) { + // mainland china + // utc + 8 + case "1": + case "2": + case "3": + case "5": + // TW, HK, MO china aswell + case "9": + // asia also follows china + case "8": + case "18": + utcDate.setHours(utcDate.getHours() - 8); + break; + + // america utc -5 + case "6": + utcDate.setHours(utcDate.getHours() + 5); + break; + + // europe utc + 1 + case "7": + utcDate.setHours(utcDate.getHours() - 1); + break; + + default: + console.error(`Unhandled UID prefix {${prefix}} for {${date}}`); + break; + } + return utcDate; +} + /** * Fetches wish history from the Genshin Impact API. * From fb13fc65423a063b922c989527cf8a9bddd69ba9 Mon Sep 17 00:00:00 2001 From: teehee567 Date: Wed, 3 Apr 2024 16:02:42 +1100 Subject: [PATCH 2/3] style: prettier got angry --- src/utils/hoyolab.ts | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/utils/hoyolab.ts b/src/utils/hoyolab.ts index 23e9af3..b979e88 100644 --- a/src/utils/hoyolab.ts +++ b/src/utils/hoyolab.ts @@ -7,44 +7,40 @@ import { } from '../types/wish'; import { getLatestWishFromGenshinAccount, linkGenshinAccountToUser } from '../db/utils'; - // last updated 3/04/2024 /** * converts a time returned by server into utc time: - * + * * @param uid genshin uid * @param date the time that should be converted into utc * @returns utc taime */ -const serverTimeToUTC = ( - uid: string, - date: string, -): Date => { +const serverTimeToUTC = (uid: string, date: string): Date => { const prefix = uid.length === 9 ? uid.substring(0, 1) : uid.substring(0, 2); // convert to utc date, or js will take in as local let utcDate = new Date(date + 'Z'); switch (prefix) { // mainland china // utc + 8 - case "1": - case "2": - case "3": - case "5": + case '1': + case '2': + case '3': + case '5': // TW, HK, MO china aswell - case "9": + case '9': // asia also follows china - case "8": - case "18": + case '8': + case '18': utcDate.setHours(utcDate.getHours() - 8); break; // america utc -5 - case "6": + case '6': utcDate.setHours(utcDate.getHours() + 5); break; // europe utc + 1 - case "7": + case '7': utcDate.setHours(utcDate.getHours() - 1); break; @@ -53,7 +49,7 @@ const serverTimeToUTC = ( break; } return utcDate; -} +}; /** * Fetches wish history from the Genshin Impact API. From 361280a8c0b8400196c8fb23fbb6d5ecb78a7299 Mon Sep 17 00:00:00 2001 From: teehee567 Date: Wed, 3 Apr 2024 16:04:13 +1100 Subject: [PATCH 3/3] fix: not exporting serverTimeToUTC func --- src/utils/hoyolab.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/hoyolab.ts b/src/utils/hoyolab.ts index b979e88..a804dfe 100644 --- a/src/utils/hoyolab.ts +++ b/src/utils/hoyolab.ts @@ -18,7 +18,7 @@ import { getLatestWishFromGenshinAccount, linkGenshinAccountToUser } from '../db const serverTimeToUTC = (uid: string, date: string): Date => { const prefix = uid.length === 9 ? uid.substring(0, 1) : uid.substring(0, 2); // convert to utc date, or js will take in as local - let utcDate = new Date(date + 'Z'); + const utcDate = new Date(date + 'Z'); switch (prefix) { // mainland china // utc + 8 @@ -157,4 +157,4 @@ const randomDelay = async (min: number, max: number): Promise => { await new Promise((resolve) => setTimeout(resolve, duration)); }; -export { getWishes, getGachaConfigList }; +export { getWishes, getGachaConfigList, serverTimeToUTC };