Beispiel #1
0
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            CultureInfo        polandCulture = new CultureInfo("pl-PL");
            DateTimeFormatInfo formatInfo    = CultureInfo.CurrentCulture.DateTimeFormat;
            TimeOnly           timeOnly      = new TimeOnly(10, 30, 50);

            yield return(ConvertTest.Valid(timeOnly, timeOnly.ToString(formatInfo.ShortTimePattern)));

            yield return(ConvertTest.Valid(timeOnly, timeOnly.ToString(polandCulture.DateTimeFormat.ShortTimePattern, polandCulture.DateTimeFormat))
                         .WithRemoteInvokeCulture(polandCulture));

            yield return(ConvertTest.Valid(timeOnly, "10:30", CultureInfo.InvariantCulture));


            yield return(ConvertTest.Valid(TimeOnly.MinValue, string.Empty));

            yield return(ConvertTest.Valid(
                             new TimeOnly(),
                             new InstanceDescriptor(typeof(TimeOnly).GetConstructor(new Type[] { typeof(long) }), new object[] { (long)0 })
                             ));

            yield return(ConvertTest.Valid(
                             timeOnly,
                             new InstanceDescriptor(
                                 typeof(TimeOnly).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }),
                                 new object[] { 10, 30, 50, 0, 0 }
                                 )
                             ));

            yield return(ConvertTest.CantConvertTo(new TimeOnly(), typeof(TimeOnly)));

            yield return(ConvertTest.CantConvertTo(new TimeOnly(), typeof(int)));
        }
Beispiel #2
0
        public static void OAndRFormatsTest()
        {
            TimeOnly timeOnly      = TimeOnly.FromDateTime(DateTime.Today);
            string   formattedDate = timeOnly.ToString("o");

            Assert.Equal(16, formattedDate.Length);
            Assert.Equal(':', formattedDate[2]);
            Assert.Equal(':', formattedDate[5]);
            Assert.Equal('.', formattedDate[8]);
            TimeOnly parsedTimeOnly = TimeOnly.Parse(formattedDate);

            Assert.Equal(timeOnly, parsedTimeOnly);
            parsedTimeOnly = TimeOnly.Parse(formattedDate.AsSpan());
            Assert.Equal(timeOnly, parsedTimeOnly);
            parsedTimeOnly = TimeOnly.ParseExact(formattedDate, "O");
            Assert.Equal(timeOnly, parsedTimeOnly);
            parsedTimeOnly = TimeOnly.ParseExact(formattedDate.AsSpan(), "O".AsSpan());
            Assert.Equal(timeOnly, parsedTimeOnly);

            formattedDate = timeOnly.ToString("r");
            Assert.Equal(8, formattedDate.Length);
            Assert.Equal(':', formattedDate[2]);
            Assert.Equal(':', formattedDate[5]);
            parsedTimeOnly = TimeOnly.Parse(formattedDate);
            Assert.Equal(timeOnly, parsedTimeOnly);
            parsedTimeOnly = TimeOnly.Parse(formattedDate.AsSpan());
            Assert.Equal(timeOnly, parsedTimeOnly);
            parsedTimeOnly = TimeOnly.ParseExact(formattedDate, "R");
            Assert.Equal(timeOnly, parsedTimeOnly);
            parsedTimeOnly = TimeOnly.ParseExact(formattedDate.AsSpan(), "R".AsSpan());
            Assert.Equal(timeOnly, parsedTimeOnly);
        }
Beispiel #3
0
        public static void CustomFormattingTest()
        {
            TimeOnly timeOnly       = TimeOnly.FromDateTime(DateTime.Now);
            string   format         = "HH 'dash' mm \"dash\" ss'....'fffffff";
            string   formatted      = timeOnly.ToString(format);
            TimeOnly parsedTimeOnly = TimeOnly.ParseExact(formatted, format);

            Assert.Equal(timeOnly, parsedTimeOnly);
            parsedTimeOnly = TimeOnly.ParseExact(formatted.AsSpan(), format.AsSpan());
            Assert.Equal(timeOnly, parsedTimeOnly);

            Assert.Throws <FormatException>(() => timeOnly.ToString("hh:mm:ss dd"));
            Assert.Throws <FormatException>(() => timeOnly.ToString("hh:mm:ss MM"));
            Assert.Throws <FormatException>(() => timeOnly.ToString("hh:mm:ss yy"));
        }
Beispiel #4
0
        public static void BasicFormatParseTest()
        {
            string   pattern        = "hh:mm:ss tt";
            DateTime dt             = DateTime.Now;
            TimeOnly timeOnly       = new TimeOnly(dt.Hour, dt.Minute, dt.Second);
            string   s              = timeOnly.ToString(pattern);
            TimeOnly parsedTimeOnly = TimeOnly.Parse(s);

            Assert.True(TimeOnly.TryParse(s, out TimeOnly parsedTimeOnly1));
            Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly.Hour % 12);
            Assert.Equal(timeOnly.Minute, parsedTimeOnly.Minute);
            Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly1.Hour % 12);
            Assert.Equal(timeOnly.Minute, parsedTimeOnly1.Minute);
            parsedTimeOnly = TimeOnly.Parse(s.AsSpan());
            Assert.True(TimeOnly.TryParse(s.AsSpan(), out parsedTimeOnly1));
            Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly.Hour % 12);
            Assert.Equal(timeOnly.Minute, parsedTimeOnly.Minute);
            Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly1.Hour % 12);
            Assert.Equal(timeOnly.Minute, parsedTimeOnly1.Minute);

            s = timeOnly.ToString(pattern, CultureInfo.InvariantCulture);
            parsedTimeOnly = TimeOnly.Parse(s, CultureInfo.InvariantCulture);
            Assert.True(TimeOnly.TryParse(s.AsSpan(), CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedTimeOnly1));
            Assert.Equal(timeOnly, parsedTimeOnly);
            Assert.Equal(timeOnly, parsedTimeOnly1);
            parsedTimeOnly = TimeOnly.Parse(s.AsSpan(), CultureInfo.InvariantCulture);
            Assert.True(TimeOnly.TryParse(s.AsSpan(), CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedTimeOnly1));
            Assert.Equal(parsedTimeOnly, parsedTimeOnly1);

            Assert.False(TimeOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out parsedTimeOnly1));
            AssertExtensions.Throws <ArgumentException>("style", () => TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal));
            Assert.False(TimeOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out parsedTimeOnly1));
            AssertExtensions.Throws <ArgumentException>("style", () => TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal));
            Assert.False(TimeOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out parsedTimeOnly1));
            AssertExtensions.Throws <ArgumentException>("style", () => TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal));
            Assert.False(TimeOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out parsedTimeOnly1));
            AssertExtensions.Throws <ArgumentException>("style", () => TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault));

            s = "     " + s + "     ";
            parsedTimeOnly = TimeOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces);
            Assert.Equal(timeOnly, parsedTimeOnly);
            parsedTimeOnly = TimeOnly.Parse(s.AsSpan(), CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces);
            Assert.Equal(timeOnly, parsedTimeOnly);
        }
Beispiel #5
0
    public void StringConversionToTimeOnly()
    {
        var    time   = new TimeOnly(1, 10, 10);
        string source = time.ToString("O", DateTimeFormatInfo.InvariantInfo);
        var    actual = ValueConverter.ConvertTo(source, typeof(TimeOnly));

        actual.ShouldNotBeNull();
        actual.ShouldBeOfType <TimeOnly>();
        actual.ShouldBe(time);
    }
Beispiel #6
0
 /// <inheritdoc/>
 protected override string FormatValueAsString(TValue value)
 {
     return(value switch
     {
         null => null,
         TimeSpan timeSpan => timeSpan.ToString(Parsers.InternalTimeFormat.ToLowerInvariant()),
         TimeOnly timeOnly => timeOnly.ToString(Parsers.InternalTimeFormat.ToLowerInvariant()),
         DateTime datetime => datetime.ToString(Parsers.InternalTimeFormat),
         _ => throw new InvalidOperationException($"Unsupported type {value.GetType()}"),
     });
Beispiel #7
0
        public static void AllCulturesTest()
        {
            TimeOnly timeOnly = new TimeOnly((DateTime.Now.TimeOfDay.Ticks / TimeSpan.TicksPerMinute) * TimeSpan.TicksPerMinute);

            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
            {
                if (string.IsNullOrEmpty(ci.DateTimeFormat.TimeSeparator))
                {
                    // cannot parse concatenated time part numbers.
                    continue;
                }

                string   formatted      = timeOnly.ToString("t", ci);
                TimeOnly parsedTimeOnly = TimeOnly.ParseExact(formatted, "t", ci);
                Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly.Hour % 12);
                Assert.Equal(timeOnly.Minute, parsedTimeOnly.Minute);

                formatted      = timeOnly.ToString("T", ci);
                parsedTimeOnly = TimeOnly.ParseExact(formatted, "T", ci);
                Assert.Equal(timeOnly.Hour % 12, parsedTimeOnly.Hour % 12);
                Assert.Equal(timeOnly.Minute, parsedTimeOnly.Minute);
            }
        }
Beispiel #8
0
        public void CanArriveAtQueryStringPageWithDateTimeQuery()
        {
            var dateTime = new DateTime(2000, 1, 2, 3, 4, 5, 6);
            var dateOnly = new DateOnly(2000, 1, 2);
            var timeOnly = new TimeOnly(3, 4, 5, 6);

            SetUrlViaPushState($"/WithQueryParameters/Abc?NullableDateTimeValue=2000-01-02%2003:04:05&NullableDateOnlyValue=2000-01-02&NullableTimeOnlyValue=03:04:05");

            var app = Browser.MountTestComponent <TestRouter>();

            Assert.Equal("Hello Abc .", app.FindElement(By.Id("test-info")).Text);
            Assert.Equal("0", app.FindElement(By.Id("value-QueryInt")).Text);
            Assert.Equal(dateTime.ToString("hh:mm:ss on yyyy-MM-dd", CultureInfo.InvariantCulture), app.FindElement(By.Id("value-NullableDateTimeValue")).Text);
            Assert.Equal(dateOnly.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), app.FindElement(By.Id("value-NullableDateOnlyValue")).Text);
            Assert.Equal(timeOnly.ToString("hh:mm:ss", CultureInfo.InvariantCulture), app.FindElement(By.Id("value-NullableTimeOnlyValue")).Text);
            Assert.Equal(string.Empty, app.FindElement(By.Id("value-StringValue")).Text);
            Assert.Equal("0 values ()", app.FindElement(By.Id("value-LongValues")).Text);

            AssertHighlightedLinks("With query parameters (none)", "With query parameters (passing Date Time values)");
        }
        public static string ToSettingsString(this TimeOnly time, DateTimeSettings settings)
        {
            string hourFormatString;
            string amPmFormatString;

            if (settings.TimeFormat == TimeFormat.Hour24)
            {
                hourFormatString = "HH";
                amPmFormatString = string.Empty;
            }
            else // 12 hour is the default.
            {
                hourFormatString = "hh";
                amPmFormatString = " tt";
            }

            string timeFormatString = $"{hourFormatString}:mm{amPmFormatString}";

            return(time.ToString(timeFormatString));
        }
Beispiel #10
0
        public static void ConvertFrom_TimeOnlyInstanceDescriptor()
        {
            using (new ThreadCultureChange("fr-FR"))
            {
                TimeOnly        testTimeOnly = TimeOnly.FromDateTime(DateTime.UtcNow);
                ConstructorInfo ctor         = typeof(TimeOnly).GetConstructor(new Type[]
                {
                    typeof(int), typeof(int), typeof(int), typeof(int), typeof(int)
                });

                InstanceDescriptor descriptor = new InstanceDescriptor(ctor, new object[]
                {
                    testTimeOnly.Hour, testTimeOnly.Minute, testTimeOnly.Second, testTimeOnly.Millisecond, testTimeOnly.Microsecond
                });

                const string format = "HH mm ss fff tt";
                object       o      = s_converter.ConvertFrom(descriptor);
                Assert.Equal(testTimeOnly.ToString(format), ((TimeOnly)o).ToString(format));
            }
        }
Beispiel #11
0
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            TimeOnly timeOnly = new TimeOnly(10, 30, 50);

            yield return(ConvertTest.Valid("", TimeOnly.MinValue));

            yield return(ConvertTest.Valid("    ", TimeOnly.MinValue));

            yield return(ConvertTest.Valid(timeOnly.ToString(), TimeOnly.Parse(timeOnly.ToString())));

            yield return(ConvertTest.Valid(timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat),
                                           TimeOnly.Parse(timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat))));

            yield return(ConvertTest.Valid(" " + timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat) + " ",
                                           TimeOnly.Parse(timeOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat))));

            yield return(ConvertTest.Throws <FormatException>("invalid"));

            yield return(ConvertTest.CantConvertFrom(new object()));

            yield return(ConvertTest.CantConvertFrom(1));
        }
Beispiel #12
0
        public static void FormatParseTest()
        {
            string[] patterns = new string[] { CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern, CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern, "t", "T", "o", "r" };

            TimeOnly timeOnly = TimeOnly.FromDateTime(DateTime.Now);

            foreach (string format in patterns)
            {
                string formattedTime = timeOnly.ToString(format);
                timeOnly = TimeOnly.Parse(formattedTime);

                Assert.True(TimeOnly.TryParse(formattedTime, out TimeOnly parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly1);
                TimeOnly parsedTimeOnly = TimeOnly.Parse(formattedTime.AsSpan());
                Assert.True(TimeOnly.TryParse(formattedTime.AsSpan(), out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);

                parsedTimeOnly = TimeOnly.Parse(formattedTime, CultureInfo.CurrentCulture);
                Assert.True(TimeOnly.TryParse(formattedTime, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);
                parsedTimeOnly = TimeOnly.Parse(formattedTime.AsSpan(), CultureInfo.CurrentCulture);
                Assert.True(TimeOnly.TryParse(formattedTime.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);

                parsedTimeOnly = TimeOnly.ParseExact(formattedTime, format);
                Assert.True(TimeOnly.TryParseExact(formattedTime, format, out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);
                parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), format.AsSpan());
                Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), format.AsSpan(), out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);

                parsedTimeOnly = TimeOnly.ParseExact(formattedTime, format, CultureInfo.CurrentCulture);
                Assert.True(TimeOnly.TryParseExact(formattedTime, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);
                parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture);
                Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);

                parsedTimeOnly = TimeOnly.ParseExact(formattedTime, patterns);
                Assert.True(TimeOnly.TryParseExact(formattedTime, patterns, out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);
                parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), patterns);
                Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), patterns, out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);

                parsedTimeOnly = TimeOnly.ParseExact(formattedTime, patterns, CultureInfo.CurrentCulture);
                Assert.True(TimeOnly.TryParseExact(formattedTime, patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);
                parsedTimeOnly = TimeOnly.ParseExact(formattedTime.AsSpan(), patterns, CultureInfo.CurrentCulture);
                Assert.True(TimeOnly.TryParseExact(formattedTime.AsSpan(), patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedTimeOnly1));
                Assert.Equal(timeOnly, parsedTimeOnly);
                Assert.Equal(timeOnly, parsedTimeOnly1);
            }
        }
Beispiel #13
0
        public void WriteValue(TimeOnly to)
        {
            var value = to.ToString(DefaultFormat.TimeOnlyFormatToWrite);

            _manualBlittableJsonDocumentBuilder.WriteValue(value);
        }
Beispiel #14
0
 public string GetTime(TimeOnly time) => time.ToString();