示例#1
0
        public List<Agenda> GetFreeTimeAgendaList(DateTime dateTime)
        {
            DateTime startDateTime = new DateTime();
            DateTime endDateTime = new DateTime();
            int year = dateTime.Year;
            int month = dateTime.Month;
            int day = dateTime.Day;
            startDateTime = new DateTime(year, month, day, 0, 0, 0);
            endDateTime = new DateTime(year, month, day, 23, 59, 59);


            AgendaSorter dateTimeSorter = new DateTimeSorter();
            List<Agenda> tempList = dateTimeSorter.Sort(GetAgendaList(dateTime));
            List<Agenda> agendaList = new List<Agenda>();
            foreach (Agenda agenda in tempList)
            {
                Agenda newAgenda = new Agenda(agenda.StartDateTime, agenda.EndDateTime);
                agendaList.Add(newAgenda);
            }
            //merge
            for (int i = 0; i < agendaList.Count; i++)
            {
                for (int j = i + 1; j < agendaList.Count; j++)
                {
                    if (agendaList[i].EndDateTime >= agendaList[j].StartDateTime
                        && agendaList[i].EndDateTime <= agendaList[j].EndDateTime)
                    {
                        agendaList[i].EndDateTime = agendaList[j].EndDateTime;
                        agendaList.RemoveAt(j);
                    }
                }
            }
            //change
            List<Agenda> freeTimeAgendaList = new List<Agenda>();
            //first
            if (agendaList.Count > 0)
            {
                if (startDateTime < agendaList[0].StartDateTime)
                {//dateTime的前一天
                    Agenda agenda = new Agenda(startDateTime, agendaList[0].StartDateTime);
                    freeTimeAgendaList.Add(agenda);
                }
                //last
                for (int i = 0; i < agendaList.Count - 1; i++)
                {
                    Agenda agenda = new Agenda(agendaList[i].EndDateTime, agendaList[i + 1].StartDateTime);
                    freeTimeAgendaList.Add(agenda);
                }
                if (agendaList[agendaList.Count - 1].EndDateTime < endDateTime)
                {//dateTime的前一天
                    Agenda agenda = new Agenda(agendaList[agendaList.Count - 1].EndDateTime, endDateTime);
                    freeTimeAgendaList.Add(agenda);
                }
            }
            else
            {
                Agenda agenda = new Agenda(startDateTime, endDateTime);
                freeTimeAgendaList.Add(agenda);
            }
            return freeTimeAgendaList;
        }
示例#2
0
        public List <Agenda> GetFreeTimeAgendaList(DateTime dateTime)
        {
            DateTime startDateTime = new DateTime();
            DateTime endDateTime   = new DateTime();
            int      year          = dateTime.Year;
            int      month         = dateTime.Month;
            int      day           = dateTime.Day;

            startDateTime = new DateTime(year, month, day, 0, 0, 0);
            endDateTime   = new DateTime(year, month, day, 23, 59, 59);


            AgendaSorter  dateTimeSorter = new DateTimeSorter();
            List <Agenda> tempList       = dateTimeSorter.Sort(GetAgendaList(dateTime));
            List <Agenda> agendaList     = new List <Agenda>();

            foreach (Agenda agenda in tempList)
            {
                Agenda newAgenda = new Agenda(agenda.StartDateTime, agenda.EndDateTime);
                agendaList.Add(newAgenda);
            }
            //merge
            for (int i = 0; i < agendaList.Count; i++)
            {
                for (int j = i + 1; j < agendaList.Count; j++)
                {
                    if (agendaList[i].EndDateTime >= agendaList[j].StartDateTime &&
                        agendaList[i].EndDateTime <= agendaList[j].EndDateTime)
                    {
                        agendaList[i].EndDateTime = agendaList[j].EndDateTime;
                        agendaList.RemoveAt(j);
                    }
                }
            }
            //change
            List <Agenda> freeTimeAgendaList = new List <Agenda>();

            //first
            if (agendaList.Count > 0)
            {
                if (startDateTime < agendaList[0].StartDateTime)
                {//dateTime的前一天
                    Agenda agenda = new Agenda(startDateTime, agendaList[0].StartDateTime);
                    freeTimeAgendaList.Add(agenda);
                }
                //last
                for (int i = 0; i < agendaList.Count - 1; i++)
                {
                    Agenda agenda = new Agenda(agendaList[i].EndDateTime, agendaList[i + 1].StartDateTime);
                    freeTimeAgendaList.Add(agenda);
                }
                if (agendaList[agendaList.Count - 1].EndDateTime < endDateTime)
                {//dateTime的前一天
                    Agenda agenda = new Agenda(agendaList[agendaList.Count - 1].EndDateTime, endDateTime);
                    freeTimeAgendaList.Add(agenda);
                }
            }
            else
            {
                Agenda agenda = new Agenda(startDateTime, endDateTime);
                freeTimeAgendaList.Add(agenda);
            }
            return(freeTimeAgendaList);
        }