I've tried searching for an algorithm that could achieve this but haven't come up with any results.
I have the constructor Date2013(int dd).
In this member function I created an array with the months in it (January, February, Etc)
Then I created an if statement that takes in dd, if dd falls in between day 1 -31, then output January, if it falls between 32 - 59, then output February.
What I'm having trouble doing is, then taking the number they input, dd, and converting that to the appropriate day in the month. ie(The 34th day of the year is February 2nd).
Does anyone know of any algorithms or know of a way I could achieve this?
(I'm just beginning to learn my C++ so an explanation or comments in any code would be really helpful so that I can understand your thought process and syntax)
So, the basic principle for CALCULATING this is to have a list of "number of days per month", and basically.
If we then have a variable called
days
which is the number of days within the year and amonth
to indicate which month we end in:Start with "month = 1;", simply check if the
days
is less thandays_in_month[month]
, and if it isn't subtractdays_in_month[month]
from days, and keep going until you either reach month = 12 and still have more days and days is more than thedays_in_month[month]
[which means it's next year, start over withmonth=1
, and move to nextyear
- but I think your task is limited to 1-365 days, so just report an error is fine for now].When the number of days isn't more than
days_in_month[month]
, you have the number of days into that month.Note that I have intentionally not written the code for how to do this, but described how to do it. Because I'm the one who has written this sort of code at least a few times, and you are the one trying to learn. You do not learn by copying and pasting from SO.
Edit: And yes, I've completely ignored leap-years. For 2013, it's not a leap-year, so we don't care... ;)
The most portable & reliable approach is to use the ANSI C mktime & localtime functions, which will work with either C or C++
Update:
To answer the question about how to implement your own algorithm rather than using standard C library mktime function, a good starting point would be to look at known-working code such as the glibc mktime source code. The relevant tidbits you would need include:
From glibc 2.17 (HEAD) mktime.c around line 141:
From glibc 2.17 (HEAD) mktime.c starting at line 160:
A sample C++ class implementing a constructor that converts year & dayOfYear to corresponding month & dayOfMonth might look something like:
Hopefully I won't get down-voted for showing sample code that might do the job. Feel free to implement it however you want of course. This is just one example approach.
If you would rather use the existing mktime functionality (recommended), which should most likely be present in the standard C library on any platform you are programming, my original answer content follows...
When using mktime you need to be sure to set tm_mon=0 and tm_mday to the day of the year (see mktime documentation for a better explanation), but in a nutshell mktime ignores tm_wday and tm_yday and only translates tm_mday, which it essentially interprets as the day of the year if you also set tm_mon = 0;
Here is some working sample code that illustrates the point, which will work in C or C++:
Compile & sample runs:
Even works for leap years like 2012: