Exemplo n.º 1
0
        public static void OAndRFormatsTest()
        {
            DateOnly dateOnly      = DateOnly.FromDateTime(DateTime.Today);
            string   formattedDate = dateOnly.ToString("o");

            Assert.Equal(10, formattedDate.Length);
            Assert.Equal('-', formattedDate[4]);
            Assert.Equal('-', formattedDate[7]);
            DateOnly parsedDateOnly = DateOnly.Parse(formattedDate);

            Assert.Equal(dateOnly, parsedDateOnly);
            parsedDateOnly = DateOnly.Parse(formattedDate.AsSpan());
            Assert.Equal(dateOnly, parsedDateOnly);
            parsedDateOnly = DateOnly.ParseExact(formattedDate, "O");
            Assert.Equal(dateOnly, parsedDateOnly);
            parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), "O".AsSpan());
            Assert.Equal(dateOnly, parsedDateOnly);

            formattedDate = dateOnly.ToString("r");
            Assert.Equal(16, formattedDate.Length);
            Assert.Equal(',', formattedDate[3]);
            Assert.Equal(' ', formattedDate[4]);
            Assert.Equal(' ', formattedDate[7]);
            Assert.Equal(' ', formattedDate[11]);
            parsedDateOnly = DateOnly.Parse(formattedDate);
            Assert.Equal(dateOnly, parsedDateOnly);
            parsedDateOnly = DateOnly.Parse(formattedDate.AsSpan());
            Assert.Equal(dateOnly, parsedDateOnly);
            parsedDateOnly = DateOnly.ParseExact(formattedDate, "R");
            Assert.Equal(dateOnly, parsedDateOnly);
            parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), "R".AsSpan());
            Assert.Equal(dateOnly, parsedDateOnly);
        }
Exemplo n.º 2
0
        public override IEnumerable <ConvertTest> ConvertToTestData()
        {
            CultureInfo        polandCulture = new CultureInfo("pl-PL");
            DateTimeFormatInfo formatInfo    = CultureInfo.CurrentCulture.DateTimeFormat;
            DateOnly           dateOnly      = new DateOnly(1998, 12, 5);

            yield return(ConvertTest.Valid(dateOnly, dateOnly.ToString(formatInfo.ShortDatePattern)));

            yield return(ConvertTest.Valid(dateOnly, dateOnly.ToString(polandCulture.DateTimeFormat.ShortDatePattern, polandCulture.DateTimeFormat))
                         .WithRemoteInvokeCulture(polandCulture));

            yield return(ConvertTest.Valid(dateOnly, "1998-12-05", CultureInfo.InvariantCulture)
                         .WithRemoteInvokeCulture(polandCulture));

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

            yield return(ConvertTest.Valid(
                             dateOnly,
                             new InstanceDescriptor(
                                 typeof(DateOnly).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int) }),
                                 new object[] { 1998, 12, 5 }
                                 )
                             ));

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

            yield return(ConvertTest.CantConvertTo(new DateOnly(), typeof(int)));
        }
Exemplo n.º 3
0
        private static async Task <byte[]> DownloadReport(DateOnly reportDate)
        {
            var client = new HttpClient();
            //var pdfFile = $"stockQuotes_{reportDate:MMddyyyy}.pdf";
            //new file name: August 24, 2021-EOD1.pdf
            var pdfFile     = reportDate.ToString("MMMM dd, yyyy") + "-EOD.pdf";// "August 24, 2021-EOD1.pdf";
            var downloadUrl = Path.Combine(pseGetOption.SourceUrl, pdfFile);

            Log.Information($"Downloading {downloadUrl}...");

            var response = await client.GetAsync(downloadUrl);

            switch (response.StatusCode)
            {
            case HttpStatusCode.NotFound:
                Log.Information($"I can't find {downloadUrl}. Trying again...");
                pdfFile     = reportDate.ToString("MMMM dd, yyyy") + "-EOD1.pdf";// "August 24, 2021-EOD1.pdf";
                downloadUrl = Path.Combine(pseGetOption.SourceUrl, pdfFile);

                Log.Information($"Downloading {downloadUrl}...");
                break;

            case HttpStatusCode.OK:
                return(await response.Content.ReadAsByteArrayAsync());
            }

            Log.Warning(response.ReasonPhrase);
            return(null);
        }
Exemplo n.º 4
0
        public static void AllCulturesTest()
        {
            DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Today);

            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
            {
                string   formatted      = dateOnly.ToString("d", ci);
                DateOnly parsedDateOnly = DateOnly.ParseExact(formatted, "d", ci);
                Assert.Equal(dateOnly, parsedDateOnly);

                formatted      = dateOnly.ToString("D", ci);
                parsedDateOnly = DateOnly.ParseExact(formatted, "D", ci);
                Assert.Equal(dateOnly, parsedDateOnly);
            }
        }
        public async void TestQueryDateOnlyString()
        {
            DateOnly d = new DateOnly(2008, 12, 18);
            var      r = await api.QueryDateOnlyAsStringAsync(d.ToString("O"));

            Assert.Equal(d, r);
        }
Exemplo n.º 6
0
        public static void CustomFormattingTest()
        {
            DateOnly dateOnly       = DateOnly.FromDateTime(DateTime.Today);
            string   format         = "dd, ddd 'dash' MMMM \"dash\" yyyy";
            string   formatted      = dateOnly.ToString(format);
            DateOnly parsedDateOnly = DateOnly.ParseExact(formatted, format);

            Assert.Equal(dateOnly, parsedDateOnly);
            parsedDateOnly = DateOnly.ParseExact(formatted.AsSpan(), format.AsSpan());
            Assert.Equal(dateOnly, parsedDateOnly);

            Assert.Throws <FormatException>(() => dateOnly.ToString("dd-MM-yyyy hh"));
            Assert.Throws <FormatException>(() => dateOnly.ToString("dd-MM-yyyy m"));
            Assert.Throws <FormatException>(() => dateOnly.ToString("dd-MM-yyyy s"));
            Assert.Throws <FormatException>(() => dateOnly.ToString("dd-MM-yyyy z"));
        }
Exemplo n.º 7
0
 /// <summary>
 /// Formats the supplied value to it's valid string representation.
 /// </summary>
 /// <param name="value">Value to format.</param>
 /// <param name="dateFormat">Date format.</param>
 /// <returns>Returns value formatted as string.</returns>
 public static string FormatDateValueAsString <TValue>(TValue value, string dateFormat)
 {
     return(value switch
     {
         null => null,
         DateTime datetime => datetime.ToString(dateFormat),
         DateTimeOffset datetimeOffset => datetimeOffset.ToString(dateFormat),
         DateOnly dateOnly => dateOnly.ToString(dateFormat),
         _ => throw new InvalidOperationException($"Unsupported type {value.GetType()}"),
     });
Exemplo n.º 8
0
    public void StringConversionToDateOnly()
    {
        var    date   = new DateOnly(2000, 10, 10);
        string source = date.ToString("O", DateTimeFormatInfo.InvariantInfo);
        var    actual = ValueConverter.ConvertTo(source, typeof(DateOnly));

        actual.ShouldNotBeNull();
        actual.ShouldBeOfType <DateOnly>();
        actual.ShouldBe(date);
    }
Exemplo n.º 9
0
        public override IEnumerable <ConvertTest> ConvertFromTestData()
        {
            DateOnly dateOnly = new DateOnly(1998, 12, 5);

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

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

            yield return(ConvertTest.Valid(dateOnly.ToString(), dateOnly));

            yield return(ConvertTest.Valid(dateOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat), dateOnly, CultureInfo.InvariantCulture));

            yield return(ConvertTest.Valid(" " + dateOnly.ToString(CultureInfo.InvariantCulture.DateTimeFormat) + " ", dateOnly, CultureInfo.InvariantCulture));

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

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

            yield return(ConvertTest.CantConvertFrom(1));
        }
Exemplo n.º 10
0
        public static void BasicFormatParseTest()
        {
            DateOnly dateOnly       = DateOnly.FromDateTime(DateTime.Today);
            string   s              = dateOnly.ToString();
            DateOnly parsedDateOnly = DateOnly.Parse(s);

            Assert.True(DateOnly.TryParse(s, out DateOnly parsedDateOnly1));
            Assert.Equal(dateOnly, parsedDateOnly);
            Assert.Equal(dateOnly, parsedDateOnly1);
            parsedDateOnly = DateOnly.Parse(s.AsSpan());
            Assert.True(DateOnly.TryParse(s.AsSpan(), out parsedDateOnly1));
            Assert.Equal(dateOnly, parsedDateOnly);
            Assert.Equal(dateOnly, parsedDateOnly1);

            s = dateOnly.ToString(CultureInfo.InvariantCulture);
            parsedDateOnly = DateOnly.Parse(s, CultureInfo.InvariantCulture);
            Assert.True(DateOnly.TryParse(s.AsSpan(), CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDateOnly1));
            Assert.Equal(dateOnly, parsedDateOnly);
            Assert.Equal(dateOnly, parsedDateOnly1);
            parsedDateOnly = DateOnly.Parse(s.AsSpan(), CultureInfo.InvariantCulture);
            Assert.True(DateOnly.TryParse(s.AsSpan(), CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDateOnly1));
            Assert.Equal(dateOnly, parsedDateOnly);
            Assert.Equal(dateOnly, parsedDateOnly1);

            Assert.False(DateOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out parsedDateOnly1));
            Assert.Throws <ArgumentException>(() => DateOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal));
            Assert.False(DateOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out parsedDateOnly1));
            Assert.Throws <ArgumentException>(() => DateOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal));
            Assert.False(DateOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out parsedDateOnly1));
            Assert.Throws <ArgumentException>(() => DateOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal));
            Assert.False(DateOnly.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out parsedDateOnly1));
            Assert.Throws <ArgumentException>(() => DateOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault));

            s = "     " + s + "     ";
            parsedDateOnly = DateOnly.Parse(s, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces);
            Assert.Equal(dateOnly, parsedDateOnly);
            parsedDateOnly = DateOnly.Parse(s.AsSpan(), CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces);
            Assert.Equal(dateOnly, parsedDateOnly);
        }
Exemplo n.º 11
0
        public static void ConvertFrom_DateOnlyInstanceDescriptor()
        {
            using (new ThreadCultureChange("fr-FR"))
            {
                DateOnly        testDateOnly = DateOnly.FromDateTime(DateTime.UtcNow);
                ConstructorInfo ctor         = typeof(DateOnly).GetConstructor(new Type[]
                {
                    typeof(int), typeof(int), typeof(int)
                });

                InstanceDescriptor descriptor = new InstanceDescriptor(ctor, new object[]
                {
                    testDateOnly.Year, testDateOnly.Month, testDateOnly.Day
                });

                const string format = "dd MMM yyyy";
                object       o      = s_converter.ConvertFrom(descriptor);
                Assert.Equal(testDateOnly.ToString(format), ((DateOnly)o).ToString(format));
            }
        }
Exemplo n.º 12
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)");
        }
Exemplo n.º 13
0
 public static string FormatValue(DateOnly?value, CultureInfo culture = null) => value?.ToString(culture ?? CultureInfo.CurrentCulture);
Exemplo n.º 14
0
 /// <summary>
 /// Get formatted time string as a korean.
 /// </summary>
 public static string GetFormattedKoreanString(this DateOnly date)
 {
     return(date.ToString("D", new System.Globalization.CultureInfo("ko-KR")));
 }
        public override string Convert(DateOnly date)
        {
            var day = date.Day > 1 ? date.Day.ToString() : date.Day.Ordinalize();

            return(day + date.ToString(" MMMM yyyy"));
        }
Exemplo n.º 16
0
 public string GetDate(DateOnly date) => date.ToString();
Exemplo n.º 17
0
        public static void FormatParseTest()
        {
            string[] patterns = new string[] { CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern, "d", "D", "o", "r" };

            DateOnly dateOnly = DateOnly.FromDateTime(DateTime.Today);

            foreach (string format in patterns)
            {
                string   formattedDate  = dateOnly.ToString(format);
                DateOnly parsedDateOnly = DateOnly.Parse(formattedDate);
                Assert.True(DateOnly.TryParse(formattedDate, out DateOnly parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);
                parsedDateOnly = DateOnly.Parse(formattedDate.AsSpan());
                Assert.True(DateOnly.TryParse(formattedDate.AsSpan(), out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);

                parsedDateOnly = DateOnly.Parse(formattedDate, CultureInfo.CurrentCulture);
                Assert.True(DateOnly.TryParse(formattedDate, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);
                parsedDateOnly = DateOnly.Parse(formattedDate.AsSpan(), CultureInfo.CurrentCulture);
                Assert.True(DateOnly.TryParse(formattedDate.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);

                parsedDateOnly = DateOnly.ParseExact(formattedDate, format);
                Assert.True(DateOnly.TryParseExact(formattedDate, format, out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);
                parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), format.AsSpan());
                Assert.True(DateOnly.TryParseExact(formattedDate.AsSpan(), format.AsSpan(), out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);

                parsedDateOnly = DateOnly.ParseExact(formattedDate, format, CultureInfo.CurrentCulture);
                Assert.True(DateOnly.TryParseExact(formattedDate, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);
                parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture);
                Assert.True(DateOnly.TryParseExact(formattedDate.AsSpan(), format.AsSpan(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);

                parsedDateOnly = DateOnly.ParseExact(formattedDate, patterns);
                Assert.True(DateOnly.TryParseExact(formattedDate, patterns, out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);
                parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), patterns);
                Assert.True(DateOnly.TryParseExact(formattedDate.AsSpan(), patterns, out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);

                parsedDateOnly = DateOnly.ParseExact(formattedDate, patterns, CultureInfo.CurrentCulture);
                Assert.True(DateOnly.TryParseExact(formattedDate, patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);
                parsedDateOnly = DateOnly.ParseExact(formattedDate.AsSpan(), patterns, CultureInfo.CurrentCulture);
                Assert.True(DateOnly.TryParseExact(formattedDate.AsSpan(), patterns, CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDateOnly1));
                Assert.Equal(dateOnly, parsedDateOnly);
                Assert.Equal(dateOnly, parsedDateOnly1);
            }
        }
Exemplo n.º 18
0
 public override string Convert(DateOnly date)
 {
     return(date.ToString("MMMM ") + date.Day.Ordinalize() + date.ToString(", yyyy"));
 }
 public virtual string Convert(DateOnly date)
 {
     return(date.Day.Ordinalize() + date.ToString(" MMMM yyyy"));
 }
Exemplo n.º 20
0
        public void SetNullableDateOnly()
        {
            // setup
            var dateonly   = new DateOnly(2020, 4, 13);
            var dateOutput = @"<span id=""nullable-date-only-event-initially-null-result"">" + dateonly.ToString().Replace("+", "&#x2B;") + "</span>";
            var comp       = RenderComponent <DatePickerComponent>();

            // test
            comp.Instance.NullableDateOnlyValue = dateonly;
            comp.Render();

            // validate
            Assert.Contains(dateOutput, comp.Markup);
        }
Exemplo n.º 21
0
        public void RenderDateOnlyTest()
        {
            // setup
            var defDate        = new DateOnly();
            var dateOpen       = "<input";
            var dateClose      = "</input>";
            var dateType       = @"type=""text""";
            var dateOutput     = @"<span id=""date-only-event-initially-undefined-result"">" + defDate.ToString().Replace("+", "&#x2B;") + "</span>";
            var nullableOutput = @"<span id=""nullable-date-only-event-initially-null-result""></span>";

            // test
            var comp = RenderComponent <DatePickerComponent>();

            // validate
            Assert.Contains(dateOpen, comp.Markup);
            Assert.Contains(dateClose, comp.Markup);
            Assert.Contains(dateType, comp.Markup);
            Assert.Contains(dateOutput, comp.Markup);
            Assert.NotNull(comp.Find("#date-only-event-initially-undefined"));
            Assert.NotNull(comp.Find("#date-only-control"));
            Assert.NotNull(comp.Find("#date-only-event-initially-undefined-result"));

            Assert.Contains(nullableOutput, comp.Markup);
            Assert.NotNull(comp.Find("#nullable-date-only-event-initially-null"));
            Assert.NotNull(comp.Find("#nullable-date-only-control"));
            Assert.NotNull(comp.Find("#nullable-date-only-event-initially-null-result"));
        }
Exemplo n.º 22
0
 public static string yyyyMMdd(this DateOnly thisDate) => thisDate.ToString("yyyyMMdd");
Exemplo n.º 23
0
        public void WriteValue(DateOnly @do)
        {
            var value = @do.ToString(DefaultFormat.DateOnlyFormatToWrite);

            _manualBlittableJsonDocumentBuilder.WriteValue(value);
        }
Exemplo n.º 24
0
 public static string ToIsoString(this DateOnly thisDate) => thisDate.ToString("yyyy-MM-dd");