2

Closed

Errors in LastDayOccurance && FirstDayOccurance

description

Methods are not working

file attachments

Closed Oct 20, 2013 at 12:08 AM by abfo

comments

lln78 wrote Jul 18, 2008 at 5:37 PM

(Sorry, I submitted when i wanted to add a new line...)

For example, with FirstDayOccurance("2008/07/01",Sunday == 0) it will return (dt.AddDays(2-0)) 2008/07/03, which is not Sunday...

But i've a solution

public static DateTime LastDayOccurance(DateTime dt, DayOfWeek dow)
    {
        // Best case is that we are there already
        if (dow == dt.DayOfWeek)
            return dt;

        int currDOW = ConvertToInt32(dt.DayOfWeek);
        int expDOW = ConvertToInt32(dow);

        int toRemove = 0;
        if (currDOW > expDOW)
        {
            toRemove = currDOW - expDOW;

            //dt = dt.AddDays(expDOW - currDOW);
        }
        else
        {
            toRemove = 7 + currDOW - expDOW;
            //dt = dt.AddDays(currDOW - expDOW);
        }
        dt = dt.AddDays(-toRemove);
        Debug.Assert(dt.DayOfWeek == dow);
        return dt;
    }

    public static DateTime FirstDayOccurance(DateTime dt, DayOfWeek dow)
    {
        // Best case is that we are there already
        if (dow == dt.DayOfWeek)
            return dt;
        int currDOW = ConvertToInt32(dt.DayOfWeek);
        int expDOW = ConvertToInt32(dow);

        int toAdd = 0;
        if (currDOW > expDOW)
        {
            toAdd = 7 - (currDOW - expDOW);
            //dt = dt.AddDays(currDOW - expDOW);
        }
        else
        {
            toAdd = expDOW - currDOW;
            //dt = dt.AddDays(expDOW - currDOW);
        }
        dt = dt.AddDays(toAdd);
        if (dt.DayOfWeek != (System.DayOfWeek)dow)
        {
            Debug.Assert(dt.DayOfWeek == dow, "FDO Day " + dow + "  ==  " + dt + " Day " + dt.DayOfWeek);
        }
        return dt;
    }


markjrodrigues wrote Sep 5, 2008 at 1:54 AM

Hi,

Sorry I haven't responded to this earlier, I was getting any notifications on comments in here.
I don't think I fully understand the problem you have found (so I can't quite evaluate the solution).

Can you please explain the problem in a little bit more detail?

Thanks

Mark

wrote Oct 29, 2008 at 10:03 AM

kijn wrote Oct 29, 2008 at 10:06 AM

I noticed this during the end of DST for the EU rule (2008-10-27 01:00:00).
This was my solution for the LastDayOccurance method and it is basically the same as above but with fewer lines ;)
Keep up the good work with this! This is the way to handle zoneinfo!!!
    private static DateTime LastDayOccurance(DateTime dt, DayOfWeek dow)
    {
        // Best case is that we are there already
        if (dow == dt.DayOfWeek)
            return dt;

        int currDOW = ConvertToInt32(dt.DayOfWeek);
        int expDOW = ConvertToInt32(dow);

        if (currDOW > expDOW)
        {
            dt = dt.AddDays(expDOW - currDOW);
        }
        else
        {
            dt = dt.AddDays((expDOW - currDOW)-7);
        }

        return dt;
    }

wrote Oct 29, 2008 at 10:06 AM

kijn wrote Oct 29, 2008 at 10:12 AM

This is just an update to the database.cs with the fix for FirstDayOccurance method incorporated as suggested by "lln78 at Jul 18 at 6:37 PM"

wrote Oct 29, 2008 at 10:12 AM

wrote Oct 29, 2008 at 11:20 AM

wrote Oct 29, 2008 at 11:20 AM

markjrodrigues wrote Oct 29, 2008 at 11:22 AM

Thanks for the comments.
I will do a little bit of testing with the change and if all looks good check it in.
Your contributions are much appreciated and I am glad you getting use out of this :)

wrote Oct 29, 2008 at 11:23 AM

markjrodrigues wrote Nov 1, 2008 at 12:14 AM

This is now checked-in.
Thanks so much for the code change.
It was a pretty significant error so I am surprised more people didn't find it (or didn't let me know about it :)
Thanks again!

wrote Nov 1, 2008 at 12:14 AM

wrote Feb 14, 2013 at 7:21 PM

wrote May 16, 2013 at 8:54 AM

wrote May 16, 2013 at 8:54 AM

wrote Jun 14, 2013 at 7:52 AM

wrote Oct 20, 2013 at 12:08 AM