I need to create random dates with the next format 2017-03-29 12:10+0200, 2017-03-29 14:08-0400. The generated dates must have to be between a start date and a final date.
How can I do this in Python 2.7
You can do this by getting a timestamp as integer from each and then getting a random integer between these two integers you've found. Which is the timestamp in between, then you can convert that timestamp back to a datetime object again:
from dateutil.parser import parse
from datetime import datetime
from random import randint
import time
timestamp_one = time.mktime(parse("2017-03-29 12:10+0200").timetuple())
timestamp_two = time.mktime(parse("2017-03-29 14:08-0400").timetuple())
timestamp = randint(timestamp_one,timestamp_two)
result = datetime.fromtimestamp(timestamp)
print result
In python 3 you can also use the .timestamp() on the datetime object directly.
Related
I want to filter all data for the following day (which is timezone aware).
Assumptions:
server data is in a specific timezone
client query is coming from different timezone
My current approach is:
date = received_date_timezone_aware # any time of that day
lower = date.replace(hour=0,minute=0,second=0)
upper = lower + datetime.timedelta(day=1)
data = Model.objects.filter(date__gte=lower, date__lt=upper)
Question
Is there a direct solution to this using django orm or raw query?
Note: I wanted to know if there is a better way which can save me a few lines of code of manipulating the datetime myself
No, there isn't. However what you're doing isn't the best way to do it either as using replace can result in some oddities with day lights savings. I'd recommend using lower = received_date_timezone_aware.date().
This assumes Model.date is a DateField. If it's a datetime, then do this:
from datetime import time, datetime
def min_datetime(date: datetime, tz):
"""Get the min datetime of the given datetime and timezone.
:return datetime: Minimum datetime of the given date.
"""
return tz.localize(
datetime.combine(date.astimezone(tz).date(), time.min))
def max_datetime(date: datetime, tz):
"""Get the max datetime of the given datetime and timezone.
:return datetime: Maximum datetime of the given date.
"""
return tz.localize(
datetime.combine(date.astimezone(tz).date(), time.max))
Model.objects.filter(date__range=(min_datetime(value, tz), max_datetime(value, tz)))
I am trying to fetch all records from a table on a particular date.
My url.py code is :
url(r'^jobs/date/?P<date>.*',RunningJobsListApiView.as_view()),
Here is the code of my view to get all the records from the table.
class RunningJobsListApiView(generics.ListAPIView):
queryset = LinuxJobTable.objects.annotate(status=Case(When(state=3, then=Value('Completed')),When(state=5, then=Value('Failed')),When(state=1, then=Value('Running')),default=Value('Unknown'),output_field=CharField(),),)
serializer_class = JobInfoSerializer
Now, I want to filter the jobs for the particular date in url. But In my database date is in UNIX timestamp format(ie.1530773247).
How can I compare DateFormat(mm-dd-yyyy) with UNIX timestamp format saved in DB?
To get a UNIX timestamp from a string date representation, you first need to convert the string to a Python datetime with strptime(), and then call the timestamp() method on it. But since a single day comprises a range of timestamps, you need to do a range query between the start of the target day and the start of the next day.
Something like:
from datetime import datetime, timedelta
target_day = datetime.strptime(date, "%m-%d-%Y")
next_day = target_day + timedelta(days=1)
queryset = LinuxJobTable.objects.filter(timestamp__range=(
int(target_day.timestamp()),
int(next_day.timestamp()) - 1 # since range is inclusive
))
I have used datetime.datetime.now() for storing datefield in my model which is saved as 2016-06-27 15:21:17.248951+05:30. Now I want to compare the datefield with the datetime value getting from the frontend, like Thu May 26 2016 00:00:00 GMT 0530 (IST). How should Django query the model to compare both datefields?
# models.py
datefield = models.DateTimeField(blank=True,null=True)
I have tried converting datefield getting from frontend by using split() and remove() function of Python to create it in format as 2016-06-27 13:25:35.
But still no solution and getting Null even I am comparing same date value like this (2016-06-27 13:25:35) value with this (2016-06-27 12:36:34.898593+00) value, as date in both values are same.
I have checked it using simple Django query as follows:
company_details.objects.filter(datefield=datefield).only('par1','par2',...)
Once you have converted the date from the front end into a ISO format like 2016-06-27 13:25:35, you can use it to query the model with one of these
Model.objects.filter(date_created__startswith=today)
Model.objects.filter(date_created__contains=today)
It works too if you are only looking to filter for a certain date like 2016-06-27.
Model.objects.filter(date_created__startswith=date(2016, 6, 27))
To parse the date string you are getting from the frontend, there are two options.
If your date string is well-formatted, you can simply parse it with for example datestring.strftime('%Y-%m-%d %H:%M:%S') to get a datetime() object.
If your date string is unreliable and may be formatted in different ways that you can't predict, I would recommend to using DateUtil. It comes with a parser that will try to convert any string into a datetime, for example
>>> from dateutil.parser import parse
>>> parse("Today is January 1, 2047 at 8:21:00AM", fuzzy_with_tokens=True)
(datetime.datetime(2011, 1, 1, 8, 21), (u'Today is ', u' ', u'at '))
Compare the date and time like this. Just give the variable like this '%Y-%m-%d %H:%M:%S' as much you want.
In [1]: from datetime import datetime
In [2]: past = datetime.now()
In [3]: present = datetime.now()
In [4]: present.strftime('%Y-%m-%d %H:%M:%S') == past.strftime('%Y-%m-%d %H:%M:%S')
Out[17]: False
In [5]: present.strftime('%Y-%m-%d %H:%M') == past.strftime('%Y-%m-%d %H:%M')
Out[2]: True
If you want to compare only the date use like this.
if present.date() == past.date():
#Do your stuff
I'm trying to export some data from Django to Excel using openpyxl.
The exported datetimes are correctly interpreted as such in Excel, but their printout is in UTC, not in local timezone (CET in this case) as I'd expect.
I tried to use to_excel, but that only outputs datetimes converted to excel's internal float format. Which is in addition also interpreted as float, not datetime. When formatted as datetime, it's still in UTC
I also tried to use Django's django.utils.timezone.localtime, but again the dates are rendered in UTC.
I could just subtract the timezone offset from my dates, but I feel it's more likely I'm missing something here.
How can I export datetime data such that Excel would display it in my local timezone?
I had the similar problem and solved it in the following way.
May be it helps.
from dateutil.tz import tzutc, tzlocal
TZ_UTC = tzutc() # UTC timezone
TZ_LOCAL = tzlocal() # Local timezone
datevalue = value #value I get from API I am using, which is datetime object.
# For some reason I don't get tzinfo filled).
datevalue = datevalue.replace(tzinfo=TZ_UTC) # Adding time zone info for UTC
datevalue = datevalue.astimezone(TZ_LOCAL) # Converting to local timezone
datevalue = datevalue.replace(tzinfo=None) # Removing tzinfo to correctly record local time to Excel
cell.value = datevalue
Excel itself has no concept of timezones and will always dates and times naively. In this context the only sane thing to do is to convert to UTC which is what openpyxl does. openpyxl.utils.datetime is the module to look at if you want to change this
I ended up using a combination of javascript & server-side processing:
At the client HTML I create an input for user's local timezone:
<input type="hidden" value="" name="tz" id="xls-tz"/>
and populate its value (using jQuery):
$("#xls-tz").val(new Date().getTimezoneOffset());
At the server, I parse the timezone offset & write to openpyxl accordingly:
tz_offs = int(request.GET.get("tz", "0"))
ws.cell(row=row, column=2, value=item.time - timedelta(minutes=tz_offs))
That comes IMO pretty close to what I needed.
Thanks Charlie for the hint about Excel not being TZ aware.
I got a pandas dataframe, containing timestamps 'expiration' and 'date'.
I want to filter for rows with a certain maximum delta between expiration and date.
When doing fr.expiration - fr.date I obtain timedelta values, but don't know how
to get a filter criteria such as fr[timedelta(fr.expiration-fr.date)<=60days]
for the 60 days you're looking to compare to, create a timedelta object of that value timedelta(days=60) and use that for the filter. and if you're already getting timedelta objects from the subtraction, recasting it to a timedelta seems unnecessary.
and finally, make sure you check the signs of the timedeltas you're comparing.
# sashkello
Thanks,
filterfr = filterfr[filterfr.expiration-filterfr.date <= numpy.timedelta64(datetime.timedelta(days = 60))]
did the trick.
filterfr.expiration-filterfr.date
resulted in timedelta64 values
and raised TypeError: can't compare datetime.timedelta to long.
Converting to numpy.timedelta before comparision worked.