Friday, 7 January 2022

Django - Return months and years that posts by a user spans given time offset from UTC

I'm working on a social media app and I want user A to be able to get all the months with the year that user B posted in user A's time zone. The front-end is in Javascript the most sensible thing would be for a user to send their time zone's offset from UTC (Can be done with new Date().getTimezoneOffset() in JS, so if you're on EET (Eastern European Time) this would return -120 as EET is 120 minutes ahead of UTC) as this is what time zone dates are saved in Django. So for example user A would ping a url that looks something like /<user B's username>/<user A's time zone offset from UTC>.

Let's say user B is in PST and created a post at November 15 2021 12:00pm and December 31 2021 at 11:00pm. Then let's say user A who's in EST calls that url with /<user B username>/<new Date().getTimezoneOffset() in JS> then it should return [(November, 2021), (January, 2022)] (Or the equivalent numerical mapping), because December 31 at 11:00pm PST is January 1st at 2:00am EST.

How can I return all months with year user B posted in user A's time zone, give user A's time zone offset from UTC?

models.py

class Post(models.Model):
    uuid = models.UUIDField(primary_key=True)
    created = models.DateTimeField('Created at', auto_now_add=True)
    updated_at = models.DateTimeField('Last updated at', auto_now=True, blank=True, null=True)
    creator = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name="post_creator")
    body = models.CharField(max_length=POST_MAX_LEN, validators=[MinLengthValidator(POST_MIN_LEN)])


from Django - Return months and years that posts by a user spans given time offset from UTC

No comments:

Post a Comment