Django Date Input Parsing? - python

I'm trying to get a date for an event from a user.
The input is just a simple html text input.
My main problem is that I don't know how to parse the date.
If I try to pass the raw string, I get a TypeError, as expected.
Does Django have any date-parsing modules?

If you are using django.forms look at DateField.input_formats. This argument allows to define several date formats. DateField tries to parse raw data according to those formats in order.

Django doesn't, so to speak, by Python does. It seems I'm wrong here, as uptimebox's answer shows.
Say you're parsing this string: 'Wed Apr 21 19:29:07 +0000 2010' (This is from Twitter's JSON API)
You'd parse it into a datetime object like this:
import datetime
JSON_time = 'Wed Apr 21 19:29:07 +0000 2010'
my_time = datetime.datetime.strptime(JSON_time, '%a %b %d %H:%M:%S +0000 %Y')
print type(my_time)
You'd get this, confirming it is a datetime object:
<type 'datetime.datetime'>
More information on strptime() can be found here.

(In 2017), you could now use django.utils.dateparse

The DateField can be used outside of Django forms.
Example, when used {{ value|date:"SHORT_DATE_FORMAT" }} in template:
from django.forms import DateField
from django.utils import formats
# need '%d.%m.%Y' instead of 'd.m.Y' from get_format()
dformat = ('.' + formats.get_format("SHORT_DATE_FORMAT", lang=request.LANGUAGE_CODE)).replace('.', '.%').replace('-', '-%').replace('/', '/%')[1:]
dfield = DateField(input_formats=(dformat,))
<date> = dfield.to_python(<string>)

Related

Parse timestamp from a txt file?

I have a large text file on the web that I am using requests to obtain and parse data from. The text file begins each line with a format like [Mon Oct 10 08:58:26 2022]. How can I get the latest 7 days or convert only the datetime to an object or string for storing and parsing later? I simply want to extract the timestamps from the log and print them
You can use TimedRotatingFileHandler for daily or 7-days logs.
read more about timed rotating file handler here
and
read more about extracting timestamps from files
Can you tell me if this snippet solves your problem?
from datetime import datetime
log_line = "[Sun Oct 09 06:14:26 2022] Wiladoc is browsing your wares."
_datetime = log_line[1:25]
_datetime_strp = datetime.strptime(_datetime, '%a %b %d %H:%M:%S %Y')
print(_datetime)
print(_datetime_strp)

localize date time in python

Overview I receive the timestamp from server_x, my application is on server_y, both are in different regions, my application calls server_x api and receives json which has timestamp, now i need to perform some calculation on server_y, for that i need to make sure that the timestamp i receive from server_x could be used to covert the local datetime of server_y , so both are in sync
I want to convert datetime.now() to the timezone I receive from server for e.g., UTC-07:00
Current solution, I pass server_timestamp to the function and then I pass its zone info to the datetime.now
Server_timestamp = "2020-04-04T10:24:49.000-0700"
dt = datetime.strptime(Server_timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")
convert_local = datetime.now(dt.tzinfo)
Problem:
I need to save the timezone of the server in db and then use that instead of passing server_timestamp everytime, the tzinfo gives a type datetime.timezone = UTC-07:00, after storing this string how can I use it to change the localtime.
Here's a function that utilizes the datetime library to convert a datetime object from one timezone to another:
from datetime import datetime
import pytz
def convert_tz(dt, current_tz, out_tz):
return dt.replace(tzinfo=current_tz).astimezone(tz=out_tz)
now = datetime.now(tz=pytz.timezone('US/Eastern'))
convert = datetime.now().astimezone().tzinfo
print(now)
print(utc_to_local(now, now.tzinfo, convert))
Output:
2020-05-10 17:02:44.245703-04:00
2020-05-10 16:02:44.245703-05:00
I used the pytz library for demonstration purposes. For you, to get the server's timezone, use the line datetime.now().astimezone().tzinfo.
I implemented a solution.
I now save the last 5 char of the timestamp in the db "-0700".
time_zone = query_from_db
tz = datetime.strptime(time_zone, "%z")
datetime_now = datetime.now(tz.tzinfo)

Read specific part of json string with python

I am currently working on a programme within the django environment which operates off a json api provided by a third party. There is an object within that API which I want however the string of information it provides is too much for me.
The data I want is the created_at tag from the twitter api using tweepy. This created_at contains data in the following format:
"created_at": "Mon Aug 27 17:21:03 +0000 2012"
This is all fine however this will return the date AND time whereas I simply want the the time part of the above example i.e. 17:21:03.
Is there any way I can just take this part of the created_at response string and store it in a separate variable?
You can use the dateutil module
from dateutil import parser
created_at = "Mon Aug 27 17:21:03 +0000 2012"
created_at = parser.parse(created_at)
print created_at.time()
Output:
17:21:03
Try below code.
my_datetime = response_from_twitter['created_at']
my_time = my_datetime.split(' ')[3]
# my_time will now contain time part.
You could just split the string into a list and take the 4th element:
time = source['created_at'].split(' ')[3]
What about a regular expression with re.search():
>>> import re
>>> d = {"created_at": "Mon Aug 27 17:21:03 +0000 2012"}
>>> re.search('\d{2}:\d{2}:\d{2}', d['created_at']).group(0)
'17:21:03'

Python Twitter API TwitterError: user_id must be type int

I'm trying to use python-twitter. I'm following this tutorial.
Here is my code:
tweet = cache.get('tweet')
if not tweet:
tweet = twitter.Api().GetUserTimeline(settings.TWITTER_USER)[0]
tweet.date = datetime.strptime(tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y")
cache.set('tweet', tweet, settings.TWITTER_TIMEOUT)
But I'm getting next error:
Twitter.error.TwitterError: {'message': '"user_id" must be type int'}
I've set the variables like in the tutorial:
TWITTER_USER = 'CaseyNeistat'
TWITTER_TIMEOUT = 3600
Is there anything I'm doing wrong?
The article got created about 9 years ago. So the API could changed many times since then.
Here is the signature of the function from python-twitter documentation:
GetUserTimeline(user_id=None, screen_name=None, since_id=None, max_id=None, count=None, include_rts=True, trim_user=False, exclude_replies=False)
So you want to do GetUserTimeline(screen_name=settings.TWITTER_USER)
Otherwise your string would go as user_id.
Just use screen_name as function argument.
For example, GetUserTimeline(screen_name="elonmusk")

How do I get the current date and current time only respectively in Django?

I came across an interesting situation when using this class:
class Company(models.Model):
date = models.DateField()
time = models.TimeField()
c = Company(date=datetime.datetime.now(), time=datetime.datetime.now())
Django decides to use DATETIME_INPUT_FORMATS defined within the formats.py file.
Which makes sense, because I am passing in a datetime.now() to both fields.
I think I could make Django to use DATE_INPUT_FORMATS and TIME_INPUT_FORMATS respectively, if I passed in only the current date and current time in.
Something like this:
c = Company(date=datetime.date.now(), time=datetime.time.now())
But this obviously throws an exception as now doesn't exist like that. Is there a different way to achieve this?
For the date, you can use datetime.date.today() or datetime.datetime.now().date().
For the time, you can use datetime.datetime.now().time().
However, why have separate fields for these in the first place? Why not use a single DateTimeField?
You can always define helper functions on the model that return the .date() or .time() later if you only want one or the other.
import datetime
datetime.datetime.now().strftime ("%Y%m%d")
20151015
For the time
from time import gmtime, strftime
showtime = strftime("%Y-%m-%d %H:%M:%S", gmtime())
print showtime
2015-10-15 07:49:18
import datetime
datetime.date.today() # Returns 2018-01-15
datetime.datetime.now() # Returns 2018-01-15 09:00
import datetime
Current Date and time
print(datetime.datetime.now())
#2019-09-08 09:12:12.473393
Current date only
print(datetime.date.today())
#2019-09-08
Current year only
print(datetime.date.today().year)
#2019
Current month only
print(datetime.date.today().month)
#9
Current day only
print(datetime.date.today().day)
#8
A related info, to the question...
In django, use timezone.now() for the datetime field, as django supports timezone, it just returns datetime based on the USE TZ settings, or simply timezone 'aware' datetime objects
For a reference, I've got TIME_ZONE = 'Asia/Kolkata' and USE_TZ = True,
from django.utils import timezone
import datetime
print(timezone.now()) # The UTC time
print(timezone.localtime()) # timezone specified time,
print(datetime.datetime.now()) # default local time
# output
2020-12-11 09:13:32.430605+00:00
2020-12-11 14:43:32.430605+05:30 # IST is UTC+5:30
2020-12-11 14:43:32.510659
refer timezone settings and Internationalization and localization in django docs for more details.
Another way to get datetime UTC with milliseconds.
from datetime import datetime
datetime.utcnow().isoformat(sep='T', timespec='milliseconds') + 'Z'
2020-10-29T14:46:37.655Z

Categories

Resources