From cd21da040749925e3c03624da8d74809a82161c9 Mon Sep 17 00:00:00 2001 From: Maxime Mangel Date: Thu, 29 Feb 2024 23:06:12 +0100 Subject: [PATCH] [Python] Fix `DateTime` custom format `fffffff` and `FFFFFFF` --- src/fable-library-py/fable_library/date.py | 12 +++++++++-- tests/Python/TestDateTime.fs | 25 +++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/fable-library-py/fable_library/date.py b/src/fable-library-py/fable_library/date.py index 80fb40586e..06f425c22b 100644 --- a/src/fable-library-py/fable_library/date.py +++ b/src/fable-library-py/fable_library/date.py @@ -11,6 +11,7 @@ from .types import FSharpRef from .util import DateKind + # Some of the code in this file has been adapted from # https://github.com/microsoft/referencesource/blob/51cf7850defa8a17d815b4700b67116e3fa283c2/mscorlib/system/globalization/datetimeformat.cs @@ -238,7 +239,9 @@ def date_to_string_with_custom_format(date: datetime, format: str, utc: bool) -> precision = 10 ** (6 - token_length) result += str(localized_date.microsecond // precision).zfill(token_length) # Python datetime only support precision up to the microsecond - # so we can't support fffffff + # so we need to fill the rest with 0 + case 7: + result += str(localized_date.microsecond).zfill(6).ljust(token_length, "0") case _: pass case "F": @@ -251,7 +254,12 @@ def date_to_string_with_custom_format(date: datetime, format: str, utc: bool) -> if value != 0: result += str(value).zfill(token_length) # Python datetime only support precision up to the microsecond - # so we can't support FFFFFFF + # so we can't go further + # We alse need to pad start with 0 if the value is not 0 + case 7: + value = localized_date.microsecond + if value != 0: + result += str(value).zfill(6).rstrip("0") case _: pass case "g": diff --git a/tests/Python/TestDateTime.fs b/tests/Python/TestDateTime.fs index ad2770cd26..19e0df2515 100644 --- a/tests/Python/TestDateTime.fs +++ b/tests/Python/TestDateTime.fs @@ -98,9 +98,13 @@ let ``test DateTime.ToString with custom format works`` () = |> equal "r 617542" DateTime.Parse("2009-06-15T13:45:30.0000005").ToString("r ffffff", CultureInfo.InvariantCulture) |> equal "r 000000" - // We only have a precision up to the microsecond - // DateTime.Parse("2009-06-15T13:45:30.6175425").ToString("r fffffff", CultureInfo.InvariantCulture) - // |> equal "r 6175425" + // We only have a precision up to the microsecond so we fill with 0 + DateTime.Parse("2009-06-15T13:45:30.617542").ToString("r fffffff", CultureInfo.InvariantCulture) + |> equal "r 6175420" + DateTime.Parse("2009-06-15T13:45:30.1").ToString("r fffffff", CultureInfo.InvariantCulture) + |> equal "r 1000000" + DateTime.Parse("2009-06-15T13:45:30.000001").ToString("r fffffff", CultureInfo.InvariantCulture) + |> equal "r 0000010" DateTime.Parse("2009-06-15T13:45:30.6175425").ToString("r F", CultureInfo.InvariantCulture) |> equal "r 6" @@ -127,8 +131,19 @@ let ``test DateTime.ToString with custom format works`` () = DateTime.Parse("2009-06-15T13:45:30.0000005").ToString("r FFFFFF", CultureInfo.InvariantCulture) |> equal "r " // We only have a precision up to the microsecond - // DateTime.Parse("2009-06-15T13:45:30.6175425").ToString("r FFFFFFF", CultureInfo.InvariantCulture) - // |> equal "r 6175425" + // Check that we fill the zero correctly and remove trailing zeros + DateTime.Parse("2009-06-15T13:45:30.617542").ToString("r FFFFFFF", CultureInfo.InvariantCulture) + |> equal "r 617542" + + DateTime.Parse("2009-06-15T13:45:30.01").ToString("r FFFFFFF", CultureInfo.InvariantCulture) + |> equal "r 01" + DateTime.Parse("2009-06-15T13:45:30.000001").ToString("r FFFFFFF", CultureInfo.InvariantCulture) + |> equal "r 000001" + DateTime.Parse("2009-06-15T13:45:30.0000000").ToString("r FFFFFFF", CultureInfo.InvariantCulture) + |> equal "r " + + DateTime.Parse("2009-06-15T13:45:30.00617").ToString("r FFFFFFF", CultureInfo.InvariantCulture) + |> equal "r 00617" DateTime(2014, 7, 1, 16, 37, 0).ToString("r g", CultureInfo.InvariantCulture) |> equal "r A.D."