Skip to content

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

@miaoz2001

Description

@miaoz2001

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions