pinax-stripe-light icon indicating copy to clipboard operation
pinax-stripe-light copied to clipboard

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

Open miaoz2001 opened this issue 6 years ago • 3 comments

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

miaoz2001 avatar Jul 04 '18 02:07 miaoz2001

Nice catch!

Please consider creating a PR (with a test).

blueyed avatar Jul 04 '18 20:07 blueyed

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

miaoz2001 avatar Jul 05 '18 23:07 miaoz2001

pull request created

miaoz2001 avatar Jul 13 '18 02:07 miaoz2001