How to increment Date objects in C++ - c++

I have an assignees that I've been working on and I'm stuck on the last function.
use the function void Increment(int numDays = 1)
This function should move the date forward by the number of calendar days given in the argument. Default value on the parameter is 1 day. Examples:
Date d1(10, 31, 1998); // Oct 31, 1998
Date d2(6, 29, 1950); // June 29, 1950
d1.Increment(); // d1 is now Nov 1, 1998
d2.Increment(5); // d2 is now July 4, 1950
I don not understand how to do this.
void Date::Increment(int numDays = 1)
I'm stuck, I know how to tell the function to increment, by the ++ operator but i get confuse when I have to get the function to increment the last day of the month to the the fist, or to end at the last date of that month for example. Oct 31 to Nov 1, or June 29 to July 4. I can do July 5 to July 8 but the changing months confuse me

You will need to store a list (or array) of how many days are in each month. If you add numDays to the current date and it becomes bigger than this, you need to increment the month as well.
For example, we have a date object representing 29 March 2010. We call Increment(4) and add 4 to the day variable, ending up with 33 March 2010. We now check how many days March has and find out it's 31 (eg. daysInMonth[3] == 31). Since 33 is greater than 31, we need subtract 31 from 33 and increase the month, ending up with 2 April 2010.
You will need special handling for February in leap years (any year divisible by 4 and not divisible by 100 unless it's also divisible by 400) and for incrementing past the end of December.

30 days has September, April, June, and November. The rest have 31 days, except for February, which has 28 days except on a leap year (every 4 years, and 2008 was the last one) when it has 29 days.
This should be plenty to get you going.

First, construct a function like
int numDaysSinceBeginning( Date );
which counts number of days elapsed from a well known date (e.g. Jan 1 1900) to the specific Date.
Next, construct another function which converts that day-delta to Date
Date createDateWithDelta( int );
From your example,
Date d2(6, 29, 1950); // June 29, 1950
int d2Delta = numDaysSinceBeginnning( d2 );
Date d2Incremented = createDateWithDelta( d2Delta + 5 ); // d2Incremented is July 4, 1950

Related

How to create a column that enumarate the month based on it's month number?

So I have the following data table:
Month
Month##
id
November
11
BC221
July
7
1232SAD
August
8
DSAGD323
December
12
OKSDF93
October
10
OPAFSD83
September
9
POWER928
August
8
DSAGD323
December
12
DASF32
October
10
HSKJFH73264
September
9
9812973HJKSDF
And I want to create a new columns that enumerate/rank the month in a ascending order like this:
Month
Month##
id
rnk
November
11
BC221
5
July
7
1232SAD
1
August
8
DSAGD323
2
December
12
OKSDF93
6
October
10
OPAFSD83
4
September
9
POWER928
3
August
8
DSAGD323
2
December
12
DASF32
6
October
10
HSKJFH73264
4
September
9
9812973HJKSDF
3
So as you can see above July is going to be the first, August the second, September the third and so on. How can I achieve this?
the easiest way;
simply; Subtract 6 from the month number, if the result is greater than 0, leave it; otherwise, add 6 to the original value;

How do I calculate average from June 1 to June 30 annually and make it a dynamic function using DAX?

I have two tables in PowerBI, one modified date and one fact for customer scores. The relationship will be using the "Month Num" column. Score assessments take place every June, so I would like to be able to have the scores for 12 months (June 1 to June 30) averaged. Then I will just have a card comparing the Previous year score and Current year score. Is there a way to do this dynamically, so I do not have to change the year in the function every new year? I know using the AVERAGE function will be nested into the function somehow, but I am getting confused not using a calendar year and not seasoned enough to use Time Intelligence functions yet.
Customer Score Table
Month
Month Num
Year
Score
Customer #
June
6
2020
94.9
11111
July
7
2020
97
11111
months
continue
2020
100
June
6
2021
89
22222
July
7
2021
91
22222
months
continue
2021
100
June
6
2022
93
33333
July
7
2022
94
33333
Date Table
Month
Month Num
Month Initial
january
1
J
feb
2
F
march
3
M
other
months
continued

Create Week Variable Based on Date Time Variable

Simple question, I think.
I have a checkin_date_time variable in a database with thousands of unique records.
Database
ID checkin_date_time
1 January 01, 2019 11:36:50
2 January 01, 2019 11:36:55
....
60000 December 31, 2019 11:36:50
60001 December 31, 2019 11:36:55
I would like to create a 'week' variable based on the checkin_date_time variable. So for example 'January 01, 2019 11:36:55' would equal week 1 and 'December 31, 2019 15:16:57' would equal week 52.
Desired Output
ID datetime Week
1 January 01, 2019 11:36:50 1
2 January 01, 2019 11:36:55 1
....
60000 December 31, 2019 11:36:50 52
60001 December 31, 2019 11:36:55 52
I tried using the following code but its saying my
data testl;
set ed_tat;
week=week(checkin_date_time);
run;
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
Week operates on a date variable, use DATEPART() to get the date first and then determine the week.
week = week(datepart(checkin_date_time));

Week number calculation in rails

This is my first question on Stack.
I am working on a booking site that relies heavily on searching and finding full weeks of accommodation. Most user searches will be on weeknumber of the year, eg. week 27 for the first week of july.
It is important that the user does not need to fill in year when searching for accommodation, and so the only thing we will get from the user is the weeknumber.
How can I get the year from the week given by the user considering that it always has to be the next upcoming occurrence of that week number?
(There is a gotcha in this. I could get the upcoming week 27 by doing something like this:
def week
week = 27
Date.commercial(Date.current.year + 1, week, 1) # gives the first day of the week
end
But that would only be right until the 1 of January, after that it would be looking for week 27 of 2015.)
You could compare the current calendar week with Date.current.cweek (Reference) with your number.
require 'active_support/core_ext' # Already included in Rails
def calendar_week(week)
now = Date.current
year = now.cweek < week ? now.year : now.year + 1
Date.commercial(year, week, 1)
end
p calendar_week(49)
# => Mon, 02 Dec 2013
p calendar_week(1)
# => Mon, 30 Dec 2013 # don't know if that's the way calendar weeks are counted
p calendar_week(27)
# => Mon, 30 Jun 2014

Am I losing my mind, or is there glaring bug in the Google Annotated Timeline?

I see Google's example code listing dates for January, but the chart is displaying dates for February!
On my test machine, it is doing the same thing. I've told it to display dates for September, but it is displaying dates for October instead!
Can anyone else confirm this as happening?
http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html
The months in the javascript date are 0 based not 1 based. So 0 is Jan, 1 is Feb, etc.
See
http://www.w3schools.com/js/js_obj_date.asp
and you might want to check out
http://en.wikipedia.org/wiki/Off-by-one_error
ECMA-262 5ed, pp.165:
15.9.1.4 Month Number
Months are identified by an integer in the range 0 to 11, inclusive.
No, you're not losing your mind. The month in the Javascript Date object is zero-indexed. That means:
0 = January
1 = February
2 = March
3 = April
4 = May
5 = June
6 = July
7 = August
8 = September
9 = October
10 = November
11 = December