2

Closed

Error on unknown token

description

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);
        }
    }
Closed Oct 19, 2013 at 11:55 PM by abfo

comments

abfo wrote May 17, 2011 at 11:52 PM

I have the same issue. The patch suggested by pawaw works for me.

wrote Feb 14, 2013 at 6:21 PM

wrote Oct 19, 2013 at 11:55 PM

wrote Oct 19, 2013 at 11:55 PM