handling timezone difference in dynamodb

Viewed 25

I can't figure out how to handle the timezone efficiently with dynamodb get operation.

Say my partition primary key and range key (sk) are below, where each record is aggregated data for the whole day done by the user. 2022-09-13 00:00:00 to 2022-09-13 23:23:59

{
 pk: 'userId',
 sk: '2022-09-13'
}

What is the best approach to store the dates as UTC and fetch based on the client's timezone?

In the current behavior of my program, I'm saving the range key (sk) as UTC date. But if the client's local timezone is +8:00, and when he performs a save operation at 2022-09-14 00:01:00, it will still be stored in the 2022-09-13.

1 Answers

Just store a timestamp (date + time) in UTC instead of the date.

If the client needs data based on the local timezone, you calculate the timestamp of the day start and end in that timezone, shift it to UTC and then do a between query using the utc-start and utc-end.

Related