Skip to content

Commit

Permalink
Merge pull request #3666 from fable-compiler/python-datetime-timespan…
Browse files Browse the repository at this point in the history
…-addition
  • Loading branch information
MangelMaxime authored Dec 15, 2023
2 parents f721982 + 498b8b3 commit 394fadf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* [GH-3655](https://github.com/fable-compiler/Fable/issues/3655) Fix for Python output file names (by @dbrattli)
* [GH-3660](https://github.com/fable-compiler/Fable/issues/3660) Fix for decimal to string with culture (by @dbrattli)
* [GH-3666](https://github.com/fable-compiler/Fable/pull/3666) Fix for `DateTime` and `TimeSpan` addition (by @dbrattli)

## 4.8.1 - 2023-12-12

Expand Down
14 changes: 12 additions & 2 deletions src/fable-library-py/fable_library/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def year(d: datetime) -> int:
return d.year


def to_universal_time(d: datetime) -> datetime:
return d.astimezone(timezone.utc)


def date_to_string_with_custom_format(date: datetime, format: str, utc: bool) -> str:
def match(match: Match[str]) -> str:
group = match.group()
Expand Down Expand Up @@ -219,8 +223,12 @@ def min_value() -> datetime:
return datetime.min


def op_addition(x: datetime, y: timedelta) -> datetime:
return x + y
def op_addition(x: datetime, y: TimeSpan) -> datetime:
return x + timedelta(microseconds=total_microseconds(y))


def add(x: datetime, y: TimeSpan) -> datetime:
return op_addition(x, y)


def parse(string: str, detectUTC: bool = False) -> datetime:
Expand All @@ -242,6 +250,7 @@ def add_milliseconds(d: datetime, v: int) -> datetime:


__all__ = [
"add",
"op_subtraction",
"subtract",
"create",
Expand All @@ -259,5 +268,6 @@ def add_milliseconds(d: datetime, v: int) -> datetime:
"min_value",
"op_addition",
"parse",
"to_universal_time",
"try_parse",
]
5 changes: 3 additions & 2 deletions src/fable-library-py/fable_library/map_util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import re
from collections.abc import ByteString, Iterable, MutableSequence
from collections.abc import Iterable
from enum import IntEnum
from re import Match
from typing import (
Expand All @@ -12,6 +12,7 @@
)

from .types import FSharpRef, Union
from .util import Array


_K = TypeVar("_K")
Expand Down Expand Up @@ -124,7 +125,7 @@ def assign(key: str, case_rule: CaseRules, value: Any):
kv_pair = name if len(kv_pair.fields) == 0 else [name, *kv_pair.fields]
case_rule = defined_case_rule

if isinstance(kv_pair, list | MutableSequence | ByteString):
if isinstance(kv_pair, Array):
length = len(kv_pair)
if length == 0:
fail(kv_pair)
Expand Down
13 changes: 13 additions & 0 deletions tests/Python/TestTimeSpan.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ let ``test TimeSpan Addition works`` () =
test -2000. 0. -2000.
test 0. 0. 0.

[<Fact>]
let ``test DateTime and TimeSpan Addition works`` () =
let test ms expected =
let dt = DateTime(2014,9,12,0,0,0,DateTimeKind.Utc)
let ts = TimeSpan.FromMilliseconds(ms)
let res1 = dt.Add(ts).ToUniversalTime()
let res2 = (dt + ts).ToUniversalTime()
equal true (res1 = res2)
equal expected res1

DateTime(2014,9,12,0,0,1,DateTimeKind.Utc) |> test 1000.
DateTime(2014,9,11,23,59,59,DateTimeKind.Utc) |> test -1000.

[<Fact>]
let ``test TimeSpan implementation coherence`` () =
TimeSpan.FromTicks(1L).Ticks |> equal 1L
Expand Down

0 comments on commit 394fadf

Please sign in to comment.