Find objects with date and time less then 24 hours from now - python

I have model with two fields:
class Event(models.Model):
date = models.DateField(_(u'Date'))
time = models.TimeField(_(u'Time'))
I need to find all objects where date&time is in 24 hours from now.
I am able to do this when using DateTime field, but I am not sure how to achieve this when fields are separated. Thanks in advance.

For the simple case (not sure if all are simple cases though...), this should do the trick:
import datetime
today = datetime.datetime.now()
tomorrow = today + datetime.timedelta(days=1)
qs_today = queryset.filter(
date=today.date(),
time__gte=today.time(),
)
qs_tomorrow = queryset.filter(
date=tomorrow.date(),
time__lt=tomorrow.time(),
)
qs = qs_today | qs_tomorrow

As you state you can do what you want with a DateTimeField, but now with the separate fields, I understand your issue is how to combine them.
Looking at the docs for DateField - your date variable is a datetime.date instance and similarly for TimeField time is a datetime.time. You can convert these into a datetime.datetime by using combine()
import datetime as dt
datetime = dt.datetime.combine(date,time)
You now have the datetime object as you would have from DateTimeField. You say in the question you can do the 24 hour from now bit from there, although let me know in comments if you need that made explicit.
Caveat I combine will fail where one of the fields is None - you state this can't happen, so I haven't added any error checking or validation of this.
EDIT
It occurs to me that the problem may not be the combination, but adding the calculated field to the Event object. You could look at this Q&A, or this. In summary you define the calculated value in a function in your class and then make it a property - either with a decorator or a function call. There's an example in the docs, adapting for your case:
def _get_datetime(self):
'''Returns a combination of date and time as a datetime'''
return dt.datetime.combine(self.date,self.time)
datetime = property(_get_datetime)
This should behave in the same way as you would expect a DateTimeField to behave.

You can use Q objects to search for "yesterday after current time or today before current time":
from django.db.models import Q
from .models import Event
from datetime import datetime, timedelta
def get_event_during_last_day():
now = datetime.now()
today = now.date()
yesterday = (now - timedelta(day=1)).date()
time = now.time()
query_yesterday = Q(date=yesterday, time__gt=time)
query_today = Q(date=today, time__lt=time)
return Event.objects.filter(query_yesterday | query_today)

Related

How to filter objects in Django by time since last 24 hours?

Assuming there's a starting time from 00:00 to 00:00 every day, how best are Django objects filtered by time, based on the current day? I initially came up with this:
from django.utils import timezone
yesterday = timezone.now() - timezone.timedelta(1)
qs = Foo.objects.filter(date__gte=yesterday)
##
yesterday = datetime.date.today() - datetime.timedelta(1)
qs = Foo.objects.filter(date__gte=yesterday)
However, this is not particularly right. I would prefer time starting exactly from 00:00 to timezone.now() -so something like Foo.objects.filter(date__range=[00:00, timezone.now()]) Thank you.
Assuming date field is actually a datetime.
If you need all records with date containing todays date, you can use __date:
qs = Foo.objects.filter(date__date=timezone.now())
If you need, for example, all yesterdays records, but with time not greater than timezone.now(), this way:
qs = Foo.objects.filter(
date__date=timezone.now() - timezone.timedelta(1),
date__lte=timezone.now() - timezone.timedelta(1)
)
Yevgeniy Kosmak's answer is correct, use datetime_field__date= filter.
Here are the Django docs
Also you can use date/datetime - timedelta() pattern with others arguments in timedelta, like
timedelta(
days=50,
seconds=27,
microseconds=10,
milliseconds=29000,
minutes=5,
hours=8,
weeks=2
)
docs are here

ideas on how to build a function in python django for cellulating hours interval

I am a total beginner starting a project in Django a payroll calculator app. In it my user has workhours with dates form. The function required should calculate (dates, in hour, out hour) and output the value to another (total hours) field.
the restrictions are:
value must be an integer
value can be crossday meaning: giving a worker crossday shift
07.01.2021,08.01.2021/dates, 22:00:00pm/hour in, 06:00:00am/hour out
function must work with Django .models/.datetime
So far I got this code:
def HoursTotalConf(in_time, out_time):
start_dt = dt.datetime.strptime(in_time, "%H:%M:%S")
end_dt = dt.datetime.strptime(out_time, "%H:%M:%S")
return relativedelta(end_dt, start_dt) '
you can directly do operations with two datetime objects, so if you do end_dt - start_dt you will get a timedelta object.
Btw if you are looking to solve the problem within django, instead of using datetime, use timezone in Django. django timezone
from django.utils import timezone
def hours_total_conf(in_time, out_time):
start_dt = timezone.datetime.strptime(in_time, '%H:%M:%S')
end_dt = timezone.datetime.strptime(out_time, '%H:%M:%S')
time_diff = end_dt - start_dt
# round to hour
total_hours = round(time_diff.seconds/3600)
return total_hours
another thing to mention here is that, if you are solving the problem in Django as explained in restriction 3, why not define these two fields as datetime fields so that you don't have to convert it yourself?

DurationField or Timedelta

im having a problem trying to set a duration in my to-do tasks.
i've tried with DurationField and some people told me to try the timedelta in your forms.py but im not quite shure how to pass the difference like (6days) from my two model DateField (start and end).
Models.py
from django.db import models
from datetime import datetime, timedelta
class To_do (models.Model):
task = models.CharField(max_length=150)
topic = models.CharField(max_length=150)
how = models.TextField(max_length=600)
start = models.DateField(default=datetime.today)
end = models.DateField(blank=False)
duration = models.DurationField(default=timedelta)
i'd like to display the difference for the user and after set an alarm for less than 3 days etc.
How do I solve this?
The difference between two date or date/time values is a timedelta.
delta_time = end - start
Or if you need to code a delta-time constant from other numbers
from datetime import timedelta
my_delta = timedelta( days=3, hours=12, minutes=1 ) # half a week plus a minute
Don't use timedelta as the name as the default value if you are importing it! If what you mean to do is to pass a callable to calculate a timedelta, define a function to do that as above, and pass it as the default value.

NDB query by time part of DateTimeProperty

I need to query items that where added at some time of day, ignoring which day. I only save the DateTime of when the item was added.
Comparing datetime.Time to DateTimeProperty gives an error, and DateTimeProperty does not have a time() method.
The only way would be to store the time of day as a separate property. An int will be fine, you can store it as seconds. You could do this explicitly (ie set the time property at the same time you set the datetime, or use a computedproperty to automatically set the value.
I've the same problem (more or less) and I think that the only way is build our own date class model.
Something like this:
class Date(ndb.model):
...
day = ndb.IntegerProperty()
month = ndb.IntegerProperty()
year = ndb.IntegerProperty()
...
class Anything(ndb.Model):
...
dateTime = ndb.StructuredProperty(Date)
...
Instead of:
class Anything(ndb.Model):
...
dateTime = ndb.DateTimeProperty()
...
But also I think that we should have other way to do this easier.

Django: How to get a time difference from the time post?

Say I have a class in model
class Post(models.Model):
time_posted = models.DateTimeField(auto_now_add=True, blank=True)
def get_time_diff(self):
timediff = timediff = datetime.datetime.now() - self.time_posted
print timediff # this line is never executed
return timediff
I defined a get_time_diff to get the time difference from the time when the Post is posted up to now, according to the document, the DateTimeField should be able to be converted to datetime automatically, is that correct? Why the print statement is never being run? How can you extract the time difference?
Beside, if you get a time difference, is there an easy way to convert the time difference to an integer, like the number of seconds of the total time.
Your code is already working; a datetime.timedelta object is returned.
To get the total number of seconds instead, you need to call the .total_seconds() method on the resulting timedelta:
from django.utils.timezone import utc
def get_time_diff(self):
if self.time_posted:
now = datetime.datetime.utcnow().replace(tzinfo=utc)
timediff = now - self.time_posted
return timediff.total_seconds()
.total_seconds() returns a float value, including microseconds.
Note that you need to use a timezone aware datetime object, since the Django DateTimeField handles timezone aware datetime objects as well. See Django Timezones documentation.
Demonstration of .total_seconds() (with naive datetime objects, but the principles are the same):
>>> import datetime
>>> time_posted = datetime.datetime(2013, 3, 31, 12, 55, 10)
>>> timediff = datetime.datetime.now() - time_posted
>>> timediff.total_seconds()
1304529.299168
Because both objects are timezone aware (have a .tzinfo attribute that is not None), calculations between them take care of timezones and subtracting one from the other will do the right thing when it comes to taking into account the timezones of either object.
Assuming you are doing this within a template, you can also use the timesince template tag.
For example:
{{ blog_date|timesince:comment_date }}
Your code
timediff = datetime.datetime.now() - self.pub_date
should work to get the time difference. However, this returns timedelta object. To get difference in seconds you use .seconds attribute
timediff = datetime.datetime.now() - self.pub_date
timediff.seconds # difference in seconds.
Just in case you want to put this process in you Django signals. Here's the one that is working for me. Hope this helps!
from django.db.models.signals import pre_save
from django.dispatch import receiver
from .models import YourModel
from datetime import datetime
#receiver(pre_save, sender = YourModel)
def beforeSave(sender, instance, **kwargs):
date_format = "%H:%M:%S"
# Getting the instances in your model.
time_start = str(instance.time_start)
time_end = str(instance.time_end)
# Now to get the time difference.
diff = datetime.strptime(time_end, date_format) - datetime.strptime(time_start, date_format)
# Get the time in hours i.e. 9.60, 8.5
result = diff.seconds / 3600;
Simply we can add the custom property to calculate the time difference with the help #property decorator in that model.
from django.utils import timezone
class Post(models.Model):
time_posted = models.DateTimeField(auto_now_add=True, blank=True)
content = models.TextField()
#property
def time_diff(self):
return timezone.now() - self.time_posted
time_diff will return object of datetime.timedelta
post = Post.objects.get(pk=1) # Post model object
# time diff in seconds.
post.time_diff.seconds
>>> 652
# time diff in days.
post.time_diff.days
>>> 0
Already answered above nicely by Martijn Pieters, just adding #property, and django.utils.timezone to calculate the difference with respective timezone from settings.py

Categories

Resources