简体   繁体   中英

C# Sorted List Order Coming out wrong - datetime

Any help would be apreciated. I'm just ac# beginner and unfortunately have inherited ac# app that uses xmlreader to validate xml files against a schema, flagging for various errors such as overlapping 'events' (dates) in the xml files.

Example of xml file with 2 events (this one would correctly flag an error saying events overlap as sched duration of PT45M overruns next schedstarttime of 18:30.):

<TimePeriod startTime="2014-01-31T18:00:00+00:00" endTime="2014-02-01T18:00:00+00:00"/>  (Whole time of all  xml file events) 
  <ScheduleEvent eventId="0019" schedStartTime="2014-01-31T18:00:00+00:00" schedDuration="PT45M"
                 billedStartTime="2014-01-31T18:00:00+00:00"
                 billedDuration="PT30M">

<ScheduleEvent eventId="001a" schedStartTime="2014-01-31T18:30:00+00:00" schedDuration="PT30M"
                     billedStartTime="2014-01-31T18:30:00+00:00"
                     billedDuration="PT30M">

etc…

This all works perfectly, until an XML file that overruns a month border is introduced - eg th time period has events that stretch from 31/01/2014 to 01/02/2014 (all times in UTC). The events that are in the next day return as an overlap error. The events are read in using a sortedlist:

  public void setEventStart(DateTime eventStartIn, int lineNumber)
    {
        eventStart.Add(lineNumber,eventStartIn.ToString());
    }

    public SortedList getEventStart()
    {
        return eventStart;
    }

    public void setBillStart(DateTime eventStartIn, int lineNumber)
    {
        billStart.Add(lineNumber, eventStartIn.ToString());
    }

    public SortedList getBillStart()
    {
        return billStart;
    }
    public void setEventOutOfTimeFrame(int inLineNumber)
    {
        eventOutOfTimeFrame = inLineNumber;
    }

this is then vaidatated using datetimecompare code:

  public int checkEventValidatity(int i)
    {
          {
            int startSortInt = eventStart.IndexOfValue(getStartEventArrayValue(i));
            //Variable get the number of Events in the file
            int length = getEventStartLength() - 1;
            int nextEvent = i + 1;

            //Resets the methods vairables
            resetVariable();

            //There is no need to check the first event
            if (i != length) 
            {
                //Variable to hold the next event index
                int eventCheckInt = eventStart.IndexOfValue(getStartEventArrayValue((nextEvent)));

                //Checks the event start time and end time are in the is in the time frame of the xml file
                if (checkInTimeFrame(startSortInt) == true)
                {
                    String eventStartTime = eventStart.GetByIndex(eventCheckInt).ToString();
                    //eventstartime is next event (eventcheckint)index puts to string eventStartTime

                    String eventEndTime = eventEnd.GetByIndex(startSortInt).ToString();

                    int j = DateTime.Compare(DateTime.Parse(eventEndTime), DateTime.Parse((eventStartTime)));

                    this.setEventInvalidLineNo1((int)eventEnd.GetKey(startSortInt));
                    this.setEventInvalidLineNo2((int)eventEnd.GetKey(eventCheckInt));
                    validEvent = 1;
                    setEventError(j);
                    Debug.WriteLine("EventStarttime " + eventStartTime + " " startSortInt); //Test output
                    Debug.WriteLine("EventEndTime " + eventEndTime + " " eventCheckInt); //Test Output
                    Debug.WriteLine("EventEnd " + eventEnd); //Test Output
                }
                //Debug.WriteLine(j);
            }
        }

The debugger shows the following output, which shows that the sorted list is not validating in order with event 9 on the following day being used as the first event, which knocks all out of sync and returns an error:

This is the output of

Debug.WriteLine("EventStarttime " + eventStartTime); //Test output
Debug.WriteLine("EventEndTime " + eventEndTime); //Test Output 

xml file duration 31/01/2014 - 01/02/2014 - sorted list in wrong order, event 9 which should be last on list at 01/02/2014 16:00:00 actually is ordered before the real fist event 31/01/2014 18:30:00 ( the 18:00:00 event should be skipped.)

EventStarttime 01/02/2014 16:00:00 9 EventEndTime 31/01/2014 18:30:00 0

EventStarttime 31/01/2014 18:00:00 0 EventEndTime 31/01/2014 19:00:00 1

EventStarttime 31/01/2014 18:30:00 1 EventEndTime 31/01/2014 19:30:00 2

EventStarttime 31/01/2014 19:00:00 2 EventEndTime 31/01/2014 20:00:00 3

EventStarttime 31/01/2014 19:30:00 3 EventEndTime 31/01/2014 20:30:00 4

EventStarttime 31/01/2014 20:00:00 4 EventEndTime 31/01/2014 21:00:00 5

EventStarttime 31/01/2014 20:30:00 5 EventEndTime 31/01/2014 21:30:00 6

EventStarttime 31/01/2014 21:00:00 6 EventEndTime 31/01/2014 22:00:00 7

EventStarttime 31/01/2014 21:30:00 7 EventEndTime 01/02/2014 17:00:00 8

It should go eventStartTime 0 - > event EndTime 1, and so on.


Xml file duration 01/01/2014 to 02/01/2014 - No issues - all in order

EventStarttime 01/01/2014 18:30:00 0 EventEndTime 01/01/2014 18:30:00 1

EventStarttime 01/01/2014 19:00:00 1 EventEndTime 01/01/2014 19:00:00 2

EventStarttime 01/01/2014 19:30:00 2 EventEndTime 01/01/2014 19:30:00 3

EventStarttime 01/01/2014 20:00:00 3 EventEndTime 01/01/2014 20:00:00 4

EventStarttime 01/01/2014 20:30:00 4 EventEndTime 01/01/2014 20:30:00 5

EventStarttime 01/01/2014 21:00:00 5 EventEndTime 01/01/2014 21:00:00 6

EventStarttime 01/01/2014 21:30:00 6 EventEndTime 01/01/2014 21:30:00 7

EventStarttime 01/01/2014 22:00:00 7 EventEndTime 01/01/2014 22:00:00 8

...and so on. Still orders perfectly in datetime even when it goes onto the next day 01/01/2014 - 02/01/2014.


What could cause this? I am only learning c# and have tried a number of things at fixing this, but I'm not sure why the change of month would knock the sorted list order out like this, (specially as it is in datetime) any help would be very appreciated????

Its incredibly frustrating!

Avoid rendering and parsing dates, just stay on the date object and use simple comparers like < and > :

DateTime eventStartTime = eventStart.GetByIndex(eventCheckInt);
DateTime eventEndTime = eventEnd.GetByIndex(startSortInt);

if(eventEndTime < eventStartTime)
{
    // here you've detected an error
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM