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

Bug: in subscriptions.create, trial_days using timezone naive utcnow() #573

Open
miaoz2001 opened this issue Jul 4, 2018 · 3 comments · Fixed by #576
Open

Bug: in subscriptions.create, trial_days using timezone naive utcnow() #573

miaoz2001 opened this issue Jul 4, 2018 · 3 comments · Fixed by #576

Comments

@miaoz2001
Copy link

miaoz2001 commented Jul 4, 2018

Issue Summary

in subscriptions.create, when calculating the trial_end value, using

    if trial_days:
        subscription_params["trial_end"] = datetime.datetime.utcnow() + datetime.timedelta(days=trial_days)

However utcnow is timezone naive so the trial_end has no timezone info.
So when stripe calculate the timestamp with the code below:

def _encode_datetime(dttime):
    if dttime.tzinfo and dttime.tzinfo.utcoffset(dttime) is not None:
        utc_timestamp = calendar.timegm(dttime.utctimetuple())
    else:
        utc_timestamp = time.mktime(dttime.timetuple())

    return int(utc_timestamp)

It will get the wrong timestamp, which is, for example, if my local timezone is UTC+10, then the timestamp of trial_end will be 10 hours earlier.

The code should be

    if trial_days:
        subscription_params["trial_end"] = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=trial_days)

If this is a bug instead of a question or feature request, please fill out the sections below.


Steps to Reproduce

To re-produce, just pass the trial_days=10 in subscriptions.create()

What were you expecting to happen?

in the pinax_stripe_subscription table it shows as below
image
However trial_end should be 2018-07-14 01:20:49

What actually happened?

trial_end should be 2018-07-14 01:20:49

@blueyed
Copy link
Contributor

blueyed commented Jul 4, 2018

Nice catch!

Please consider creating a PR (with a test).

@miaoz2001
Copy link
Author

Thanks!
I will try, maybe take some time though...

@miaoz2001
Copy link
Author

pull request created

miaoz2001 added a commit to sptgps/pinax-stripe that referenced this issue Jul 16, 2018
…ow()

- Changed to timezone.now()
- Add test
Fixed pinax#573
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants