Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return timestamps as datetime objects #14

Open
DevilXD opened this issue May 27, 2021 · 7 comments
Open

Return timestamps as datetime objects #14

DevilXD opened this issue May 27, 2021 · 7 comments

Comments

@DevilXD
Copy link

DevilXD commented May 27, 2021

Hi 👋

Describe the solution you'd like

I think Submission.created_utc should contain a datetime object, instead of just the unix timestamp.

Describe alternatives you've considered

Currently, one has to wrap those themselves:

from datetime import datetime, timezone

post: Submission
posted_at = datetime.fromtimestamp(post.created_utc, timezone.utc)

Now, I have two variables, post and posted_at, that have information tied together, instead of just having one post variable, and accessing everything I'd need from it. Doing this:

# overwrite the attribute
post.created_utc = datetime.fromtimestamp(post.created_utc, timezone.utc)

... is risky, because the library may not expect this data type to be there.

Additional context

This would have to be applied to every object with a created_utc attribute, including submissions, comments, redditors, live threads, messages, etc. The same should probably apply to the edited attribute, which appears to be either false or specify the edition timestamp: https://i.imgur.com/1Mwbi9X.png In this case, the attribute should probably be either None when not edited, and datetime specifying the edition time when edited. The current documentation of "Whether or not the submission has been edited." is slightly misleading on this fact right now.

@bboe
Copy link
Member

bboe commented May 27, 2021

Hi, I like this idea, however, it's a pretty significant breaking change so I don't think we'd be willing to do that for PRAW / asyncpraw. However, https://github.com/praw-dev/prawdditions might be a great place to add that conversion.

@DevilXD
Copy link
Author

DevilXD commented May 27, 2021

Should I move this issue there then?

@bboe bboe transferred this issue from praw-dev/asyncpraw May 27, 2021
@bboe
Copy link
Member

bboe commented May 27, 2021

I just moved it :)

@PythonCoderAS
Copy link
Contributor

I feel that the best idea would be to just add a property.

@property
def created_at(self):
    return ...

@created_at.setter
def created_at(self, val):
    self.created_utc = # Conversion code

@property
def edited_at(self):
    # The same thing

@DevilXD
Copy link
Author

DevilXD commented May 28, 2021

I mean, it should be as simple as:

from datetime import datetime, timezone

@property
def created_at(self):
    return datetime.fromtimestamp(self.created_utc, timezone.utc)

@property
def edited_at(self):
    if self.edited:
        return datetime.fromtimestamp(self.edited, timezone.utc)
    # self.edited was False here
    return self.edited  # I guess?

I don't think setters are needed here, since the user isn't supposed to set these attributes anyway.

@bboe
Copy link
Member

bboe commented Jun 7, 2021

@DevilXD that's a fine approach. Would you like to make a PR?

@DevilXD
Copy link
Author

DevilXD commented Jun 8, 2021

I'm really not familiar with how these "additions" work, nor how they hook up into the praw/asyncpraw. I don't even know where there are supposed to go. I'm okay with you just taking this code and putting it where it needs to be - unless you'll tell me when it's supposed to go, then I can do it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants