#Timezone inconsistency between saving and loading

28 messages · Page 1 of 1 (latest)

astral dirge
#

Hello. I think I've lost my mind trying to figure this out so I'm asking for help.
I have a FormSet of ModelForms where the model has a DateTime field. When I save the Form the model doesn't shift the timezone the correct amount, but then loading it it does so saving multiple times in a row continuously shifts the time. For example
The form input value is "2024-10-12 12:34:00" (I have verified this is unchanged in the line before the formset's .save())
This saves into the database as "2024-10-12 02:29:00" which is 10 hours and 5 minutes different. Not a valid timezone difference.
This then loads back into the form as "2024-10-12 13:29:00" which is the correct 11 hours different from the database value, but is not the same value as was saved.

In Django settings.py I have USE_TZ=True and my localtime is Australia/Sydney which is +10 or +11 depending on daylight savings.
MariaDB has timezone set to UTC.
Clearly this would be a massive issue if it was a problem with Django itself so it's probably something I'm doing wrong but how do I figure out what?

Thanks.

fleet nest
#

have u tried add TIME_ZONE = 'Australia/Sydney' in django settings.py ?

astral dirge
fleet nest
#

could u put this debug statements into ur code:

from django.utils.timezone import get_current_timezone
print(get_current_timezone())
#

provide the output here.

astral dirge
#

Australia/Sydney

fleet nest
#

I need more information from u regrading the models.py code and forms.py code so we can explore it together.

fleet nest
#

CountDateTime = models.DateTimeField('Replicate Count Date/Time (YYYY-mm-dd HH:MM) (24H)',null=True,blank=True)
instead of this, u could try auto_now_add=True, django will save it as UTC

astral dirge
#

Then I do

CountFormset = modelformset_factory(DataSheetCount, form=DataSheetCountForm, exclude=['Sheet'], can_delete=True, can_order=True, extra=0)
countFormset = CountFormset(request.POST, prefix='countFormset', queryset=DataSheetCount.objects.filter(Sheet=sheetInstance).order_by('ORDER'))
...
countFormset.save()
astral dirge
fleet nest
#

is there any modifications done on timezone before save() ?

astral dirge
# fleet nest is there any modifications done on timezone before save() ?

No, here is (almost) all the code I removed from that ... section

countFormset = CountFormset(POSTdata, prefix='countFormset', queryset=DataSheetCount.objects.filter(Sheet=sheetInstance).order_by('ORDER'))
singleSheetForm.fields['Wetland'].queryset = Wetland.objects.all()
if not all([dataEntryForm.is_valid(), singleSheetForm.is_valid(), countFormset.is_valid()]):
  # redacted because this section will raise an exception so it doesn't enter here
# it's valid so save it
  try:
    with transaction.atomic():
      dataEntry = dataEntryForm.save(commit=False)
      dataEntry.LastEditBy = User.objects.get(pk=request.user.pk)
      if not hasattr(dataEntry, 'CreatedBy') or dataEntry.CreatedBy is None:
        dataEntry.CreatedBy = User.objects.get(pk=request.user.pk)
      dataEntry.save()
      entry_ref_id = dataEntry.DataEntryID
      postType = POSTdata.get("form-submit-type")
      if postType == 'delete':
        # redacted because it doesnt enter here
      else:
        # save the sheet
        singleSheet = singleSheetForm.save(commit=False)
    singleSheet.DataEntry = dataEntry
    if singleSheet.PageNumber is None:
      singleSheet.PageNumber = 1
      singleSheet.save()
      # save the counts
      countFormset.save(commit=False)
      shiftDeleted = 0
      # this loop includes deleted forms, so ignore them from the ORDER values
      for formIdx, countForm in enumerate(countFormset):
        countForm.instance.Sheet = singleSheet
        countForm.instance.ORDER = formIdx - shiftDeleted
        if countForm in countFormset.deleted_forms:
          shiftDeleted += 1
      countFormset.save()
shrewd wolf
#

Where are you seeing the date not be converted to the Sydney timezone?

astral dirge
#

Then when the form reloads the same form field is 2024-10-12 13:29:00

shrewd wolf
#

So thr 5 minutes thing feels like user error to me. Unless the timezone offset is 5minutes. The hours change feels like how Django should work. The data coming in from a form is assumed to be at the timezone of the application. When it's saved, it's converted to UTC and stored in the db as utc. When it's pulled back out, it's rendered shifted back as that timezone.

astral dirge
shrewd wolf
#

Can you try again with a different value to see if the shift is consistently 10:05?

astral dirge
shrewd wolf
#

You could search for a timedelta to see if that's being used somewhere.

#

Search for your date field to see where that's being used.

astral dirge
shrewd wolf
#

Does the database have a trigger on it? What is your machine's offset for thr active timezone? Otherwise I'm out of ideas.

astral dirge
#
Empty set (0.004 sec)

MariaDB [db]> quit
Bye
sh-5.1$ date
Thu Feb  6 10:29:17 PM AEDT 2025
sh-5.1$ timedatectl
               Local time: Thu 2025-02-06 22:29:39 AEDT
           Universal time: Thu 2025-02-06 11:29:39 UTC
                 RTC time: Thu 2025-02-06 11:29:39
                Time zone: Australia/Sydney (AEDT, +1100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no```
#

I'm going to go to sleep now. Hopefully I can have an elucidating dream... Thanks for both of your help 🙂

fleet nest
#

I would recommend make new simple django project, without any special code.
and see if the same problem happens or not.