I'm scraping a a page that includes among other things, date information. So I have a variable named warrant_issued that contains u'11/5/2003' -- I want to store this as a machine readable date. PHP has a handy strtotime function that works fabulously. I was hoping that datetime's strptime would help me but it doesn't seem to be available in my version of datetime -- here is everything in my tab complete on datetime.
In [231]: datetime.
datetime.MAXYEAR datetime.__hash__ datetime.__sizeof__
datetime.MINYEAR datetime.__init__ datetime.__str__
datetime.__class__ datetime.__name__ datetime.__subclasshook__
datetime.__delattr__ datetime.__new__ datetime.date
datetime.__dict__ datetime.__package__ datetime.datetime
datetime.__doc__ datetime.__reduce__ datetime.datetime_CAPI
datetime.__file__ datetime.__reduce_ex__ datetime.time
datetime.__format__ datetime.__repr__ datetime.timedelta
datetime.__getattribute__ datetime.__setattr__ datetime.tzinfo
I'm using iPython 2.7.2+
Am I barking up the wrong tree here? What's the best way to turn u'11/5/2003' into a date?
strptime() is definitely the right approach, it's just a class method for the datetime class (confusingly part of the datetime module).
That is, datetime.datetime.strptime() is what you're looking for (and not datetime.strptime().
Try this:
For use with the datetime module, documentation here
>>>import datetime
>>>a = u'11/5/2003'
>>>time1 = datetime.datetime.strptime(a, "%m/%d/%Y")
>>>print time1
datetime.datetime(2003, 11, 5, 0, 0)
In ipython:
In [1]: import datetime
In [2]: a = u'11/5/2003'
In [3]: time1 = datetime.datetime.strptime(a, "%m/%d/%Y")
In [4]: print time1
2003-11-05 00:00:00
Use with the time module, documentation here
>>>import time
>>>a = u'11/5/2003'
>>>time1 = time.strptime(a, "%m/%d/%Y")
>>>print time1
time.struct_time(tm_year=2003, tm_mon=11, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=309, tm_isdst=-1)
Thanks to the comment reply from #David Cain:
The dateutil library can parse datetime strings (inferring their format). However, note that "11/5/2003" is not an unambiguous format (MM/DD or DD/MM differs by locale), so dateutil should be used with caution in this case. – David Cain
So an alternative good practice is to use the dateutil library:
>>> from dateutil.parser import parse
>>> dt = parse('2016/12/05 05:18 pm')
>>> dt
datetime.datetime(2016, 12, 5, 17, 18)
>>> dt.timestamp()
1480929480.0
>>> parse('16/11/12')
>>> datetime.datetime(2012, 11, 16, 0, 0)
strtotime you use to work with in PHP may be most similar to dateparser.
Note if you don't have the dateparser library.
Install it with
pip3 install dateparser
If you are using conda
conda install dateparser
This library understands over 200 language locales plus numerous formats in a language agnostic fashion.
Parsing generic terms also works:
import dateparser
print (dateparser.parse('yesterday')) #2019-05-19 08:08:14.934992
And also supports non-Gregorian calendar systems.
Related
I am looking to retrieve the next possible date for a weekday contained in a string. Complexity being that this weekday will be in foreign language (sv_SE).
In bash I can solve this using `dateround´:
startdate=$(dateround --from-locale=sv_SE -z CET today $startday)
Highly appreciate your guidance on how to solve this in Python.
Thank you very much!
Dateparser has support for quite a few languages. You could parse the weekday to a datetime object then determine the next possible date available.
-- Edit --
from dateparser import parse
parse('Onsdag').isoweekday() # 3
Now that you have the iso weekday, you can find the next possible date. You can refer to this to see how.
It seems locale aliases are platform specific and case sensitive. I've windows. So locale will be sv_SE.
You can use babel for date/time conversion and is much more comprehensive than native locale module.
Babel is an integrated collection of utilities that assist in internationalizing and localizing Python applications, with an emphasis on web-based applications.
Which can be installed as:
pip install Babel
Once installed, we can use format_date , format_datetime , format_time utilities to format one language date , time to other.
You can use these utilities to convert date/time data between English and Swedish.
>>>import datetime
>>>from babel.dates import format_date, format_datetime, format_time
#Here we get current date time in an datetime object
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2017, 10, 31, 9, 46, 32, 650000)
#We format datetime object to english using babel
>>> format_date(now, locale='en')
u'Oct 31, 2017'
#We format datetime object to sweedish using babel
>>> format_date(now, locale='sv_SE')
u'31 okt. 2017'
>>>
This question already has answers here:
Convert timestamps with offset to datetime obj using strptime
(4 answers)
Closed 6 years ago.
I have time as 2016-03-07 14:42:48.901013+05:30 but this is in the form of string. I want to have it as a datetime object type , as I want to compare it with the update_ts(auto_add_now) field of django. Any help to achieve this will be highly appreciated.
I have tried using x = datetime.strptime(ts, '%Y-%m-%dT%H:%M:%S.%UTZ') but it doesn't work for me.
You should use correct format '%Y-%m-%d %H:%M:%S.%f' and ignore timezone: ts.split('+')[0]
x= datetime.strptime(ts.split('+')[0],'%Y-%m-%d %H:%M:%S.%f')
ofcouse you should handle timezone by yourself or use default one.
Also you can use dateutil.parser.parse:
First install the python-dateutil package: pip install python-dateutil
Then:
from dateutil.parser import parse
x = parse(ts)
datetime.datetime(2016, 3, 7, 14, 42, 48, 901013, tzinfo=tzoffset(None, 19800))
Check this reference too: Convert timestamps with offset to datetime obj using strptime
You can do this with dateutil parser
Before Using dateutil , install it using pip install python-dateutil
from dateutil import parser
your_date_string = '2016-03-07 14:42:48.901013+05:30'
yourDate = parser.parse(your_date_string)
# yourDate -> datetime.datetime(2016, 3, 7, 14, 42, 48, 901013, tzinfo=tzoffset(None, 19800))
If you want Datetime object alone ,
a = '2016-03-07 14:42:48.901013+05:30'.split('+')[0]
get_dateiobj = datetime.strptime(a,"%Y-%m-%d %H:%M:%S.%f")
it will return upto your timezone
dateutil also the better method it seems like angularjs - momentjs .
You can parse multiple dateformat using this.
pip install python-dateutil
than you can parse different format like,
from dateutil.parser import *
now = parse("2016-03-07 14:42:48.901013+05:30")
I am reading different log files with different date formats. I am using python to read in the files line by line and then parse the line. I want to parse the line for dates and then formulate them into a date object to do comparisons on.
For example, say that I have 2 log files both with different date formats. How do I read them into an object to compare them to a known date. Assume for example, I wanted to discard all dates before a certain time.
Let's assume the first log file just has one line:
invalid access 2015-01-04 14:23:15 on IP 5.5.5.5
How do I read in 2015-01-04 14:23:15 into a dateobject (so I can do comparisons)
What if the date format was different? How would I read in that?
You can use datetime.datetime.strptime:
In [1]: from datetime import datetime
In [2]: d = '2015-01-04 14:23:15'
In [3]: datetime.strptime(d, '%Y-%m-%d %H:%M:%S')
Out[3]: datetime.datetime(2015, 1, 4, 14, 23, 15)
For other formats, check out the documentation
dateutil can usually parse whatever
import dateutil.parser as p
print p.parse("2015-01-04 14:23:15")
this assumes you can isolate your datestring
$ easy_install python-dateutil
$ pip install python-dateutil
or simply attainable at https://pypi.python.org/pypi/python-dateutil/2.4.0 if you need the source ...
Using re module directly might be more efficient than using it implicitly via datetime.strptime() (measure it to find out whether it matters in your case):
>>> import datetime, re
>>> s = '2015-01-04 14:23:15'
>>> datetime.datetime(*map(int, re.findall('\d+', s)))
datetime.datetime(2015, 1, 4, 14, 23, 15)
See the following code:
import datetime
import pytz
fmt = '%Y-%m-%d %H:%M:%S %Z'
d = datetime.datetime.now(pytz.timezone("America/New_York"))
d_string = d.strftime(fmt)
d2 = datetime.datetime.strptime(d_string, fmt)
print d_string
print d2.strftime(fmt)
the output is
2013-02-07 17:42:31 EST
2013-02-07 17:42:31
The timezone information simply got lost in the translation.
If I switch '%Z' to '%z', I get
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z'
I know I can use python-dateutil, but I just found it bizzare that I can't achieve this simple feature in datetime and have to introduce more dependency?
Part of the problem here is that the strings usually used to represent timezones are not actually unique. "EST" only means "America/New_York" to people in North America. This is a limitation in the C time API, and the Python solution is… to add full tz features in some future version any day now, if anyone is willing to write the PEP.
You can format and parse a timezone as an offset, but that loses daylight savings/summer time information (e.g., you can't distinguish "America/Phoenix" from "America/Los_Angeles" in the summer). You can format a timezone as a 3-letter abbreviation, but you can't parse it back from that.
If you want something that's fuzzy and ambiguous but usually what you want, you need a third-party library like dateutil.
If you want something that's actually unambiguous, just append the actual tz name to the local datetime string yourself, and split it back off on the other end:
d = datetime.datetime.now(pytz.timezone("America/New_York"))
dtz_string = d.strftime(fmt) + ' ' + "America/New_York"
d_string, tz_string = dtz_string.rsplit(' ', 1)
d2 = datetime.datetime.strptime(d_string, fmt)
tz2 = pytz.timezone(tz_string)
print dtz_string
print d2.strftime(fmt) + ' ' + tz_string
Or… halfway between those two, you're already using the pytz library, which can parse (according to some arbitrary but well-defined disambiguation rules) formats like "EST". So, if you really want to, you can leave the %Z in on the formatting side, then pull it off and parse it with pytz.timezone() before passing the rest to strptime.
Unfortunately, strptime() can only handle the timezone configured by your OS, and then only as a time offset, really. From the documentation:
Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).
strftime() doesn't officially support %z.
You are stuck with python-dateutil to support timezone parsing, I am afraid.
Here is my answer in Python 2.7
Print current time with timezone
from datetime import datetime
import tzlocal # pip install tzlocal
print datetime.now(tzlocal.get_localzone()).strftime("%Y-%m-%d %H:%M:%S %z")
Print current time with specific timezone
from datetime import datetime
import pytz # pip install pytz
print datetime.now(pytz.timezone('Asia/Taipei')).strftime("%Y-%m-%d %H:%M:%S %z")
It will print something like
2017-08-10 20:46:24 +0800
Try this:
import pytz
import datetime
fmt = '%Y-%m-%d %H:%M:%S %Z'
d = datetime.datetime.now(pytz.timezone("America/New_York"))
d_string = d.strftime(fmt)
d2 = pytz.timezone('America/New_York').localize(d.strptime(d_string,fmt), is_dst=None)
print(d_string)
print(d2.strftime(fmt))
I need to know the current time at CDT when my Python script is run. However this script will be run in multiple different timezones so a simple offset won't work.
I only need a solution for Linux, but a cross platform solution would be ideal.
pytz or dateutil.tz is the trick here. Basically it's something like this:
>>> from pytz import timezone
>>> mytz = timezone('Europe/Paris')
>>> yourtz = timezone('US/Eastern')
>>> from datetime import datetime
>>> now = datetime.now(mytz)
>>> alsonow = now.astimezone(yourtz)
The difficulty actually lies in figuring out which timezone you are in. dateutil.tz is better at that.
>>> from dateutil.tz import tzlocal, gettz
>>> mytz = tzlocal()
>>> yourtz = gettz('US/Eastern')
If you want all the nitty gritty details of why timezones are evil, they are here:
http://regebro.wordpress.com/2007/12/18/python-and-time-zones-fighting-the-beast/
http://regebro.wordpress.com/2008/05/10/python-and-time-zones-part-2-the-beast-returns/
http://regebro.wordpress.com/2008/05/13/thanks-for-the-testing-help-conclusions/
You can use time.gmtime() to get time GMT (UTC) from any machine no matter the timezone, then you can apply your offset.
A simple offset will work, you just need to offset from UTC.
Using datetime you can get the current utc (gmt) time and use datetime objects:
datetime.datetime.utcnow() - Provides time at UTC
datetime.datetime.now() - Provides time at local machine
To get the CT time from any system you need to know the CT time offset from UTC.
Then to account for daylight savings time code a function to get the current offset.
>>> import datetime
>>> utc = datetime.datetime.utcnow()
>>> current_ct_offset = get_current_ct_offset()
>>> ct_datetime = utc + datetime.timedelta(hours=current_ct_offset)
I could be overlooking something here, but if your only concerned about one timezone and your not doing tz name handling, it's pretty straight forward.