I'm in calendar hell, and I'm hoping there exists a Python module out there that does what I want.
I'm making a Python web app that deals with subscriptions. It's conceptually similar to a cell phone plan: You start your subscription on a certain date (say 1.13.2011), and for every billing month you have a bunch of "sessions" (phone calls), that you would be charged for.
We need to:
Know under which billing month each session falls.
Know the start time and end time of each billing month.
For example, if you signed up on 1.13.2011, and made a phone call on 1.20.2011, it would count on your first billing month. Same for a phone call on 2.10.2011. But if you were to make a phone call on 2.15.2011, it will count on your second billing month.
Regarding start and end dates: If today is 2.15.2011, then the start date of the current month is 2.13.2011 and its end date is 3.13.2011.
You may be thinking this is not so complicated, but then you have to consider that months have different lengths. The rule for handling this is that if your subscription started on the 30th of whatever month, its cutoff dates on each month would be min(30, n_days_in_that_month). This goes for 29, 30 and 31 as well.
I tried coding this, but it got too complex. What I'm looking for is a ready-made, existing module that does these things.
For the love of God don't post an answer with a sketch of an implementation! This is useless for me. I appreciate your intentions, but in calendar hell, sketches of implementations do not help. I already have a sketch of an implementation, and debugging yours will take just as long as debugging mine.
I am only interested in using an existing module that handles such calendar tasks. Do you know one?
http://labix.org/python-dateutil
Ram's edit: The dateutil.rrule.rrule class is the one that did exactly what I wanted.
Regarding start and end dates: If today is 2.15.2011, then the start date of the current month is 2.13.2011 and its end date is 3.13.2011.
You may be thinking this is not so complicated, but then you have to consider that months have different lengths. The rule for handling this is that if your subscription started on the 30th of whatever month, its cutoff dates on each month would be min(30, n_days_in_that_month). This goes for 29, 30 and 31 as well.
Its still pretty basic. Use datetime module to store datetimes, so you can easily parse out the day (e.g., if dt is a date then dt.day). A billing cycle starts on say the 29th (toughest type of case). Let billing_cycle_day=29. A billable event occurs on say the event_day=10, event_month=5. Then since event_day < billing_cycle_day you bill to event_month's bill. Otherwise you bill to the next months bill (remembering that if month=12; you have to increment the year).
So now the billing cycle will always be from the 29th to the 28th in the next month. The complication arises if say a date like 2/29/2011 doesn't exist. E.g., a billing cycle start_date should be 2/29/2011 (but it doesn't exist); in this case you just make it the first on the next month.
billing_cycle_day = 29
year, month = 2011, 2
import datetime
def create_date_or_first_of_next_month(year, month, day):
try:
return datetime.date(year, month, day)
except ValueError:
year_n, month_n = (year, month+1) if month != 12 else (year+1, 1)
return datetime.date(year_n, month_n, 1)
This problem is not as hard as you think. All you have to do is write a function that given a starting day (like 13 or 30) it returns two date objects which are the beginning and end of the current fiscal month. You have already sketched out all the details in your question. Best to include an optional todayis parameter to the function so that you specify what day to use as a reference for today. For instance, if today is the 15th of October 2011, and you specify 13, the function would assume that you mean the 13th of October 2011. But if you want to rerun June data, you would specify todayis=date(2011,06,13)
The return values (start and end) allow you to pinpoint dates that belong in this fiscal month. But if the date is before the start date and less than 29 days before the start date, then you can also pinpoint in the previous fiscal month. The same goes for the next fiscal month. This is useful because there will be a lot of situations where you process data after a few days, so you will have a mix of two fiscal months to process.
Related
How to convert relative time that are expressed in humanised words like "-100 days, -6 months, -1 year, +1 year" into YYYY-MM-DD format?
I am posting my answer to help others if they need the same thing.
What I am doing is developing a CLI application, part of the process is user will input starting date and end date, but I want the user to use a relative time like the following:
(for the sake of a an example date, October 23, 2017 would be the current date)
$ cliapp.py --start_time="10 days ago" --end_time="yesterday"
10 days ago is "2017-10-13"
yesterday is "2017-10-22"
$ cliapp.py --start_time="tomorrow"
tomorrow is "2017-10-24"
to accomplish this I found dateparser module and works exactly what I need.
here is the link to it: https://dateparser.readthedocs.io/en/latest/
If you have other solution, feel free to put on comment. :)
I am writing a python code to change the date in linux system to today-1 (dynamically). I tried various combinations but, yet I am not able to succeed. I searched and I found a close proximity to my scenario in this question .
I am able to change the date if I execute the command with static value say:
date --set="$(date +'2013%m%d %H:%M')"
However, I don't want to specify hardcoded value for year i.e., 2013. Instead i want to specify something like "%y-1" i.e.,
date --set="$(date +'%y-1%m%d %H:%M')"
If I run the above command I get the following error
[root#ramesh ~]$ date --set="$(date +'%y-1%m%d %H:%M')"
date: invalid date `14-11016 13:05'
Thanks for your answer. I did not try your approach though, reason being it has to be once again dealt with formatting issues when working with arithmetic operations incase if you want to.
So, I figured out a much simpler and generalized approach
Fetch the previous_year value with this command
date --date='1 years ago'
This gives the previous year date. Now this can be used in the python program to update the system in the following way
"date --set=$(date +'%%y%%m%s %%H:%%M') % previous_year"
This method has few advantages like
I can apply this method for day and month as well like "1 days ago", "1 month ago" along with +%d, +%m, +%y values.
e.g., date --date='1 years ago' +%y
I don't have to worry about the date and month arithmetic calculation logics
date will interpret the %y-1 literally has you showed. What you need is to retrieve the current year, subtract 1 and use this value as the new year.
To get the current_year - 1 you can do:
previous_year=$((`date +'%y'`-1))
echo $previous_year
>>> 13
Now you just need to use this variable to set the new date.
So say I have a date, for example, 08/27/2014, and I want to find out what week it is (like if it's the first week, or the second week, or the third week. In this case, it would the fourth week), assuming that a new week starts on Sunday. Does anyone have any ideas as to how to do this?
You could do it simply using the isocalendar function (https://docs.python.org/2/library/datetime.html#datetime.date.isocalendar).
If your week starts on Wed instead of Mon, you just need a little logic get the correct number (-1 if you're on a Mon or Tue).
EDIT : saw you edit after posting, but still works, just need a different logic (+1 if you're on a Sunday)
Week as a service:
import urllib2
import re
def week():
response = urllib2.urlopen('http://vecka.nu')
html = response.read()
match = re.search('datetime="(\d+)-W(\d+)"', html, re.M)
if match:
return int(match.group(2))
Just kidding! Although, this is how I usually check which week it is ;)
The problem. In my Django application, users create tasks for scheduled execution. The users are quite non-technical, and it would be great if they can write conventional human-readable expressions to define when to execute certain task, such as:
every monday
every fri, wed
daily
1, 14, 20 of each month
every fri; every end of month
This is inspired by Todoist. For now, only dates are necessary; no times. I've spent a couple of hours googling for a library to do that, but with no luck. I am expecting a function, say, in_range(expression, date), such that:
>>> in_range('every monday, wednesday', date(2014, 4, 28))
True
>>> in_range('every end of month', date(2014, 5, 12))
False
>>> in_range('every millenium', date(2014, 5, 8))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: unknown token "millenium".
Variants. That's what I've looked through.
Standard datetime library does date parsing, but not date range parsing as per above.
Python-dateutil - supports recurring dates via rrule, very functional, but still does not support parsing.
Python-crontab and Python-croniter accept standard Unix crontab syntax (and allow to specify weekdays, etc), but still such syntax is a way too technical and I'd like to avoid it if possible.
Arrow and Parsedatetime do not support the feature.
So, is there a Python code snippet, or a library that I missed, to do the thing? If not, I'm going to write the parser myself. Would like to release it in open source if it appears to be not too bad.
Recurrent is a library that will do natural language date parsing with support for recurring dates. It doesn't match the API you provided, but allows you to create rules that can be used with Python's datetime library.
From their Github page:
Natural language parsing of dates and recurring events
Examples
Date times
next tuesday
tomorrow
in an hour
Recurring events
on weekdays
every fourth of the month from jan 1 2010 to dec 25th 2020
each thurs until next month
once a year on the fourth thursday in november
tuesdays and thursdays at 3:15
Messy strings
Please schedule the meeting for every other tuesday at noon
Set an alarm for next tuesday at 11pm
I'm trying to get week number with this simple script on python.
import datetime
t = datetime.date(2013,8,18)
print t.isocalendar()[1]
It returns 33 for ISO format, but for the US calendar it should be 34.
How can I get this week number for US format?
I ran into the same problem. The ISO calendar is a great concept and may have some really good uses, but for business in the U.S. it just doesn't work well. I can't write an application that could potentially report days in the end of December as being in the next calendar year. The solution that works for me is:
from datetime import *
today = datetime.today()
print today.strftime("%U")
This will return the correct U.S. week number. The only caveat with this is that all days in a new year preceding the first Sunday are considered to be in week 0. Credit to Chaggster who gave a similar answer here: How to get week number in Python?