Determine the amount of days between events excluding weekends in python - python

I am trying to determine the amount of days between updates on a ticket in zendesk, excluding weekends. I have been able to determine the difference between updates, but it doesnt take into account weekends. I want to determine if the user has updated their ticket within 3 days, excluding weekends, then close the ticket if it has been over 3 days. This is what I have:
updated_time = updated_time[0:10:1]
time_now = str(datetime.datetime.now())
time_now = time_now[0:10:1]
updated_time = dateutil.parser.parse(updated_time)
time_now = dateutil.parser.parse(time_now)
date_to_close = datetime.timedelta(days=3)
please_close = time_now - updated_time
My problem is because it doesnt distinguish between weekdays and weekends, it will automatically close the ticket on Monday if no one has responded over the weekend, which I would not like to count. I've been looking at the isoweekday() but its not really giving me what I need. I was thinking about trying to assign the ticket to a variable and assigning it a number, then increase that number by days without an update, that way it would not count the specific date but rather the number of days in between, but not sure how to do this or if it would even work.
Does anyone have an idea how to count the difference between days while excluding weekends? Is this possible?

If you already know how many days (of any kind), that value must be equal to 7w+x (that is, w full weeks and x days of the remaining partial week). You know that there are 5 weekdays in every full week; that just leaves figuring out how many of the remaining x days are weekdays, which you can determine from how many days until the first weekend and comparing that to x.

Ok, after a lot of digging and reaching out to another python programmer I know, he pointed me to the numpy function busday_count(). This does the calculation that I need. The reference is: Python - Exclude weekends between two Dates
Thanks very much for everyone's answers!

Related

How can I get all the dates of current week?

I have a use case where I need to count the server failures for current week. I am counting by reading a file where all the dates and failures are given. But I need to Calculate weekly failures, So I thought of to get all the dates in current week and compare that with dates in file and hence count the failures. So the question is how can I get all the dates of current week ? Also, how can i check if any date comes in that week?
Can anyone please help ?
Using pandas:
df.loc[df["dates"].dt.week == week_number]
This simply gets all the rows where week is equal to the specified week (you can find out that week by trying a dummy value and using .dt.week).
print(date.today())
for x in range(7):
print(date.today() + timedelta(days=x))

How can a Python script determine when the current time is between two given daily times?

I'm writing a Python script that is to run continuously, performing some of its functionality only if it determines that the current time is between two daily times. Specifically, the script is given two command line arguments that are both of the form HHMM, where HH is the daily starting hour and MM is minutes and UTC is assumed.
So, for example, the script might be told to run through the day (0900, 1700), or it might be told to run through the night (2200, 0700).
Given such arguments -- that specify times that can be in the current day and the following day -- is there some straightforward way (perhaps using the datetime module) for the script to know if the current time is between these two times?
Construct two datetime objects from your arguments + today's date.
If the "later" one is less then the earlier one then add one day to the later time. It should now be greater than the early time.
Then compare the current time to see if it is greater than the early time and less than the later time.
start and end can be represented as tuples:
start = (9, 0)
end = (17,0)
now = datetime.datetime.utcnow().timetuple()[3:5] # gives you (hour,min)
print start <= now <= end

Python: creating list of timestamps by minute

I am trying to figure out what the best way to create a list of timestamps in Python is, where the values for the items in the list increment by one minute. The timestamps would be by minute, and would be for the previous 24 hours. I need to create timestamps of the format "MM/dd/yyy HH:mm:ss" or to at least contain all of those measures. The timestamps will be an axis for a graph of data that I am collecting.
Calculating the times alone isn't too bad, as I could just get the current time, convert it to seconds, and change the value by one minute very easily. However, I am kind of stuck on figuring out the date aspect of it without having to do a lot of checking, which doesn't feel very Pythonic.
Is there an easier way to do this? For example, in JavaScript, you can get a Date() object, and simply subtract one minute from the value and JS will take care of figuring out if any of the other fields need to change and how they need to change.
datetime is the way to go, you might want to check out This Blog.
import datetime
import time
now = datetime.datetime.now()
print now
print now.ctime()
print now.isoformat()
print now.strftime("%Y%m%dT%H%M%S")
This would output
2003-08-05 21:36:11.590000
Tue Aug 5 21:36:11 2003
2003-08-05T21:36:11.590000
20030805T213611
You can also do subtraction with datetime and timedelta objects
now = datetime.datetime.now()
minute = timedelta(days=0,seconds=60,microseconds=0)
print now-minute
would output
2015-07-06 10:12:02.349574
You are looking for datetime and timedelta objects. See the docs.

Python: How to extract time date specific information from text/nltk_contrib timex.py bug

I am new to python. I am looking for ways to extract/tag the date & time specific information from text
e.g.
1.I will meet you tomorrow
2. I had sent it two weeks back
3. Waiting for you last half an hour
I had found timex from nltk_contrib, however found couple of problems with it
https://code.google.com/p/nltk/source/browse/trunk/nltk_contrib/nltk_contrib/timex.py
b. Not sure of the Date data type passed to ground(tagged_text, base_date)
c. It deals only with date i.e. granularity at day level. Cant find expression like next one hour etc.
Thank you for your help
b) The data type that you need to pass to ground(tagged_text, base_date) is an instance of the datetime.date class which you'd initialize using something like:
from datetime import date
base_date = date.today()

Calculate next scheduled time based on cron spec

What's an efficient way to calculate the next run time of an event given the current time and a cron spec?
I'm looking for something other than "loop through every minute checking if it matches spec".
Examples of specs might be:
Every month, on the 1st and 15 at 15:01
At 10,20,30,40,50 mins past the hour every hour
Python code would be lovely but psuedo code or high level description would also be appreciated.
[Update] Assume the spec is already parsed and is in some reasonable format.
Just looking at it, I think you need to:
parse the chron spec to five arrays containing acceptable values for each field;
parse 'now' to a value for each field;
in order of minute, hour, {day-of-month OR day-of-week}, month-of year: find the lowest array value that matches or exceeds the current value, correcting for carry.
I don't know how to handle day-of-week and day-of-month simultaneously; I am sure there is a way, but on the other hand I don't think I've ever seen a spec that actually specified both. I think it would be sufficient to write a handler for either and throw an error if you receive both.
Edit: apparently if day-of-week and day-of-month are both specified, it is supposed to fire on both - ie if the rule is '15th, Wednesday' it will fire on every 15th and every Wednesday.
The croniter package does what you want:
import croniter
import datetime
now = datetime.datetime.now()
sched = '1 15 1,15 * *' # at 3:01pm on the 1st and 15th of every month
cron = croniter.croniter(sched, now)
for i in range(4):
nextdate = cron.get_next(datetime.datetime)
print nextdate
prints
2011-01-15 15:01:00
2011-02-01 15:01:00
2011-02-15 15:01:00
2011-03-01 15:01:00
although it would be nice if it were written as an actual iterator. Maybe I've got my next project ;-)

Categories

Resources