0

Here I'm using Django and comparing timing. But I'm unable to do that. In the following code. wts value which is time value coming as a string. If it comes in string type then first I convert that in regular time after that I subtract 5 hours from it. and then compare if it is less than current or not..

if not wts:
            raise ValueError('When to send(wts) require ')
        
        if type(wts) is str:
            wts_compare = parser.parse(wts)
            print("wts time type ->>>" ,type(wts_compare),wts_compare)
            wts = wts_compare - timedelta(hours=5)
            if wts_compare <= timezone.now():
                raise ValueError('when to send (wts) must be a future date-time')
        else:
            wts = wts-timedelta(hours=5)
            if wts <= timezone.now():
                raise ValueError('when to send (wts) must be a future date-time')
Ahmed Yasin
  • 886
  • 1
  • 7
  • 31

1 Answers1

0

You can use django build-in timezone.make_naive and timezone.make_aware

from django.utils import timezone

In [1]: timezone.now()
Out[1]: datetime.datetime(2022, 6, 13, 17, 57, 41, 613329, tzinfo=<UTC>)

In [2]: timezone.make_naive(timezone.now()) # i am in UTC+2
Out[2]: datetime.datetime(2022, 6, 13, 19, 57, 34, 94246)

so now it's fine to compare all naive dates you want (use timezone.is_naive for avoiding error)

docs : https://docs.djangoproject.com/fr/4.0/ref/utils/#django.utils.timezone.is_naive

MaximeK
  • 2,039
  • 1
  • 10
  • 16