Error on unknown token

Oct 25, 2010 at 10:51 AM
Edited Oct 25, 2010 at 2:03 PM

Hi,

The current version of this library does not seem to handle all possible tokens like "Sat>=13".

Example: Try to convert a local Spanish time from 1974 to utc and an exception will be raised in Database.ConvertToDateTime.

Solution: I have made the following change to Database.ConvertToDateTime in order to fix this issue:

       public static DateTime ConvertToDateTime(string token, int year, int month)
{
DateTime dt = new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Local);

int dayOfMonth = 1;
if (Int32.TryParse(token, out dayOfMonth))
{
// The token we have is a straight number
dt = dt.AddDays(dayOfMonth - 1);

// Make sure we haven't gone over the month
if (dt.Month != month)
{
dt = dt.AddDays(-1 * dt.Day);
}
}
else
{
String first3 = token.Substring(0,3);
String last3 = "";
if (token.Length - 3 >= 0)
last3 = token.Substring(token.Length - 3, 3);

DayOfWeek day;

// check if first 3 letters is a day
if (IsDay(first3) && token.StartsWith(first3 + ">=", StringComparison.InvariantCultureIgnoreCase))
{
day = ConvertToDay(first3);
String numberStr = token.Substring(5, token.Length - 5);
int date;
if (int.TryParse(numberStr, out date))
if (date < 1 || date > 31)
throw new ArgumentException("The value is an invalid day of month token", token);
else
dt = FirstDayOccurance(dt.AddDays(date - 1), day);
else
throw new ArgumentException("The value is an invalid day of month token", token);
}
else if (IsDay(last3) && token.Equals("last" + last3, StringComparison.InvariantCultureIgnoreCase))
{
day = ConvertToDay(last3);
dt = LastDayOccurance(dt.AddMonths(1).AddDays(-1), day);
}
else
throw new ArgumentException("The value is an invalid day of month token", token);
}

return dt;
}

 

New functions to support the changes above:

 

        public static bool IsDay(string dayStr)
{
switch (dayStr.ToUpperInvariant())
{
case "MON":
case "TUE":
case "WED":
case "THU":
case "FRI":
case "SAT":
case "SUN":
return true;
default:
return false;
}
}

public static DayOfWeek ConvertToDay(string dayStr)
{
switch (dayStr.ToUpperInvariant())
{
case "MON":
return DayOfWeek.Monday;
case "TUE":
return DayOfWeek.Tuesday;
case "WED":
return DayOfWeek.Wednesday;
case "THU":
return DayOfWeek.Thursday;
case "FRI":
return DayOfWeek.Friday;
case "SAT":
return DayOfWeek.Saturday;
case "SUN":
return DayOfWeek.Sunday;
default:
throw new ArgumentException("The value is an invalid day", dayStr);
}
}

 

Any comments are of course welcome.

May 18, 2011 at 12:51 AM

Thanks for sharing, I've recently encountered the same issue and this fix works for me.

Dec 6, 2011 at 12:54 PM
Edited Dec 6, 2011 at 12:55 PM

Thank you very much for the fix.

Visual studio 2010 has updated my project and then I started receiving: System.ArgumentException: The value is an invalid day of month token for Zone.Now

The value is an invalid day of month token Parameter name: Sun&gt;=16 at PublicDomain.ZoneInfo

After replacing the ConvertToDateTime method with yours and adding the other two methods from your snippet in Database.cs and rebuilding the .dll for my website now it seems to work fine!

Oct 20, 2013 at 12:58 AM
Added this fix to the code today.