Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string with date:
C++
char sDate[] = "10/04/2014";

and I have to get double value from it - "41739"
Posted
Updated 20-Apr-14 0:11am
v4
Comments
Andreas Gieriet 20-Apr-14 6:58am    
What is the algorithm? It looks like days since January 1st, 1900, correct?
Andi
[no name] 20-Apr-14 20:53pm    
Not a good question because the requirement is not clear.

Do somethnig like the following brute force approach:
C++
int calulate_day_number_since_19000101(int year, int month, int day)
{
    int days = 0;
    for (int y = 1900; y < year; ++y)
    {
        days += 365;
        if (is_leap_year(y)) days++;
    }
    for (int m = 1; m < month; ++m)
    {
        if (m == 2)
        {
            days += 28;
            if (is_leap_year(year)) days++;
        }
        else if (m < 8)
        {
            days += m % 2 ? 31 : 30;
        }
        else
        {
            days += m % 2 ? 30 : 31;
        }
    }
    days += day;
}
int is_leap_year(int year)
{
    if (year % 400  == 0) return 1;
    if (year % 100  == 0) return 0;
    if (year % 4    == 0) return 1;
    return 0;
}


calulate_day_number_since_19000101(1900, 1, 1); returns 1.
calulate_day_number_since_19000101(2014, 4, 10); returns 41738.

So, why 41739 is not clear to me... I guess the leap year calculation of your reference implementation is wrong: every 100th year is not a leap year, but every 400th year is a leap year again. If ignoring the above mentioned facts, I also get 41739.

Cheers
Andi

PS: Parsing the string into year, month, day is left as exercise ;-)
 
Share this answer
 
v2
Comments
[no name] 20-Apr-14 20:52pm    
This seems to meet requirement. Why a double is needed for this is a mystery though.
Use the function ToOADate()[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900