Windows/c++- Date subtraction (week/month/year) using boost - c++

How can I subtract a length-of-time from a boost gregorian date?
Let's say I construct a date as follows:
boost::gregorian::date Today(2012, 02, 13);
I would like to do Today - N weeks from Today - N Months from Today - N years and get a valid date after subtraction.

To get one week prior to today, just use
today - weeks(1)
To get the previous month you can do.
today - months(1)
But the same day of the previous month might not exist. For example, if today were March 30, there is no February 30th, but boost will "snap" to the end of the month in the case of March 30 - months(1) and give you Feb 28th (or Feb 29th in leap years).
The classes are all part of the boost::gregorian namespace.

I think you want the Date Iterators, described here: http://www.boost.org/doc/html/date_time/gregorian.html#date_time.gregorian.date_iterators

Related

How to use the EOMONTH DAX to get the last date of iso month?

ISO months start and end at various dates. Not necessarily 1st/31st.
How to use the EOMONTH DAX to get the last date of iso month?
DAX can handle everything that follows a certain logic.
In your example Jan is week 1-4, Feb is 5-8 and Mar is probably 9-12. But April has to be 13-17 (5 weeks), which ends on Apr-30? After all 52 weeks divided by 4 is 13 not 12. So some "months" have to have 5 weeks instead of 4.
I'm afraid EOMONTH is not familiar with your calendar preferences as long as they not defined in any ISO regulation.
However, as mentioned above, it's possible to find the first Monday and last Sunday within a month using DAX, but not with the EOMONTH function.

how to subtract months giving a specific criteria

hi I have a rolling last 12 months in excel but I would like to exclude specific months so that way it will skip does months on the report. but the formula that I have wont work for every month. the months that I would like to skip is march 2020, april 2020, may 2020, june 2020. I have a drop list I which it will subtract the last 12 months.
enter image description here

Is Python's `datetime.strptime` buggy for week numbers on years with 53 weeks?

I am using Python 2.7 and I am trying to convert week numbers to actual dates.
I was using this solution which worked fine for basics tests, but I found a case where there seems to be a break: in years with 53 weeks.
The example below shows the issue:
datetime.datetime.strptime( "2015-W53-0", "%Y-W%W-%w").strftime("%Y-W%W-%w")
This returns '2016-W01-0', which I think does not make sense.
Is this a known issue, is there a known workaround?
Note that:
datetime.datetime.strptime( "2015-W53-0", "%Y-W%W-%w").isocalendar()
yields (2016, 1, 7), so it's probably strptime which is guessing wrong here.
I actually found a good library called isoweek which works as I expect.
#This is Week 53 of 2015 according to ISO
week = isoweek.Week.withdate( datetime.date( 2016,1,1) )
monday = week.monday()
print monday.isocalendar()
#Prints (2015, 53, 1), which is correct.
I think this is the simplest way to handle these issues.
%W is the week of the year, with the first week containing a Monday counted as week 1.
The first day of the week is Monday, which has a %w day number of 1. The last day of the week is Sunday, which has a day number of 0. So in this reckoning %Y-%W-0 always comes six days later than %Y-%W-1.
By this reckoning, the 52nd week starts on the 28th of December, and doesn't contain a Sunday. So 2015-W52-0 is interpreted as the first Sunday in 2016 (Jan 3rd). Since this is before the first Monday, 2015-W52-0 is canonically 2016-W00-0 and 2015-W53-0 is the second Sunday in 2016, which is at the end of week 1, ie Jan 10th, or 2016-W01-0.
So the methods are working as documented.
The %W and %w don't implement the ISO week date algorithm, which sets the first week as that containing a Thursday. 2015 contains a 53rd week by the ISO method, but not by the %W method. If you want to use ISO weekdates you should use isocalendar.
Since the standard implementation of python is just calling the C library functions, you may be able to use %G (The year) and %V (the week number) according to the ISO algorithm. But these can't be guaranteed to be portable.

Finding the day of the thirteenth of every month over a period of years

I am currently trying to solve some problems from the USACO training website in preparation for an unrelated C++ programming competition.
However, I am stuck on this problem:
Does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.
The number N is provided in an input file and the output is to be a file with seven numbers in it, each representing the number of 13th's falling on a particular day of the week.
I was wondering how you guys would approach this problem. I am not looking for code or anything since that would just defeat the purpose of me doing this, instead just a starting point or an algorithm would be helpful.
So far the only thing I could think of is using the Doomsday Algorithm, however I am unsure about how I would implement that in code.
Any help would be greatly appreciated.
As Denny says, N is so small that you can easily iterate through the months using a table of days-in-a-month and a simple is-a-leap-year predicate to handle February. Just find out what day the 13th of Jan was in 1900 and then add up the elapsed days until 13th Feb, then 13th March etc.. Use a % operator to wrap the # of elapsed days back into a day-of-week value.
N is less than 400? well you just need to go over 365.25*400=146100 days at max. sounds easy to enumerate all of them, convert dates into year/month/date (with your favorite date conversion routine), testing for day of week is trivial.
I would precalculate the table though.
Just use brute force. Like this pseudocode example:
from datetime import date
day_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday']
counts = [0] * 7
for year in range(1900, 2300):
for month in range(1, 13):
counts[date(year, month, 13).weekday()] += 1
for day, count in zip(day_names, counts):
print('%s: %d' % (day, count))
The "hard" part is calculating the day of the week a date falls on. In C(++), you can use the mktime and localtime library functions if you know that your platform handles a large enough date range.

Calculate the first day of a calendar week

I need to calculate the date of the first day in a calendar week, e.g.
Week 1 in 2009 -> Mon, 29.12.2008 (!)
Week 44 in 2009 -> Mon, 26.10.2009
I have some ugly code for this, but I would prefer a nice C++ lib.
Any suggestions?
what about boost::gregorian::date with its algorithms ?