c# - How can I check whether there is conflict in time from string like 6:00 PM to 9:00 PM -


i building exam datesheet. i'm having issues in finding conflicts between time..

i have list of strings stores time intervals like-

list<string> times = new list<string>(); times.add("6:00 pm 9:00 pm"); times.add("10:00 1:00 pm"); 

now suppose, if want add below time list, first want check not conflict time there.

so, in below case, should not added.

if(notconflict("5:00 pm 7:00 pm"))     times.add("5:00 pm 7:00 pm"); 

but following can added since there no conflict.

if(notconflict("2:00 pm 5:00 pm"))     times.add("2:00 pm 5:00 pm"); 

i cannot use datetime here because old system , time being stored above. , being used @ many places.

this should work:

private static tuple<datetime, datetime> parsedate(string datetimes) {     var split = datetimes.split(new[] { " " }, stringsplitoptions.none);     var time1 = datetime.parseexact(split[0], "h:mm tt",                                         cultureinfo.invariantculture);     var time2 = datetime.parseexact(split[1], "h:mm tt",                                         cultureinfo.invariantculture);      return tuple.create(time1, time2); }   private static bool notconflict(ienumerable<string> times, string time) {     var inctime = parsedate(time);      return !times.any(t => {         var parsed = parsedate(t);           return inctime.item1 <= parsed.item2 && parsed.item1 <= inctime.item2;     }); }  public static void main() {     var times = new list<string>();     times.add("6:00 pm 9:00 pm");     times.add("10:00 1:00 pm");      console.writeline("no conflict 5:00 pm 7:00 pm: {0}", notconflict(times, "5:00 pm 7:00 pm"));     console.writeline("no conflict 2:00 pm 5:00 pm: {0}", notconflict(times, "2:00 pm 5:00 pm")); } 

parsedate return formatted tuple start , end time in item1 , item2 respectively. use linq's any function filter , make sure don't return fall within bounds.

see dotnet fiddle here.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -