Use of the individual parts with Tkcalendar - python

My problem is that I want to use the individual parts from the calendar. That means, I would like to be able to use dd, mm and y individually. I decided to use the German spelling: dd.mm.y.
datepicker = Calendar(root, selectmode="day", year=2021, month=1, day=1, date_pattern='ddmmy')
In order to only filter out the day, I have so far proceeded as follows:
birthday = str(datepicker.get_date())
bday = int(str(birthday)[:-6])
But that's just a stopgap. How can I sort the day, month and year properly so that I can use the values ​​individually?
Thank you in advance for your answers, Best regards.

Related

Adding a range of dates as one holiday rule, instead of just a single date, in Pandas.tseries AbstractHolidayCalendar?

I'm working on a Python script to offset a given start date with X number of business days according to a custom holiday calendar. Pandas.tseries seems to be a good choice.
When building my generic holiday calendar, I have come across examples on adding a single date to the holiday rules.
Example:
import pandas as pd
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, Easter
from pandas.tseries.offsets import Day
class myCalendar(AbstractHolidayCalendar):
rules = [
Holiday('Off-day during Easter', month=1, day=1, offset=[Easter(), Day(-2)]),
Holiday('Christmas Day', month=12, day=25)
]
When using a function like this:
def offset_date(start, offset):
return start + pd.offsets.CustomBusinessDay(n=offset, calendar=myCalendar())
The dates within the rules will be skipped as expected.
But I now want to add 3 full weeks, 21 days to the rule set, with a given start-offset, instead of writing 21 rule lines to achieve the same thing?
I wonder if you guys know if it's possible to create a one-liner that adds 21 days to the rule set?
Here is one way to do it with a list comprehension, which keeps it short and readable:
class myCalendar(AbstractHolidayCalendar):
rules = [
Holiday("Off-day during Easter", month=1, day=1, offset=[Easter(), Day(-2)]),
Holiday("Christmas Day", month=12, day=25),
Holiday("Christmas Day", month=12, day=25),
] + [Holiday("Ski days", month=2, day=x) for x in range(1, 22)]
Here, a 21 days-off period starting February, 1st is added to the set of rules.
So that:
print(offset_date(pd.to_datetime("2023-01-31"), 1))
# 2023-02-22 00:00:00 as expected

Python - Calendar / Date Library for Arithmetic Date Operations

This is for Python:
I need a library that is able to do arithmetic operations on dates while taking into account the duration of a month and or year.
For example, say I add a value of "1 day" to 3/31/2020, the result of should return:
1 + 3/31/2020 = 4/1/2020.
I also would need to be able to convert this to datetime format, and extract day, year and month.
Does a library like this exist?
import datetime
tday = datetime.date.today() # create today
print("Today:", tday)
""" create one week time duration """
oneWeek = datetime.timedelta(days=7)
""" create 1 day and 1440 minutes of time duraiton """
eightDays = datetime.timedelta(days=7, minutes=1440)
print("A week later than today:", tday + oneWeek) # print today +7 days
And the output to this code snippet is:
Today: 2020-03-25
A week later than today: 2020-04-01
>>>
As you see, it takes month overflows into account and turns March to April. datetime module has lots of things, I don't know all its attributes well and haven't used for a long time. However, I believe you can find nice documentation or tutorials on the web.
You definitely can create any specific date(there should be some constraints though) instead of today by supplying day, month and year info. I just don't remember how to do it.

How to get weekly data from beginning of month using date_range of Pandas?

I want to divide the date range starting from 1st July to 1st August, in weekly basis. But I want it to start from 1st day of the month.
I am using pd.date_range('2015-07-01', '2015-08-01', freq='W' )
But I am getting
DatetimeIndex(['2015-07-05', '2015-07-12', '2015-07-19', '2015-07-26'], dtype='datetime64[ns]', freq='W-SUN')
I want this to be done from 2015-07-01. I know I can use timedelta or find the start day of the month and use W-WED. But is there any other shortcut to do the same using date_range of pandas?
I have checked http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases, but could not come up with anything useful.
Any help is appreciated. Thanks in advance.
I would suggest using the frequency of 7 days instead of a week, so that you will start at the first day of the month rather than the first day of the week
pd.date_range('2015-07-01', '2015-08-01', freq='7d')
EDIT
To clarify, it is not strictly the first day of the month, but the first day you provide. But in your example those two are the same

Get all objects created on same day till now

I have a model which contains created_at DateTimeField. I want to filter the model to get all object created on this day, month till now.
For Example : Date = 21/06/2016
Now I want to get all objects created on 21/06 till now irrespective of year.
Edit:
To be precise, I have model which stores Date of Birth of Users. I want to get all the users who were born on this day.
I tried using the __range, __gte, __month & __day. This things did not work.
Thanks for your comments and answers. I have used this answer to solve the problem. I have removed the timedelta(days) from the code.
An example of filtering outside of the queryset.
Get the date u want and remove unwanted results
from datetime import datetime, timedelta
twodaysago = str(datetime.strptime(str(datetime.now().date()-timedelta(days=2)), '%Y-%m-%d')).split()[0]
now do your query and filter it like this, you may also do it in the query filter, but if u need some extra manipulations do it in your code
date_filtered = [x for x in query\
if datetime.strptime(
x.get('created_at ', ''),
'%Y-%m-%d') > twodaysago
]
Not sure if I understand the problem correctly, but try this:
before = datetime.date(2016, 06, 21)
today = datetime.date.today()
MyModel.objects.filter(
created_at__month__range=(before.month, today.month),
created_at__day__range=(before.day, today.day)
)
From what I can understand from your question, you want to get all objects with the same date as today, irrespective of the year.
I would use something like this. See if this helps.
from datetime import datetime
today = datetime.now()
OneModel.objects.filter(created_at__day = today.day.__str__(),
created_at__month = today.month.__str__())
For more see this link: How can I filter a date of a DateTimeField in Django?

How format date into BYDAY (iCalendar specification)

I need to create RRULE string with BYDAY parameter, from my date value.
Is there any natural way to do it?
I wrote this utility code for this purpose:
import calendar
fweek_fday, mdays = calendar.monthrange(date.year, date.month)
# Get weekday of first day and total days in current month
mweeks = ((mdays+fweek_fday-1)//7)+1
# Get total weeks in current month
mday, wday = date.day, date.weekday()
# Get current day of month and current day as day of a week
week_days = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']
week = ((mday+fweek_fday-1)//7)+(1 if wday>=fweek_fday else 0)
# Get current week no
week = -1 if week == mweeks else week
wday = week_days[wday]
output = "BYDAY=%d%s" % (week, wday)
as you said in comment there is no module that I've found yet to make a rule out from a set of constraints.
should it be possible for you, you may consider the RDATE rather than going for the BYDAY.
another option for you would be:
import datetime
(y,w,d ) = date.isocalendar()
#w will be the iso week number, d the day of week
(y2,wb,d2) = datetime.datetime(date.year,date.month,1).isocalendar()
wkcount = 1 if d>=d2 else 0
# you need to account whether your date is a weekday after the one of the first of the month or not
print "BYDAY=%d%s"%(w-wb+wkcount,date.strftime("%a").upper()[0:2])
however be careful should your rule also include WKST
The natural way to do this—unless you're writing an iCalendar library—is to use a pre-existing iCalendar library that has methods for dealing with recurrence rules, etc., as objects instead of doing string parsing and generation.
It's possible that there is no such library, in which case you're out of luck. But the first thing you should do is look for one.
A quick search at PyPI turns up many possibilities. The first one, iCalendar, is described as "a parser/generator of iCalendar files", which sounds like a good candidate. Skim the docs, or just pip install it and play with it, to see if it can do what you want.

Categories