Date Logic?

02-17-2015, 08:41 PM
#1

Date Logic?

Maybe I just need to sleep on this one... I don't know why I'm finding this so difficult right now, but here is my problem:

Say I have a date in the format M/D/Y. The date values are improper to start with:

The only proper value to go by is the year. How would you go about normalizing this into a proper date, taking into account leap years? I know how to calculate whether a year is a leap year, but the problem is updating -8 and -17 to their respective values and updating the year as well if needed so that these numbers are in the proper ranges of 1-(31/30/28/29) and 1-12.

This is assuming 1 is either January or the 1st day of the month, depending on whether you are looking at the M or D aspect of the date string.

So in other words, you could look at this invalid date as a resultant date offset 9 months backwards, 18 days backwards from 1/1/2000 (January 1st, 2000).

I need to normalize this into the resultant date, considering leap years that my arithmetic might cross, meaning either 28 or 29 days in February, in addition to the 30 or 31 days for every other month of the year.

** All logic can not use any libraries, this is a strictly mathematical problem **

edit: I'm assuming I should start with the invalid day offset, since that will (based on the year specified), once normalized, give me the final invalid month value, based on the initial invalid month offset. Then I should just be able to take that invalid month offset, see how many cycles it makes around a full year, normalize it, and update the year.

Say I have a date in the format M/D/Y. The date values are improper to start with:

Code:

`-8/-17/2000`

The only proper value to go by is the year. How would you go about normalizing this into a proper date, taking into account leap years? I know how to calculate whether a year is a leap year, but the problem is updating -8 and -17 to their respective values and updating the year as well if needed so that these numbers are in the proper ranges of 1-(31/30/28/29) and 1-12.

This is assuming 1 is either January or the 1st day of the month, depending on whether you are looking at the M or D aspect of the date string.

So in other words, you could look at this invalid date as a resultant date offset 9 months backwards, 18 days backwards from 1/1/2000 (January 1st, 2000).

I need to normalize this into the resultant date, considering leap years that my arithmetic might cross, meaning either 28 or 29 days in February, in addition to the 30 or 31 days for every other month of the year.

** All logic can not use any libraries, this is a strictly mathematical problem **

edit: I'm assuming I should start with the invalid day offset, since that will (based on the year specified), once normalized, give me the final invalid month value, based on the initial invalid month offset. Then I should just be able to take that invalid month offset, see how many cycles it makes around a full year, normalize it, and update the year.

This post was last modified: 02-17-2015, 08:46 PM by AceInfinity.

02-17-2015, 09:45 PM
#2

RE: Date Logic?

I believe I got it, but this thread will remain open for suggestions and alternative methods. :)

02-18-2015, 03:59 PM
#3

RE: Date Logic?

So, will both month and day always be offset? Or it varies?

Either way, I think you'd handle it the same.

I'd start by determining if their is an offset, If the date IS in fact an offset, modify the month offset (i.e. if it's -17 day and the month is -8, adjust the month to be -9 and set the date accordingly [13 if it's a 30 day month, etc]). Then do the month offset, and if it's -12, offset the year as well. I'm assuming this counts down from 12/31/(given year).

Then of course the leap year/days logic would come with that.

Either way, I think you'd handle it the same.

I'd start by determining if their is an offset, If the date IS in fact an offset, modify the month offset (i.e. if it's -17 day and the month is -8, adjust the month to be -9 and set the date accordingly [13 if it's a 30 day month, etc]). Then do the month offset, and if it's -12, offset the year as well. I'm assuming this counts down from 12/31/(given year).

Then of course the leap year/days logic would come with that.

02-18-2015, 07:46 PM
#4

RE: Date Logic?

Varies... It may be in the proper format. I just want a method that will normalize the values, assuming the Year will always be valid to go by as a reference.

So a date string of "-1/-1/2014" would be 1/1/2014 -2days, and -2months, since the values are not 0 based, 1 - -1 == 2. The problem comes in when you have values like "-234/-2348/2014". (My program determines that to be 7/28/2007)

Because, now I have to find out what month is the month ofset from the January, back 2349 times (cyclically), determine if that is a leap year, and deal with the days offset, which may update the month, but also the resultant year.

So a date string of "-1/-1/2014" would be 1/1/2014 -2days, and -2months, since the values are not 0 based, 1 - -1 == 2. The problem comes in when you have values like "-234/-2348/2014". (My program determines that to be 7/28/2007)

Because, now I have to find out what month is the month ofset from the January, back 2349 times (cyclically), determine if that is a leap year, and deal with the days offset, which may update the month, but also the resultant year.

This post was last modified: 02-18-2015, 07:50 PM by AceInfinity.

02-19-2015, 08:26 PM
#5

RE: Date Logic?

Thinking about it now, in .Net this would be easy using the Date class. I assume this is out of the question?

02-19-2015, 10:57 PM
#6

RE: Date Logic?

Yep. I'm writing a class from scratch to implement this functionality in standard C++. .NET is not available. It seems to work, but I'm not confident in my method right now. I just haven't had time to look it over since yesterday.

This post was last modified: 02-19-2015, 10:58 PM by AceInfinity.

Users browsing this thread: 1 Guest(s)