public static bool TryParseDateTime(this string s, out System.DateTime result)
        {
            // implement this using string manipulation

            result = new System.DateTime();

            // Wed, 07 Oct 2015 23:17:26 GMT
            // 0         1         2
            // 0123456789012345678901234567890
            try
            {
                int day = int.Parse(s.Substring(5, 2));
                int month = System.Array.IndexOf(months, s.Substring(8, 3)) + 1;
                int year = int.Parse(s.Substring(12, 4));

                int hour = int.Parse(s.Substring(17, 2));
                int minute = int.Parse(s.Substring(20, 2));
                int second = int.Parse(s.Substring(23, 2));

                // create DateTime with these values
                // make sure it's UTC
                result = new System.DateTime(year, month, day, hour, minute, second, 0).ToUniversalTime();

                // we should have a valid DateTime 
                return true;
            }
            catch { };

            // try with next format
            // 10/7/2015 23:17:26 PM
            // this one has a nice pattern, split with spaces to get date and time and then split each one to get individual date bits
            try
            {
                string[] splitDateTime = s.Split(' ');
                string[] date = splitDateTime[0].Split('/');
                string[] time = splitDateTime[1].Split(':');

                // create DateTime with these values
                // make sure it's UTC
                result = new System.DateTime(int.Parse(date[2]), int.Parse(date[0]), int.Parse(date[1]), int.Parse(time[0]), int.Parse(time[1]), int.Parse(time[2])).ToUniversalTime();

                // check for PM time and add 12H if required
                if (splitDateTime[2] == "PM")
                {
                    result = result.AddHours(12);
                }

                // we should have a valid DateTime now
                return true;
            }
            catch { };

            // no joy parsing this one
            return false;
        }
Ejemplo n.º 2
0
        public static bool TryParseDateTime(this string s, out System.DateTime result)
        {
            // implement this using string manipulation

            result = new System.DateTime();

            // Wed, 07 Oct 2015 23:17:26 GMT
            // 0         1         2
            // 0123456789012345678901234567890
            try
            {
                int day   = int.Parse(s.Substring(5, 2));
                int month = System.Array.IndexOf(months, s.Substring(8, 3)) + 1;
                int year  = int.Parse(s.Substring(12, 4));

                int hour   = int.Parse(s.Substring(17, 2));
                int minute = int.Parse(s.Substring(20, 2));
                int second = int.Parse(s.Substring(23, 2));

                // create DateTime with these values
                // make sure it's UTC
                result = new System.DateTime(year, month, day, hour, minute, second, 0).ToUniversalTime();

                // we should have a valid DateTime
                return(true);
            }
            catch { };

            // try with next format
            // 10/7/2015 23:17:26 PM
            // this one has a nice pattern, split with spaces to get date and time and then split each one to get individual date bits
            try
            {
                string[] splitDateTime = s.Split(' ');
                string[] date          = splitDateTime[0].Split('/');
                string[] time          = splitDateTime[1].Split(':');

                // create DateTime with these values
                // make sure it's UTC
                result = new System.DateTime(int.Parse(date[2]), int.Parse(date[0]), int.Parse(date[1]), int.Parse(time[0]), int.Parse(time[1]), int.Parse(time[2])).ToUniversalTime();

                // check for PM time and add 12H if required
                if (splitDateTime[2] == "PM")
                {
                    result = result.AddHours(12);
                }

                // we should have a valid DateTime now
                return(true);
            }
            catch { };

            // no joy parsing this one
            return(false);
        }
Ejemplo n.º 3
0
        private bool BoolMethod()
        {
            System.Type c    = GetType();
            string      name = Name;

            Name = name;
            System.DateTime cal = System.DateTime.Now;
            int             min = cal.Minute;

            cal.AddMonths(7 - cal.Month);
            cal.AddHours(12);
            return(base.ExistSimilarFieldAndMethod());
        }
        public void AddHours_ShouldMimicSystem()
        {
            var anyDateTimeTicks = 200;
            var anyHours = 5.5d;

            var sut = new Abstractions.DateTime(anyDateTimeTicks);
            var systemDateTime = new System.DateTime(anyDateTimeTicks);

            //  Act.
            var res = sut.AddHours(anyHours);

            //  Assert.
            var expectedResult = systemDateTime.AddHours(anyHours);
            AssertEquals(expectedResult, res);
        }
Ejemplo n.º 5
0
        private void Update_GMT_Zone_Date()
        {
            string text = "";

            System.DateTime temp = System.DateTime.UtcNow;
            temp = temp.AddHours((double)GMT_UTC);

            if (Flip_Format)
            {
                if (Show_GMT_UTC_value)
                {
                    if (GMT_UTC >= 0)
                    {
                        text += "(+" + GMT_UTC + ") ";
                    }
                    else
                    {
                        text += "(" + GMT_UTC + ") ";
                    }
                }
                text += temp.Year.ToString() + Padding;
                text += temp.Month.ToString() + Padding;
                text += temp.Day.ToString();
            }
            else
            {
                text += temp.Day.ToString() + Padding;
                text += temp.Month.ToString() + Padding;
                text += temp.Year.ToString();
                if (Show_GMT_UTC_value)
                {
                    if (GMT_UTC >= 0)
                    {
                        text += " (+" + GMT_UTC + ")";
                    }
                    else
                    {
                        text += " (" + GMT_UTC + ")";
                    }
                }
            }

            set_text(GMT_Zone_Time_Date, GMT_UTC_TimeZone_Date_MEM, text);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Retrieves the linker timestamp.
 /// </summary>
 /// <param name="filePath">The file path.</param>
 /// <returns></returns>
 /// <remarks>http://www.codinghorror.com/blog/2005/04/determining-build-date-the-hard-way.html</remarks>
 private static System.DateTime RetrieveLinkerTimestamp(string filePath)
 {
     const int peHeaderOffset = 60;
     const int linkerTimestampOffset = 8;
     var b = new byte[2048];
     System.IO.FileStream s = null;
     try
     {
         s = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
         s.Read(b, 0, 2048);
     }
     finally
     {
         if (s != null)
             s.Close();
     }
     var dt = new System.DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(System.BitConverter.ToInt32(b, System.BitConverter.ToInt32(b, peHeaderOffset) + linkerTimestampOffset));
     return dt.AddHours(System.TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Retrieves the linker timestamp.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        /// <remarks>http://www.codinghorror.com/blog/2005/04/determining-build-date-the-hard-way.html</remarks>
        private static System.DateTime RetrieveLinkerTimestamp(string filePath)
        {
            const int peHeaderOffset        = 60;
            const int linkerTimestampOffset = 8;
            var       b = new byte[2048];

            System.IO.FileStream s = null;
            try {
                s = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                s.Read(b, 0, 2048);
            } finally {
                if (s != null)
                {
                    s.Close();
                }
            }
            var dt = new System.DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(System.BitConverter.ToInt32(b, System.BitConverter.ToInt32(b, peHeaderOffset) + linkerTimestampOffset));

            return(dt.AddHours(System.TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours));
        }
Ejemplo n.º 8
0
        public Form1()
        {
            InitializeComponent();
            _1min = Properties.Settings.Default._1min;
            _5min = Properties.Settings.Default._5min;
            menuStrip.BackColor = System.Drawing.ColorTranslator.FromHtml("#88ffffff");
            horario = System.DateTime.Today;
            horario = horario.AddHours(Properties.Settings.Default.Hora); //Utiliza a configuração padrão de hora
            horario = horario.AddMinutes(Properties.Settings.Default.Minuto); //Utiliza a configuração padrão de minuto
            alterarFontes();
            lblRelogio.Text = System.DateTime.Now.ToString("HH:mm");
            timer1.Interval = 1000;
            timer1.Start();
            //Inicializa a localização dos audios
            _1minPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\resources\\mp3_1min.mp3";
            _5minPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\resources\\mp3_5min.mp3";
            if (!System.IO.File.Exists(_1minPath) || !System.IO.File.Exists(_5minPath))
                System.Windows.MessageBox.Show("Arquivos de áudio foram deletados ou corrompidos\nReinstale o programa para corrigir.", "Falha ao abrir áudios", System.Windows.MessageBoxButton.OK, MessageBoxImage.Error);

            configurarExibicao();
        }
Ejemplo n.º 9
0
        public Form1()
        {
            InitializeComponent();
            _1min = Properties.Settings.Default._1min;
            _5min = Properties.Settings.Default._5min;
            menuStrip.BackColor = System.Drawing.ColorTranslator.FromHtml("#88ffffff");
            horario             = System.DateTime.Today;
            horario             = horario.AddHours(Properties.Settings.Default.Hora);     //Utiliza a configuração padrão de hora
            horario             = horario.AddMinutes(Properties.Settings.Default.Minuto); //Utiliza a configuração padrão de minuto
            alterarFontes();
            lblRelogio.Text = System.DateTime.Now.ToString("HH:mm");
            timer1.Interval = 1000;
            timer1.Start();
            //Inicializa a localização dos audios
            _1minPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\resources\\mp3_1min.mp3";
            _5minPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\resources\\mp3_5min.mp3";
            if (!System.IO.File.Exists(_1minPath) || !System.IO.File.Exists(_5minPath))
            {
                System.Windows.MessageBox.Show("Arquivos de áudio foram deletados ou corrompidos\nReinstale o programa para corrigir.", "Falha ao abrir áudios", System.Windows.MessageBoxButton.OK, MessageBoxImage.Error);
            }

            configurarExibicao();
        }
Ejemplo n.º 10
0
        /// <summary>Generates a random DateTime value.</summary>
        /// <param name="random">The random generation algorithm.</param>
        /// <param name="min">The minimum allowed value of the random generation.</param>
        /// <param name="max">The maximum allowed value of the random generation.</param>
        /// <returns>A randomly generated DateTime value.</returns>
        public static System.DateTime DateTime(this System.Random random, System.DateTime min, System.DateTime max)
        {
            if (random == null)
            {
                throw new System.ArgumentNullException("random");
            }
            if (min > max)
            {
                throw new System.ArgumentException("!(min <= max)");
            }
            System.TimeSpan span      = max - min;
            int             day_range = span.Days;

            span -= System.TimeSpan.FromDays(day_range);
            int hour_range = span.Hours;

            span -= System.TimeSpan.FromHours(hour_range);
            int minute_range = span.Minutes;

            span -= System.TimeSpan.FromMinutes(minute_range);
            int second_range = span.Seconds;

            span -= System.TimeSpan.FromSeconds(second_range);
            int millisecond_range = span.Milliseconds;

            span -= System.TimeSpan.FromMilliseconds(millisecond_range);
            int tick_range = (int)span.Ticks;

            System.DateTime result = min;
            result.AddDays(random.Next(day_range));
            result.AddHours(random.Next(hour_range));
            result.AddMinutes(random.Next(minute_range));
            result.AddSeconds(random.Next(second_range));
            result.AddMilliseconds(random.Next(millisecond_range));
            result.AddTicks(random.Next(tick_range));
            return(result);
        }
Ejemplo n.º 11
0
 public IDateTime AddHours(double value)
 {
     return(new DateTime(backingDateTime.AddHours(value).Ticks));
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Converts a string value to a System.DateTime
        /// </summary>
        /// <param name="value">
        /// The date string.
        /// </param>
        /// <param name="format">
        /// The format where 103 = dd/MM/yyyy
        /// </param>
        /// <returns>
        /// A System.DateTime
        /// </returns>
        /// <exception cref="System.Exception">
        /// Exception is thrown if the given string value does not meet the expected format.
        /// </exception>
        public static System.DateTime ToDateTime(this string value, int format)
        {
            var result = new System.DateTime();

            switch (format)
            {
            case 103:
                // Validate
                if ((!string.IsNullOrEmpty(value)) && System.Text.RegularExpressions.Regex.IsMatch(value.Trim(), "^(((0[1-9]|[12]\\d|3[01])\\/(0[13578]|1[02])\\/((1[6-9]|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\/(0[13456789]|1[012])\\/((1[6-9]|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\/02\\/((1[6-9]|[2-9]\\d)\\d{2}))|(29\\/02\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"))
                {
                    // Build
                    var dateParts = value.Trim().Split(System.Convert.ToChar("/"));
                    result = result.AddYears(System.Convert.ToInt32(dateParts[2]) - 1);
                    result = result.AddMonths(System.Convert.ToInt32(dateParts[1]) - 1);
                    result = result.AddDays(System.Convert.ToInt32(dateParts[0]) - 1);
                }
                else
                {
                    throw new System.Exception($"String \"{value}\" is not of expected format \"{format}\".");
                }

                break;

            case -1:
                // Validate
                if ((!string.IsNullOrEmpty(value)) && value.Contains(" "))
                {
                    var strDate = value.Trim().Split(System.Convert.ToChar(" "));
                    if (strDate.Length == 2)
                    {
                        if (System.Text.RegularExpressions.Regex.IsMatch(strDate[0].Trim(), "^(((0[1-9]|[12]\\d|3[01])\\/(0[13578]|1[02])\\/((1[6-9]|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\/(0[13456789]|1[012])\\/((1[6-9]|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\/02\\/((1[6-9]|[2-9]\\d)\\d{2}))|(29\\/02\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"))
                        {
                            // Build
                            var dateParts = strDate[0].Trim().Split(System.Convert.ToChar("/"));
                            result = result.AddYears(System.Convert.ToInt32(dateParts[2]) - 1);
                            result = result.AddMonths(System.Convert.ToInt32(dateParts[1]) - 1);
                            result = result.AddDays(System.Convert.ToInt32(dateParts[0]) - 1);
                        }
                        else
                        {
                            throw new System.Exception($"String \"{value}\" is not of expected format \"{System.Convert.ToString(format)}\".");
                        }

                        if (System.Text.RegularExpressions.Regex.IsMatch(strDate[1].Trim(), "^([0-1][0-9]|[2][0-3]):([0-5][0-9])$"))
                        {
                            // Build
                            var timeParts = strDate[1].Trim().Split(System.Convert.ToChar(":"));
                            result = result.AddHours(System.Convert.ToInt32(timeParts[0]));
                            result = result.AddMinutes(System.Convert.ToInt32(timeParts[1]));
                        }
                        else
                        {
                            throw new System.Exception($"String \"{value}\" is not of expected format \"{System.Convert.ToString(format)}\".");
                        }
                    }
                    else
                    {
                        throw new System.Exception($"String \"{value}\" is not of expected format \"{System.Convert.ToString(format)}\".");
                    }
                }
                else
                {
                    throw new System.Exception($"String \"{value}\" is not of expected format \"{System.Convert.ToString(format)}\".");
                }

                break;
            }

            return(result);
        }