-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: TimezoneInterceptor를 추가하여 Date 객체를 서울 시간대로 변경 #70
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
Injectable, | ||
NestInterceptor, | ||
ExecutionContext, | ||
CallHandler, | ||
} from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
@Injectable() | ||
export class TimezoneInterceptor implements NestInterceptor { | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
return next.handle().pipe( | ||
map((data) => { | ||
if (data && typeof data === 'object') { | ||
return this.convertDateToSeoulTime(data); | ||
} | ||
return data; | ||
}), | ||
); | ||
} | ||
|
||
private convertDateToSeoulTime(data: any): any { | ||
for (const key in data) { | ||
if (data[key] instanceof Date) { | ||
data[key] = data[key].toLocaleString('ko-KR', { | ||
timeZone: 'Asia/Seoul', | ||
}); | ||
} else if (typeof data[key] === 'object') { | ||
data[key] = this.convertDateToSeoulTime(data[key]); | ||
} | ||
} | ||
return data; | ||
} | ||
} |