86613

DST timezone issue in django app

I am making an events app in django.

In that I need to make a copy of one event object (row/entry in DB) into another. It creates another object with same details.

This event object involves timezone and Day light Saving.

I am facing problems during DST. I have applied time delta of 5 days during copying of objects.

<hr>

In normal days

Event object 1 date: Nov. 10, 2014 10:36 a.m.

Event object 2(copy of 1) date: Nov. 15, 2014 10:36 a.m.

This OK..!

<hr>

But in DST days I don't want this 1 hr change.

Event object 1 date: Oct. 30, 2015, 10:36 a.m.

Event object 2(copy of 1) date: Nov. 4, 2015, 9:36 a.m.

<hr>

Here 1 hour is automatically reducing in object 2 during DST change(Oct 30 is belongs to DST, Nov 4 is not). But I want same time. How can I fix this???

Answer1:

Timedeltas algebra work by adding/substracting time, not dates, that's why you get one hour less when passing through a DST change.

I think the easiest solution is to perform the algebra using localized times and not normalizing. This way you will be "doing it wrong" but you'll get what you want.

Example:

timestamp_1 = event.timestamp # we assume this datetime comes in UTC madrid = pytz.timezone('Europe/Madrid') madrid.localize(timestamp_1) timestamp_2 = timestamp_1 + datetime.timedelta(days=5) new_event = Event(timestamp=timestamp_2)

See http://pytz.sourceforge.net/#localized-times-and-date-arithmetic

Recommend

  • What is the proper way to RTSP stream, transcode and save to file?
  • Change projection in MSSQL for web mapping (Leaflet,Openlayer, OpenStreetMaps, GoogleAPI, …) to WSG4
  • Using python to determine if a timestamp is under daylight savings time
  • Towers of Hanoi with “counter” in python
  • Issues with wmode=“opaque” and issues with wmmode=“window”
  • How can I sum two different columns at once where one contains Decimal objects in pandas?
  • Capturing HTML Text Input Key press after key has been applied?
  • How to have a website splash screen (web app)
  • Gerrit will not push. Error: No common ancestry
  • How to protect the mp3 file from read or copy on Android?
  • Enable Bootstrap Intellisense using Angular 4 in WebStorm
  • Buffer a large file; BufferedInputStream limited to 2gb; Arrays limited to 2^31 bytes
  • How gzip file gets stored in HDFS
  • XOR with Neural Networks (Matlab)
  • XSLT foreach repeating nodes to flat
  • Do query loads all the data in memory
  • How to access culture data in globalize.js V1.0.0
  • Django model inheritance, filtering models
  • How can I set a binding to a Combox in a UserControl?
  • How to handle images sent by a mobile device?
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Debug.DrawLine not showing in the GameView
  • print() is showing quotation marks in results
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Recording logins for password protected directories
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • CSS Linear-gradient formatting issue accross different browsers
  • Pass value from viewmodel to script in zk
  • Problems to linebreak with an int in JLabel
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • Benchmarking RAM performance - UWP and C#
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Python/Django TangoWithDjango Models and Databases