Django DST Time change problem with django datetimefield

I ran into a problem in my django application while working with DateTimeField. Its mainly due to changes in DST time.

I created the event on October 30, 2015 (DST last week). I created a copy of the first event, which will be held from November 06 to 15 (without DST day).

I applied timedeltafrom 7 days, so it canceled days to 7+. But I didn’t see Timedelta for hours.

Due to the preservation of daylight, it is reduced by one hour . I do not need it. I just want to have the same watch. How can i do this?

I tried with this, but did not help me.

DST timezone issue in django app

See this screenshot enter image description here

my models.py

from django.db import models
from datetime import timedelta

class Event(models.Model):

    name = models.CharField(max_length=100)

    created = models.DateTimeField(auto_now_add=True)

    start_date = models.DateTimeField()

    def __unicode__(self):
        return self.name


    def copy_event(self, add_days=timedelta(days=7)):

        start_date = self.start_date + add_days
        name = self.name +' (copy)'
        new_event = Event(name = name,created = self.created,start_date=start_date,)

        new_event.save()

        return new_event
+4
3

USE_TZ=True, , . , - . DST, , , (.. ).

:

  • , ( ), .

    from django.utils.timezone import localtime
    new_start_date = localtime(self.start_date) + add_days
    

    add_days ( , DST). new_start_date UTC , .

  • , , - , . , , . - USE_TZ=False, , , , , . - . , DateField TimeField DateTimeField.

0

,

from django.utils.timezone import localtime

l = Event.objects.get(...)
time = localtime(l.time + timedelta(days=7))
time += localtime(l.time).dst() - time.dst()
0

, Django, , , . , :

start_date = self.start_date + add_days

Try:

start_date = self.start_date.replace(tzinfo=None) + add_days

This converts the self.start_date parameter to a naive datetime and allows you to add days without regard to the time zone. I tested this separately on my own, and it works, both forward and backward.

Link: Converting time and time based on time zones to local time in Python

0
source

All Articles