Skip to content

Commit

Permalink
UTC 타임 변환 함수 작성 및 채팅 페이지에 적용 (#295)
Browse files Browse the repository at this point in the history
* feat: UTC 기준 시간을 한국 시간으로 변환하는 함수 작성

* feat: 채팅 목록 페이지 한국 시간대 적용

* chore: createdAt 타입 변경

* fix: 변경된 type 적용

* feat: 채팅방 페이지 한국 시간대 적용
  • Loading branch information
1g2g authored Nov 23, 2023
1 parent de28765 commit 274ec47
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 35 deletions.
7 changes: 5 additions & 2 deletions src/pages/ChatRoomListPage/ChatRoomItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { theme } from '@styles/theme';

import { ChatRoom } from '@type/models/ChatRoom.ts';

import { getTimeStamp } from '@utils/getTimeStamp.ts';
import { convertUTCToKoreanTime } from '@utils/convertUTCToKoreanTime.ts';
import { createdAtToString } from '@utils/createdAtToString.ts';

import {
ChatItemAvatar,
Expand Down Expand Up @@ -48,6 +49,8 @@ export const ChatRoomItem = ({
roomIconImageUrl,
} = chatRoomItem;

const lastTime = convertUTCToKoreanTime(lastMessageCreatedAt);

return (
<Flex justify="space-between" onClick={onClickChatRoomItem}>
<Flex gap={8}>
Expand All @@ -73,7 +76,7 @@ export const ChatRoomItem = ({
</MessageContainer>
</Flex>
<DateText size={8} color={theme.PALETTE.GRAY_500} nowrap>
{getTimeStamp(lastMessageCreatedAt)}
{createdAtToString(new Date(lastTime))}
</DateText>
</Flex>
);
Expand Down
9 changes: 7 additions & 2 deletions src/pages/ChattingPage/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { theme } from '@styles/theme';

import { ChatMessage } from '@type/models/ChatMessage';

import { convertUTCToKoreanTime } from '@utils/convertUTCToKoreanTime';

import {
BalloonContainer,
BalloonInfo,
Expand All @@ -26,7 +28,10 @@ export const Chat = ({
const { type: chatType, createdAt, sender, content } = chatInfo;

return chatType === '대화' ? (
<Flex key={createdAt} direction={isOthersMessage ? 'row' : 'row-reverse'}>
<Flex
key={String(createdAt)}
direction={isOthersMessage ? 'row' : 'row-reverse'}
>
{isOthersMessage && (
<Avatar
src={sender.profileImageUrl}
Expand All @@ -52,7 +57,7 @@ export const Chat = ({
{}
</Text>
<Text size={10} weight={300}>
{createdAt.slice(11, 16)}
{String(convertUTCToKoreanTime(createdAt)).slice(11, 16)}
</Text>
</BalloonInfo>
</Flex>
Expand Down
2 changes: 1 addition & 1 deletion src/type/models/ChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export type ChatMessage = {
content: string;
sender: ChatMember;
roomId: number;
createdAt: string;
createdAt: Date;
};
4 changes: 2 additions & 2 deletions src/type/models/ChatRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export type ChatRoom = {
playStartTime: string | null;
playTimeMinutes: number | null;
lastMessageContent: string;
lastMessageCreatedAt: string;
createdAt: string;
lastMessageCreatedAt: Date;
createdAt: Date;
};

export type ChatRoomDetail = Omit<
Expand Down
16 changes: 16 additions & 0 deletions src/utils/convertUTCToKoreanTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const convertUTCToKoreanTime = (utcTime: string | Date) => {
const utcDate = new Date(utcTime);
const offset = utcDate.getTimezoneOffset();
const koreanDate = new Date(utcDate.getTime() - offset * 60 * 1000);

const formattedTime = `${koreanDate.getFullYear()}-${String(
koreanDate.getMonth() + 1
).padStart(2, '0')}-${String(koreanDate.getDate()).padStart(2, '0')}T${String(
koreanDate.getHours()
).padStart(2, '0')}:${String(koreanDate.getMinutes()).padStart(
2,
'0'
)}:${String(koreanDate.getSeconds()).padStart(2, '0')}`;

return formattedTime;
};
28 changes: 0 additions & 28 deletions src/utils/getTimeStamp.ts

This file was deleted.

0 comments on commit 274ec47

Please sign in to comment.