Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to the Datetime support on Python #3600

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Fix #3584: Unit type compiles to undeclared variable (by @ncave)

#### Python

* Support `DateTime(..., DateTimeKind.Utc).ToString("O")` (by @MangelMaxime)

#### Rust

* Added `Guid.TryParse`, `Guid.ToByteArray` (by @ncave)
Expand All @@ -27,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#### Python

* Fixed char to string type regression with binary operator (by @dbrattli)
* Fix `DateTime(..., DateTimeKind.Local).ToString("O")` (by @MangelMaxime)

## 4.5.0 - 2023-11-07

Expand Down
15 changes: 10 additions & 5 deletions src/fable-library-py/fable_library/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ def match(match: Match[str]) -> str:


def date_to_string_with_offset(date: datetime, format: str | None = None) -> str:
if format and len(format) == 1:
return date_to_string_with_custom_format(date, format, True)

raise NotImplementedError("date_to_string_with_offset")
match format:
case None:
raise NotImplementedError("date_to_string_with_offset")
case "O" | "o":
return date.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
case _ if len(format) == 1:
raise Exception("Unrecognized Date print format")
case _:
return date_to_string_with_custom_format(date, format, True)


def date_to_string_with_kind(date: datetime, format: str | None = None) -> str:
Expand All @@ -152,7 +157,7 @@ def date_to_string_with_kind(date: datetime, format: str | None = None) -> str:
elif format == "T" or format == "t":
return dateToHalfUTCString(date, "second") if utc else str(date.time())
elif format == "O" or format == "o":
return dateToISOString(date, utc)
return date.astimezone().isoformat(timespec="milliseconds")
else:
raise Exception("Unrecognized Date print format")

Expand Down
14 changes: 8 additions & 6 deletions tests/Python/TestDateTime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ let ``test DateTime.ToString with milliseconds`` () =
DateTime(2014, 9, 11, 16, 37, 11, 345).ToString("ss.fff")
|> equal "11.345"

// FIXME: missing regex module
// [<Fact>]
// let ``test DateTime.ToString with Round-trip format works for Utc`` () =
// let str = DateTime(2014, 9, 11, 16, 37, 2, DateTimeKind.Utc).ToString("O")
// System.Text.RegularExpressions.Regex.Replace(str, "0{3,}", "000")
// |> equal "2014-09-11T16:37:02.000Z"
[<Fact>]
let ``test DateTime.ToString with Round-trip format works for Utc`` () =
let str = DateTime(2014, 9, 11, 16, 37, 2, DateTimeKind.Utc).ToString("O")
// FIXME: missing regex module
// System.Text.RegularExpressions.Regex.Replace(str, "0{3,}", "000")
// Hardcode the replace string so we can test that "O" format is supported
str.Replace("0000000Z", "000000Z")
|> equal "2014-09-11T16:37:02.000000Z"

[<Fact>]
let ``test DateTime from Year 1 to 99 works`` () =
Expand Down
Loading