views:

39

answers:

0

Here's a big challenge... something not easy to do with clean code (though that's not really necessary).

I have two strings, one a list of dates, one of times. The time can be on the first string. The format is unpredictable, but if it can meet the samples below, it would cover 90% of cases. Input date format is DD/MM

public void Test()
{
    System.DateTime BaseDate = new System.DateTime(2010, 9, 29);
    TestDate("Next tuesday", "20 to 4h", "2010-10-05T20:00:00;", BaseDate);
    TestDate("Every tuesday until 14/10", "20 to 4h", "2010-10-05T20:00:00;2010-10-12T20:00:00;", BaseDate);
    TestDate("1 to 3/10", "Friday and saturday 20h, Sunday 18h", "2010-10-01T20:00:00;2010-10-02T21:00:00;2010-10-03T18:00:00;", BaseDate);
    TestDate("31/12 to 2/1/2011 at 20:30", "", "2010-12-30T20:30:00;2011-01-01T20:30:00;2011-01-02T20:30:00;", BaseDate);
    TestDate("Tomorrow 20h, saturday 21h", "", "2010-09-30T20:00:00;2010-10-02T21:00:00;", BaseDate);
    TestDate("1 and 3/10, 4/11", "20h", "2010-10-01T20:00:00;2010-10-03T20:00:00;2010-11-04T20:00:00;", BaseDate);
}

public void TestDate(string InputDates, string InputTimes, string Output, System.DateTime BaseDate)
{
    Debug.out("testing " + InputDates + "; " + InputTimes + " -> " + Output);
    dynamic D = AnalyzeDate(InputDates, InputTimes, BaseDate);
    string R = "";
    foreach (object DD_loopVariable in D) {
        DD = DD_loopVariable;
        R += D.ToString("s") + ";";
    }
    if (R != Output)
        throw new Exception("Fail");
}