0

I need to find the UTC equivalent time for the PDT time.

So that I have implemented the code which is as follows:

        utc = pytz.utc
        local = pytz.timezone ("America/Los_Angeles")
        begin_localize = local.localize(begin)
        begin = begin_localize.astimezone(utc)

        end_localize = local.localize(end)
        end = end_localize.astimezone(utc)

It is generating the output as follows:

2016-04-02 07:00:00+00:00

Type is as follows:

<type 'datetime.datetime'>

Even though it is working fine, My backend needs a value which in the format as follows:

2016-04-02 00:00:00 

Also should be of type type 'datetime.datetime'.

Someone help me with the way of achieving the same.

Sparky
  • 91
  • 1
  • 8

1 Answers1

0

Don't confuse an object such as datetime.datetime and its printable representation such as '2016-04-02 00:00:00' string. You could get any format you like using .strftime() method.

My backend needs a value which in the format as follows:
2016-04-02 00:00:00
Also should be of type type 'datetime.datetime'.

The comment suggest that you want a naive datetime object. To remove the timezone info, you could use .replace() method:

naive_dt = begin_localize.replace(tzinfo=None)

You could get the naive object that represents utc time directly:

# <local time> == <utc time> + <utc offset> by definition
# <utc time> = <local time> - <utc offset>
naive_utc_dt = naive_local - local_timezone.utcoffset(naive_local)

Note: the time that naive_local represents may be an ambiguous or even non-existent in the local timezone (e.g., during DST transitions). Pass is_dst parameter to disambiguate if necessary.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670