Skip to content

Commit

Permalink
Fixed #3815 (#3816)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave authored May 17, 2024
1 parent 8b48542 commit 1d50541
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 10 deletions.
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

### Fixed

* [JS/TS] Fixed TimeSpan.FromMilliseconds (#3815) (by @ncave)

## 4.17.0 - 2024-04-23

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/fable-library-ts/TimeSpan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tslint:disable:max-line-length
import { FSharpRef } from "./Types.js";
import { comparePrimitives, padLeftAndRightWithZeros, padWithZeros } from "./Util.js";
import { toInt64 } from "./BigInt.js";
import { toInt64, fromFloat64 } from "./BigInt.js";

// TimeSpan in runtime just becomes a number representing milliseconds
export type TimeSpan = number;
Expand Down Expand Up @@ -73,7 +73,7 @@ export function milliseconds(ts: TimeSpan) {
}

export function ticks(ts: TimeSpan) {
return toInt64(BigInt(ts) * 10000n);
return toInt64(fromFloat64(ts * 10000));
}

export function totalDays(ts: TimeSpan) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Dart/src/TimeSpanTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ let tests() =
t.Days + t.Hours + t.Minutes + t.Seconds + t.Milliseconds |> float
|> equal 686.

testCase "TimeSpan.FromMilliseconds works" <| fun () ->
let t = TimeSpan.FromMilliseconds(1. / 3.)
t.Ticks / 10L |> equal 333L // Dart only has microsecond precision

testCase "TimeSpan.Ticks works" <| fun () ->
let t = TimeSpan.FromTicks(20000L)
t.Ticks
Expand Down
4 changes: 4 additions & 0 deletions tests/Js/Main/TimeSpanTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ let tests =
t.Days + t.Hours + t.Minutes + t.Seconds + t.Milliseconds |> float
|> equal 686.

testCase "TimeSpan.FromMilliseconds works" <| fun () ->
let t = TimeSpan.FromMilliseconds(1. / 3.)
t.Ticks |> equal 3333L

testCase "TimeSpan.Ticks works" <| fun () ->
let t = TimeSpan.FromTicks(20000L)
t.Ticks
Expand Down
2 changes: 1 addition & 1 deletion tests/Php/TestRecordType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ let ``test Record methods can be generated`` () =
|> equal "Hello World! by Alfonso"

[<Fact>]
let ``test RRecord expression constructors can be generated`` () =
let ``test Record expression constructors can be generated`` () =
let x = { name = "Alfonso"; luckyNumber = 7 }
let y = { x with luckyNumber = 14 }
equal "Alfonso" y.name
Expand Down
2 changes: 1 addition & 1 deletion tests/Python/TestApplicative.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1723,7 +1723,7 @@ let ``test Accessor function shorthand works for anonymous records`` () =
equal names ["John"; "Jane"]

[<Fact>]
let ``test Accessor function shorthand works with STRP syntax`` =
let ``test Accessor function shorthand works with STRP syntax`` () =
let user : AccessorFunctionShorthand.User =
{ Name = "John" }
let student : AccessorFunctionShorthand.Student =
Expand Down
8 changes: 4 additions & 4 deletions tests/Python/TestRecordType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let ``test Record methods can be generated`` () =
|> equal "Hello World! by Alfonso"

[<Fact>]
let ``test RRecord expression constructors can be generated`` () =
let ``test Record expression constructors can be generated`` () =
let x = { name = "Alfonso"; luckyNumber = 7 }
let y = { x with luckyNumber = 14 }
equal "Alfonso" y.name
Expand Down Expand Up @@ -133,23 +133,23 @@ let ``test Mutating records work`` () =
equal -20 x''.uniqueB

[<Fact>]
let ``test Nested record field copy and update works for records`` =
let ``test Nested record field copy and update works for records`` () =
let car =
{ Interior = { Seats = 4 } }
let car2 =
{ car with Interior.Seats = 5 }
equal 5 car2.Interior.Seats

[<Fact>]
let ``test Nested record field copy and update works for anonymous records`` =
let ``test Nested record field copy and update works for anonymous records`` () =
let car =
{| Interior = {| Seats = 4 |} |}
let car2 =
{| car with Interior.Seats = 5 |}
equal 5 car2.Interior.Seats

[<Fact>]
let ``test Record equality when it has optional field`` =
let ``test Record equality when it has optional field`` () =
let a = { OptionalField = None }
let b = { OptionalField = None }
let c = { OptionalField = Some "test" }
Expand Down
4 changes: 2 additions & 2 deletions tests/Python/TestString.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let ``test sprintf displays sign correctly`` () =
sprintf "%.2f" -1. |> equal "-1.00"

[<Fact>]
let ``test format string can use and compose string literals`` =
let ``test format string can use and compose string literals`` () =
let renderedCoordinates = sprintf formatCoordinateBody 0.25 0.75
let renderedText = sprintf fullFormat 0.25 0.75

Expand Down Expand Up @@ -122,7 +122,7 @@ let ``test string interpolation works with anonymous records`` () =
|> equal "Hi! My name is John DOE. I'm 32 years old and I'm from The United Kingdom!"

[<Fact>]
let ``test Extended string interpolation syntax`` =
let ``test Extended string interpolation syntax`` () =
let classAttr = "item-panel"
let cssNew = $$""".{{classAttr}}:hover {background-color: #eee;}"""
cssNew |> equal ".item-panel:hover {background-color: #eee;}"
Expand Down
5 changes: 5 additions & 0 deletions tests/Python/TestTimeSpan.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ let ``test TimeSpan components work`` () =
t.Days + t.Hours + t.Minutes + t.Seconds + t.Milliseconds |> float
|> equal 686.

[<Fact>]
let ``test TimeSpan.FromMilliseconds works`` () =
let t = TimeSpan.FromMilliseconds(1. / 3.)
t.Ticks |> equal 3333L

[<Fact>]
let ``test TimeSpan.Ticks works`` () =
let t = TimeSpan.FromTicks(20000L)
Expand Down
5 changes: 5 additions & 0 deletions tests/Rust/tests/src/TimeSpanTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ let ``TimeSpan components work`` () =
t.Days + t.Hours + t.Minutes + t.Seconds + t.Milliseconds |> float
|> equal 686.

[<Fact>]
let ``TimeSpan.FromMilliseconds works`` () =
let t = TimeSpan.FromMilliseconds(1. / 3.)
t.Ticks |> equal 3333L

[<Fact>]
let ``TimeSpan.Ticks works`` () =
let t = TimeSpan.FromTicks(20000L)
Expand Down

0 comments on commit 1d50541

Please sign in to comment.