Skip to content

Commit

Permalink
[Python] Fix DateTime custom format fffffff and FFFFFFF
Browse files Browse the repository at this point in the history
  • Loading branch information
MangelMaxime committed Feb 29, 2024
1 parent 8d85753 commit cd21da0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/fable-library-py/fable_library/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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":
Expand All @@ -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":
Expand Down
25 changes: 20 additions & 5 deletions tests/Python/TestDateTime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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."
Expand Down

0 comments on commit cd21da0

Please sign in to comment.