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

Add new TimSpan overload support coming from .NET 9 #3994

Merged
merged 7 commits into from
Jan 8, 2025
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
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RUN apt-get update && apt-get install -y xz-utils

# Options
# Let it here to avoid conflicts with Microsoft options !!!
ARG DOTNET_VERSION=8.0.404
ARG DOTNET_VERSION=9.0.101
ARG NODE_VERSION=18.8.0
ARG PYTHON_VERSION=3.10.0
ARG DART_VERSION=3.1.2
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4

- name: Restore tools
run: dotnet tool restore
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Setup Node.js environment
uses: actions/[email protected]
Expand All @@ -79,7 +79,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Setup Node.js environment
uses: actions/[email protected]
Expand All @@ -100,7 +100,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Setup Node.js environment
uses: actions/[email protected]
Expand All @@ -118,7 +118,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Setup Node.js environment
uses: actions/[email protected]
Expand All @@ -140,7 +140,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Setup dotnet tools
run: dotnet tool restore
Expand Down Expand Up @@ -186,7 +186,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Setup Node.js environment
uses: actions/[email protected]
Expand All @@ -212,7 +212,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Setup Dart SDK
uses: dart-lang/[email protected]
Expand All @@ -230,7 +230,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Restore tools
run: dotnet tool restore
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.100",
"version": "9.0.101",
"rollForward": "latestMinor"
}
}
1 change: 1 addition & 0 deletions src/Fable.Build/Quicktest/Core.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ let genericQuicktest (config: QuicktestConfig) (args: string list) =
|> CmdLine.appendPrefix "--extension" config.Extension
|> CmdLine.appendPrefix "--exclude" "Fable.Core"
|> CmdLine.appendRaw "--noCache"
|> CmdLine.appendRaw "--verbose"
|> CmdLine.appendRaw "--watch"
|> appendRunMode,
workingDirectory = projectDir
Expand Down
3 changes: 3 additions & 0 deletions src/Fable.Build/SimpleExec.Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Command with
let argsBuilder =
CmdLine.empty
|> CmdLine.appendRaw "watch"
|> CmdLine.appendRaw "--no-hot-reload"
|> CmdLine.appendPrefix "--project" localFableDir
|> CmdLine.appendRaw "run"
// Without the release mode, Fable stack overflow when compiling the tests
Expand All @@ -91,6 +92,7 @@ type Command with
[
CmdLine.empty
|> CmdLine.appendRaw "watch"
|> CmdLine.appendRaw "--no-hot-reload"
|> CmdLine.appendPrefix "--project" localFableDir
|> CmdLine.appendRaw "run"
// Without the release mode, Fable stack overflow when compiling the tests
Expand All @@ -117,6 +119,7 @@ type Command with
[
CmdLine.empty
|> CmdLine.appendRaw "watch"
|> CmdLine.appendRaw "--no-hot-reload"
|> CmdLine.appendPrefix "--project" localFableDir
|> CmdLine.appendRaw "run"
// Without the release mode, Fable stack overflow when compiling the tests
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Build/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Cmd =
CmdLine.empty
|> CmdLine.appendRaw "dotnet"
|> CmdLine.appendRaw "watch"
|> CmdLine.appendRaw "--no-hot-reload"
|> CmdLine.appendPrefix "--project" localFableDir
|> CmdLine.appendRaw "run"
// Without the release mode, Fable stack overflow when compiling the tests
Expand Down
4 changes: 4 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

* [JS/TS/Python] Add new `TimSpan` overload support coming from .NET 9

## 5.0.0-alpha.4 - 2025-01-03

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

* [JS/TS/Python] Add new `TimSpan` overload support coming from .NET 9

## 5.0.0-alpha.4 - 2025-01-03

### Fixed
Expand Down
42 changes: 34 additions & 8 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3129,18 +3129,43 @@ let dateOnly (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
Helper.LibCall(com, "DateOnly", meth, t, args, i.SignatureArgTypes, ?thisArg = thisArg, ?loc = r)
|> Some

let timeSpans (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
let timeSpans (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
let timeSpanLibCall meth args =
let args = ignoreFormatProvider meth args
let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst

Helper.LibCall(com, "TimeSpan", meth, t, args, i.SignatureArgTypes, ?thisArg = thisArg, ?loc = r)
|> Some

let isNotDefaultInt64ZeroValue (expr: Expr) =
match expr with
| Value(NumberConstant(NumberValue.Int64 0L, _), _) -> false
| _ -> true

let limitArgsCountToMilliseconds (meth: string) (maxArgsCount: int) =
if args.Length > maxArgsCount then
if isNotDefaultInt64ZeroValue args.[maxArgsCount] then
addWarning
com
ctx.InlinePath
r
"TimeSpan precision is limited to milliseconds, microsecond arguments will be ignored"

args |> List.take maxArgsCount |> timeSpanLibCall meth

else
timeSpanLibCall meth args

// let callee = match i.callee with Some c -> c | None -> i.args.Head
match i.CompiledName with
| ".ctor" ->
let meth =
match args with
| [ ticks ] -> "fromTicks"
| [ _ticks ] -> "fromTicks"
| _ -> "create"

Helper.LibCall(com, "TimeSpan", meth, t, args, i.SignatureArgTypes, ?loc = r)
|> Some
| "FromMilliseconds" -> TypeCast(args.Head, t) |> Some
| "get_TotalMilliseconds" -> TypeCast(thisArg.Value, t) |> Some
| "ToString" when (args.Length = 1) ->
"TimeSpan.ToString with one argument is not supported, because it depends on local culture, please add CultureInfo.InvariantCulture"
Expand All @@ -3159,12 +3184,13 @@ let timeSpans (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
|> addError com ctx.InlinePath r

None
| meth ->
let args = ignoreFormatProvider meth args
let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst
| "FromDays" -> limitArgsCountToMilliseconds "fromDays" 5
| "FromHours" -> limitArgsCountToMilliseconds "fromHours" 4
| "FromMinutes" -> limitArgsCountToMilliseconds "fromMinutes" 3
| "FromSeconds" -> limitArgsCountToMilliseconds "fromSeconds" 2
| "FromMilliseconds" -> limitArgsCountToMilliseconds "fromMilliseconds" 1

Helper.LibCall(com, "TimeSpan", meth, t, args, i.SignatureArgTypes, ?thisArg = thisArg, ?loc = r)
|> Some
| meth -> timeSpanLibCall meth args

let timeOnly (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
match i.CompiledName with
Expand Down
39 changes: 27 additions & 12 deletions src/fable-library-py/fable_library/time_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from math import ceil, floor, fmod
from typing import Any

from .types import FSharpRef
from .types import FSharpRef, int64
from .util import pad_left_and_right_with_zeros, pad_with_zeros


Expand Down Expand Up @@ -75,32 +75,47 @@ def total_days(ts: TimeSpan) -> float:
return ts / 864000000000


def from_microseconds(micros: float) -> TimeSpan:
def from_microseconds(micros: int64 | float) -> TimeSpan:
return create(0, 0, 0, 0, 0, micros)


def from_milliseconds(msecs: int) -> TimeSpan:
return create(0, 0, 0, 0, msecs)
def from_milliseconds(msecs: int64 | float, mc: int64 | None = None) -> TimeSpan:
return create(0, 0, 0, 0, msecs, mc)


def from_ticks(ticks: int) -> TimeSpan:
return create(ticks)


def from_seconds(s: int) -> TimeSpan:
return create(0, 0, s)
def from_seconds(s: int64 | float, ms: int64 | None = None, mc: int64 | None = None) -> TimeSpan:
return create(0, 0, 0, s, ms or 0, mc or 0)


def from_minutes(m: int) -> TimeSpan:
return create(0, m, 0)
def from_minutes(
m: int64 | float, s: int64 | None = None, ms: int64 | None = None, mc: int64 | None = None
) -> TimeSpan:
return create(0, 0, m or 0, s or 0, ms or 0, mc or 0)


def from_hours(h: int) -> TimeSpan:
return create(h, 0, 0)
def from_hours(
h: int | float,
m: int64 | None = None,
s: int64 | None = None,
ms: int64 | None = None,
mc: int64 | None = None,
) -> TimeSpan:
return create(0, h, m or 0, s or 0, ms or 0, mc or 0)


def from_days(d: int) -> TimeSpan:
return create(d, 0, 0, 0)
def from_days(
d: int | float,
h: int | None = None,
m: int64 | None = None,
s: int64 | None = None,
ms: int64 | None = None,
mc: int64 | None = None,
) -> TimeSpan:
return create(d, h or 0, m or 0, s or 0, ms or 0, mc or 0)


def ticks(ts: TimeSpan) -> int:
Expand Down
20 changes: 12 additions & 8 deletions src/fable-library-ts/TimeSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,24 @@ export function fromTicks(ticks: number | bigint) {
return Number(BigInt(ticks) / 10000n);
}

export function fromDays(d: number) {
return create(d, 0, 0, 0);
export function fromDays(d: number, h: number = 0, m: bigint = 0n, s: bigint = 0n, ms: bigint = 0n) {
return create(d, h, Number(m), Number(s), Number(ms));
}

export function fromHours(h: number) {
return create(h, 0, 0);
export function fromHours(h: number, m: bigint = 0n, s: bigint = 0n, ms: bigint = 0n) {
return create(0, h, Number(m), Number(s), Number(ms));
}

export function fromMinutes(m: number) {
return create(0, m, 0);
export function fromMinutes(m: number | bigint, s: bigint = 0n, ms: bigint = 0n) {
return create(0, 0, Number(m), Number(s), Number(ms));
}

export function fromSeconds(s: number) {
return create(0, 0, s);
export function fromSeconds(s: number | bigint, ms: bigint = 0n) {
return create(0, 0, 0, Number(s), Number(ms));
}

export function fromMilliseconds(ms: number | bigint) {
return Number(ms);
}

export function days(ts: TimeSpan) {
Expand Down
4 changes: 2 additions & 2 deletions src/quicktest-dart/Quicktest.Dart.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RollForward>Major</RollForward>
</PropertyGroup>
<ItemGroup>
Expand All @@ -14,4 +14,4 @@
<ItemGroup>
<ProjectReference Include="../Fable.Core/Fable.Core.fsproj" />
</ItemGroup>
</Project>
</Project>
4 changes: 2 additions & 2 deletions src/quicktest-rust/Quicktest.Rust.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RollForward>Major</RollForward>
</PropertyGroup>
<ItemGroup>
Expand All @@ -10,4 +10,4 @@
<ItemGroup>
<!-- <ProjectReference Include="../Fable.Core/Fable.Core.fsproj" /> -->
</ItemGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion src/quicktest/QuickTest.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RollForward>Major</RollForward>
<LangVersion>Preview</LangVersion>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion tests/Dart/src/Fable.Tests.Dart.fsproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RollForward>Major</RollForward>
</PropertyGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion tests/Js/Main/Fable.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RollForward>Major</RollForward>
<OtherFlags>$(OtherFlags) --crossoptimize- --nowarn:3370</OtherFlags>
<FableDefinesQueue>True</FableDefinesQueue>
Expand Down
Loading
Loading