How to convert a date into a time period python - python

User case:
June 2016(OR 06/2016) => 06/01/2016-06/30/216
June 2016 to Dec 2016(OR between June 2016 and Dec 2016) => 06/01/2016-12/31/216
I'm wondering if there a python library or API service I can call from python, so that I can translate date in a natural language into a standard time period like the above user case

Looks like dateutil should help.
Ex:
from dateutil import parser
from dateutil.relativedelta import relativedelta
s = 'June 2016(OR 06/2016)'
s = s.split("(OR")
start = parser.parse(s[0]).strftime("%m/01/%Y")
end = (parser.parse(start) + relativedelta(day=31)).strftime("%m/%d/%Y")
print "{0}-{1}".format(start, end)
Output:
06/01/2016-06/30/2016

Related

how to add month abbr to string and create Dynamic string using python

I want make string Dynamic with Month abbr in it.
for eg.
01] In current Month (Means Sep) it will print below string.
i.e: payment links that are yet to be renewed for the month of Aug to Oct 2022
02] In Next Month (Means Oct) it will print below string.
i.e: payment links that are yet to be renewed for the month of Sep to Nov 2022
You ignore the corner cases. For instance, if current month is Dec. 2022, what you expect should be Nov. 2022 to Jan. 2023, not Nov. to Jan. 2022.
You can use dateutil
import datetime
from dateutil import relativedelta
now = datetime.date.today()
prev_month = now - relativedelta.relativedelta(months=1)
next_month = now + relativedelta.relativedelta(months=1)
print(f"payment links that are yet to be renewed for the month of {prev_month.strftime('%b %Y')} to {next_month.strftime('%b %Y')}")

How to efficiently parse Time/Date string into datetime object?

I'm scraping data from a news site and want to store the time and date these articles were posted. The good thing is that I can pull these timestamps right from the page of the articles.
When the articles I scrape were posted today, the output looks like this:
17:22 ET
02:41 ET
06:14 ET
When the articles were posted earlier than today, the output looks like this:
Mar 10, 2021, 16:05 ET
Mar 08, 2021, 08:00 ET
Feb 26, 2021, 11:23 ET
Current problem: I can't order my database by the time the articles were posted, because whenever I run the program, articles that were posted today are stored only with a time. Over multiple days, this will create a lot of articles with a stamp that looks as if they were posted on the day you look at the database - since there is only a time.
What I want: Add the current month/day/year in front of the time stamp on the basis of the already given format.
My idea: I have a hard time to understand how regex works. My idea would be to check the length of the imported string. If it is exactly 8, I want to add the Month, Date and Year in front. But I don't know whether this is a) the most efficient approach and b) most importantly, how to code this seemingly easy idea.
I would glady appreciate if someone can help me how to code this. The current line which grabs the time looks like this:
article_time = item.select_one('h3 small').text
Try this out and others can correct me if I overlooked something,
from datetime import datetime, timedelta
def get_datetime_from_time(time):
time, timezone = time.rsplit(' ', 1)
if ',' in time:
article_time = datetime.strptime(time, r"%b %d, %Y, %H:%M")
else:
article_time = datetime.strptime(time, r"%H:%M")
hour, minute = article_time.hour, article_time.minute
if timezone == 'ET':
hours = -4
else:
hours = -5
article_time = (datetime.utcnow() + timedelta(hours=hours)).replace(hour=hour, minute=minute) # Adjust for timezone
return article_time
article_time = item.select_one('h3 small').text
article_time = get_datetime_from_time(article_time)
What I'm doing here is I'm checking if a comma is in your time string. If it is, then it's with date, else it's without. Then I'm checking for timezone since Daylight time is different than Standard time. So I have a statement to adjust timezone by 4 or 5. Then I'm getting the UTC time (regardless of your timezone) and adjust for timezone. strptime is a function that parses time depending on a format you give it.
Note that this does not take into account an empty time string.
Handling timezones properly can get fairly involved since the standard library barely supports them (and recommends using the third-party pytz module) to do so). This would be especially true if you need it
So, one "quick and dirty" way to deal with them would be to just ignore that information and add the current day, month, and year to any timestamps encountered that don't include that. The code below demonstrates how to do that.
from datetime import datetime
scrapped = '''
17:22 ET
02:41 ET
06:14 ET
Mar 10, 2021, 16:05 ET
Mar 08, 2021, 08:00 ET
Feb 26, 2021, 11:23 ET
'''
def get_datetime(string):
string = string[:-3] # Remove timezone.
try:
r = datetime.strptime(string, "%b %d, %Y, %H:%M")
except ValueError:
try:
today = datetime.today()
daytime = datetime.strptime(string, "%H:%M")
r = today.replace(hour=daytime.hour, minute=daytime.minute, second=0, microsecond=0)
except ValueError:
r = None
return r
for line in scrapped.splitlines():
if line:
r = get_datetime(line)
print(f'{line=}, {r=}')
"I can't order my database" - to be able to do so, you'll either have to convert the strings to datetime objects or to an ordered format (low to high resolution, so year-month-day- etc.) which would allow you to sort strings correctly.
"I have a hard time to understand how regex works" - while you can use regular expressions here to somehow parse and modify the strings you have, you don't need to.
#1 If you want a convenient option that leaves you with datetime objects, here's one using dateutil:
import dateutil
times = ["17:22 ET", "02:41 ET", "06:14 ET",
"Mar 10, 2021, 16:05 ET", "Mar 08, 2021, 08:00 ET", "Feb 26, 2021, 11:23 ET"]
tzmapping = {'ET': dateutil.tz.gettz('US/Eastern')}
for t in times:
print(f"{t:>22} -> {dateutil.parser.parse(t, tzinfos=tzmapping)}")
17:22 ET -> 2021-03-13 17:22:00-05:00
02:41 ET -> 2021-03-13 02:41:00-05:00
06:14 ET -> 2021-03-13 06:14:00-05:00
Mar 10, 2021, 16:05 ET -> 2021-03-10 16:05:00-05:00
Mar 08, 2021, 08:00 ET -> 2021-03-08 08:00:00-05:00
Feb 26, 2021, 11:23 ET -> 2021-02-26 11:23:00-05:00
Note that you can easily tell dateutil's parser to use a certain time zone (e.g. to convert 'ET' to US/Eastern) and it also automatically adds today's date if the date is not present in the input.
#2 If you want to do more of the parsing yourself (probably more efficient), you can do so by extracting the time zone first, then parsing the rest and adding a date where needed:
from datetime import datetime
from zoneinfo import ZoneInfo # Python < 3.9: you can use backports.zoneinfo
# add more if you not only have ET...
tzmapping = {'ET': ZoneInfo('US/Eastern')}
# get tuples of the input string with tz stripped off and timezone object
times_zones = [(t[:t.rfind(' ')], tzmapping[t.split(' ')[-1]]) for t in times]
# parse to datetime
dt = []
for t, z in times_zones:
if len(t)>5: # time and date...
dt.append(datetime.strptime(t, '%b %d, %Y, %H:%M').replace(tzinfo=z))
else: # time only...
dt.append(datetime.combine(datetime.now(z).date(),
datetime.strptime(t, '%H:%M').time()).replace(tzinfo=z))
for t, dtobj in zip(times, dt):
print(f"{t:>22} -> {dtobj}")

Date/Time formatting

I'm creating a python script that will display busy, no-answer and failed calls for a specific date but I'm stuck on the formatting of the date that's displayed. The start_time and end_time "variables" from Twilio print something like this: "Mon, 25 Jul 2016 16:03:53 +0000". I want to get rid of the day name and the comma since I'm saving the results into a csv file (script_name.py > some_file.csv) and the comma after the day name kind of screws up the csv structure.
In the settings.py file the time_zone variable is set to the right one (America/Chicago) and the USE_TZ variable is set to true. But anyway the output is still in UTC.
I don't know anything about Python and the things I've tried to parse call.start_time to a datetime have failed . . . I would know how to do it if it was a given value like start_time = '2016-07-26', but I don't know how to do it when the value comes from for call in client.calls.list . . .
Any guidance will be greatly appreciated!
Thanks!
from twilio.rest import TwilioRestClient
from datetime import datetime
from pytz import timezone
from dateutil import tz
# To find these visit https://www.twilio.com/user/account
account_sid = "**********************************"
auth_token = "**********************************"
client = TwilioRestClient(account_sid, auth_token)
for call in client.calls.list(
start_time="2016-07-25",
end_time="2016-07-25",
status='failed',
):
print(datetime.datetime.strptime(call.start_time, "%Y-%m-%d %H:%M:%S"))
The code I've provided does simple date and time format.
from datetime import datetime
from time import sleep
print('The Time is shown below!')
while True:
time = str(datetime.now())
time = list(time)
for i in range(10):
time.pop(len(time)-1)
time = ('').join(time)
time = time.split()
date = time[0]
time = time[1]
print('Time: '+time+', Date: '+date, end='\r')
sleep(1)
However if you looking just to format "Mon, 25 Jul 2016 16:03:53 +0000" as you said and just remove the day consider something like this:
day = "Mon, 25 Jul 2016 16:03:53 +0000"
# Convert to an array
day = list(day)
# Remove first 5 characters
for i in range(5):
day.pop(0)
day = ('').join(day)
print(day)
# You can use if statements to determine which day it is to decide how many characters to remove.
>>> "25 Jul 2016 16:03:53 +0000"
The format you need to parse is dictated by the timestamp provided by Twillo. You will likely need the following format string to properly parse the timestamp:
print(datetime.datetime.strptime(call.start_time, "%a, %d %b %Y %H:%M:%S %z"))
A great guide for the formatting string is http://strftime.org/.
Another good library for lazily converting dates from strings is the python-dateutil library found at https://dateutil.readthedocs.io/.

Parse date/time from a string

I'm using Python 3.3. I'm getting an email from an IMAP server, then converting it to an instance of an email from the standard email library.
I do this:
message.get("date")
Which gives me this for example:
Wed, 23 Jan 2011 12:03:11 -0700
I want to convert this to something I can put into time.strftime() so I can format it nicely. I want the result in local time, not UTC.
There are so many functions, deprecated approaches and side cases, not sure what is the modern route to take?
Something like this?
>>> import time
>>> s = "Wed, 23 Jan 2011 12:03:11 -0700"
>>> newtime = time.strptime(s, '%a, %d %b %Y %H:%M:%S -0700')
>>> print(time.strftime('Two years ago was %Y', newtime))
Two years ago was 2011 # Or whatever output you wish to receive.
I use python-dateutil for parsing datetime strings. Function parse from this library is very handy for this kind of task
Do this:
import email, email.utils, datetime, time
def dtFormat(s):
dt = email.utils.parsedate_tz(s)
dt = email.utils.mktime_tz(dt)
dt = datetime.datetime.fromtimestamp(dt)
dt = dt.timetuple()
return dt
then this:
s = message.get("date") # e.g. "Wed, 23 Jan 2011 12:03:11 -0700"
print(time.strftime("%Y-%m-%d-%H-%M-%S", dtFormat(s)))
gives this:
2011-01-23-21-03-11

Python date string mm/dd/yyyy to datetime

I have to write a program where I take stocks from yahoo finance and print out certain information for the site. One of the pieces of data is the date. I need to take a date such as 3/21/2012 and converter to the following format: Mar 21, 2012.
Here is my code for the entire project.
def getStockData(company="GOOG"):
baseurl ="http://quote.yahoo.com/d/quotes.csv?s={0}&f=sl1d1t1c1ohgvj1pp2owern&e=.csv"
url = baseurl.format(company)
conn = u.urlopen(url)
content = conn.readlines()
data = content[0].decode("utf-8")
data = data.split(",")
date = data[2][1:-1]
date_new = datetime.strptime(date, "%m/%d/%Y").strftime("%B[0:3] %d, %Y")
print("The last trade for",company, "was", data[1],"and the change was", data[4],"on", date_new)
company = input("What company would you like to look up?")
getStockData(company)
co = ["VOD.L", "AAPL", "YHOO", "S", "T"]
for company in co:
getStockData(company)
You should really specify what about your code is not working (i.e., what output are you getting that you don't expect? What error message are you getting, if any?). However, I suspect your problem is with this part:
strftime('%B[0:3] %d, %Y')
Since Python won't do what you think with that attempt to slice '%B'. You should instead use '%b', which as noted in the documentation for strftime(), corresponds to the locale-abbreviated month name.
EDIT
Here is a fully functional script based on what you posted above with my suggested modifications:
import urllib2 as u
from datetime import datetime
def getStockData(company="GOOG"):
baseurl ="http://quote.yahoo.com/d/quotes.csv?s={0}&f=sl1d1t1c1ohgvj1pp2owern&e=.csv"
url = baseurl.format(company)
conn = u.urlopen(url)
content = conn.readlines()
data = content[0].decode("utf-8")
data = data.split(",")
date = data[2][1:-1]
date_new = datetime.strptime(date, "%m/%d/%Y").strftime("%b %d, %Y")
print("The last trade for",company, "was", data[1],"and the change was", data[4],"on", date_new)
for company in ["VOD.L", "AAPL", "YHOO", "S", "T"]:
getStockData(company)
The output of this script is:
The last trade for VOD.L was 170.00 and the change was -1.05 on Mar 06, 2012
The last trade for AAPL was 530.26 and the change was -2.90 on Mar 06, 2012
The last trade for YHOO was 14.415 and the change was -0.205 on Mar 06, 2012
The last trade for S was 2.39 and the change was -0.04 on Mar 06, 2012
The last trade for T was 30.725 and the change was -0.265 on Mar 06, 2012
For what it's worth, I'm running this on Python 2.7.1. I also had the line from __future__ import print_function to make this compatible with the Python3 print function you appear to be using.
Check out Dateutil. You can use it to parse a string into python datetime object and then print that object using strftime.
I've since come to a conclusion that auto detection of datetime value is not always a good idea. It's much better to use strptime and specify what format you want.

Categories

Resources