Exemplo n.º 1
0
        static void Main(string[] args)
        {
            ConvertDate converter = new ConvertDate();

            if (args.Length == 1 && args[0] == "--help")
            {
                Info.WriteHelp();
            }
            else if (args.Length == 2)
            {
                try
                {
                    DateTime date1 = converter.ParseDate(args[0]),
                             date2 = converter.ParseDate(args[1]);

                    converter.CheckDates(date1, date2);

                    Console.WriteLine(converter.GetDateRange(date1, date2));
                }
                catch (IncorrectFormatException e)
                {
                    Console.WriteLine(e.Message);
                    Info.RunInfo();
                }
                catch (IncorrectRelationshipException e)
                {
                    Console.WriteLine(e.Message);
                    Info.RunInfo();
                }
            }
            else
            {
                Info.IncorrectNumberOfParameters(args);
            }
        }
        public void GetDateRangeTest()
        {
            var      converter = new ConvertDate();
            DateTime date1, date2;
            string   format = "dd.MM.yyyy";

            //same month and year
            date1 = DateTime.ParseExact("01.01.2017", format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
            date2 = DateTime.ParseExact("05.01.2017", format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);

            Assert.AreEqual("01-05.01.2017", converter.GetDateRange(date1, date2));

            //same year
            date1 = DateTime.ParseExact("01.01.2017", format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
            date2 = DateTime.ParseExact("05.02.2017", format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);

            Assert.AreEqual("01.01-05.02.2017", converter.GetDateRange(date1, date2));

            //different dates
            date1 = DateTime.ParseExact("01.01.2016", format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
            date2 = DateTime.ParseExact("05.01.2017", format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);

            Assert.AreEqual("01.01.2016-05.01.2017", converter.GetDateRange(date1, date2));
        }