开发者

TryParse datetime from invidivual time elements

开发者 https://www.devze.com 2023-03-18 11:42 出处:网络
I need to validate that certain variables can create a valid datetime and if so make it otherwise ignore it without throwing an exception.

I need to validate that certain variables can create a valid datetime and if so make it otherwise ignore it without throwing an exception.

I have the following code

 int y, m, d, h, mi, s, tz;
ogrFeature.GetFieldAsDateTime(field, out y, out m, out d, out h, out mi, out s, out tz);
fdr[fdrIndex++] = new DateTime(y, m, d, h, mi, s);

At the moment the construction of the date time will fail if (from MSDN)

year is less than 1 or grea开发者_StackOverflowter than > 9999.

month is less than 1 or greater than> 12.

day is less than 1 or greater than the> number of days in month.

hour is less than 0 or greater than> 23.

minute is less than 0 or greater than> 59.

second is less than 0 or greater than> 59.

millisecond is less than 0 or greater> than 999.

I've had a look through and there doesn't seem to be any specific methods to validate this kind of input.

Is there a nice way to validate this input without having to have a whole bunch of ifs or wrap it in a nasty try/catch and catch the ArgumentOutOfRangeException?


You can use this method:

public static DateTime? GetFieldAsDateTime(int y, int m, int d, 
    int h, int mi, int s)
{
    DateTime result;
    var input = 
        string.Format("{0:000#}-{1:0#}-{2:0#}T{3:0#}:{4:0#}:{5:0#}", 
        y, m, d, h, mi, s);

    if (DateTime.TryParse(input, CultureInfo.InvariantCulture, 
        System.Globalization.DateTimeStyles.RoundtripKind, out result))
    {
        return result;
    }

    return null;
}


You've come close to the right answer: the TryParse pattern.

public static bool TryCreateDateTime(int y, int m, int d, int h, int mi, int s, 
    out DateTime dt){
  int[] normalDays={0,31,28,31,30,31,30,31,31,30,31,30,31};
  int[] leapDays  ={0,31,29,31,30,31,30,31,31,30,31,30,31};
  dt=DateTime.MinValue;
  if(y>=1 && y<=9999 &&
    m>=1 && m<=12 &&
    d>=1 && d<=(DateTime.IsLeapYear(y) ? leapDays : normalDays)[m] &&
    h>=0 && h<=23 &&
    mi>=0 && mi<=59 && s>=0 && s<=59){
    dt=new DateTime(y,m,d,h,mi,s);
    return true;
  }
  return false;
}

Returns true if the input is valid, false otherwise.


I had a lot of fun coming up with an answer to this! I think this came out pretty neat, because it validates the input, returns a true value you can use easily, or a human readable error message. Furthermore, with one simple if-statement you can execute your code or do something with the error. Customize this how you see fit (ie. refactor into a method), if it doesn't work out for you or fit your needs I might try something else as I thought this was an interesting problem. Basically the idea here is that for each condition that returns true, we simply step to the next condition until there are no more conditions to check, then we return a Boolean.TrueString you can use to do some checking.

int y, m, d, h, mi, s, tz;
ogrFeature.GetFieldAsDateTime(field, out y, out m, out d, out h, out mi, out s, out tz);

string error = 
      (y < 1 || y > 9999) 
    ? (m < 1 || m > 12) 
    ? (d < 1 || d > 31) 
    ? (h < 0 || h > 23) 
    ? (m < 0 || m > 59) 
    ? (s < 0 || s > 59) 
    ? (tz < 0 || tz > 999) 
    ? Boolean.TrueString // All conditions have passed validation. Return "True".
    : "Year is less than 1 or greater than > 9999."
    : "Month is less than 1 or greater than > 12."
    : "Day is less than 1 or greater than the > number of days in month."
    : "Hour is less than 0 or greater than > 23."
    : "Minute is less than 0 or greater than > 59."
    : "Second is less than 0 or greater than > 59."
    : "Millisecond is less than 0 or greater > than 999.";

if (error == bool.TrueString) {
    fdr[fdrIndex++] = new DateTime(y, m, d, h, mi, s);
}
else {
    // Display error, for example:
    // ie.
    MessageBox.Show(error, "Incorrect Date Format", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号