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

[Python] System.DateTime.Substract not correctly transpiled #3628

Closed
Freymaurer opened this issue Nov 30, 2023 · 1 comment · Fixed by #3630
Closed

[Python] System.DateTime.Substract not correctly transpiled #3628

Freymaurer opened this issue Nov 30, 2023 · 1 comment · Fixed by #3630

Comments

@Freymaurer
Copy link

Description

I want to use the Substract member on System.DateTime and noticed that it is transpiled to subtract and imported by from fable_modules.fable_library.date import (now, subtract). If i go into this file i can find the following code, which seems right:

# fable_modules.fable_library.date
def op_subtraction(x: datetime, y: datetime) -> TimeSpan:
    delta = x - y
    # ts.microseconds only contains the microseconds provided to the constructor
    # so we need to calculate the total microseconds ourselves
    delta_microseconds = delta.days * (24 * 3600) + delta.seconds * 10**6 + delta.microseconds
    return create_time_span(0, 0, 0, 0, 0, delta_microseconds)

So my guess is it should be transpiled to op_subtraction, instead of subtract.

Repro code

#r "nuget: Fable.Core, 4.2.0"

open Fable.Core
open System

[<AttachMembersAttribute>]
type Stopwatch() =
  member val StartTime: DateTime option = None with get, set
  member val StopTime: DateTime option = None with get, set
  member this.Start() = this.StartTime <- Some DateTime.Now
  member this.Stop() = 
    match this.StartTime with
    | Some _ -> this.StopTime <- Some DateTime.Now
    | None -> failwith "Error. Unable to call `Stop` before `Start`."
  member this.Elapsed = 
    match this.StartTime, this.StopTime with
    | Some start, Some stop -> stop.Subtract(start)
    | _, _ -> failwith "Error. Unable to call `Elapsed` without calling `Start` and `Stop` before."

let testFunc() = 
  let stopwatch = new Stopwatch()
  stopwatch.Start()
  System.Threading.Thread.Sleep(2000)
  stopwatch.Stop()
  stopwatch.Elapsed

testFunc().ToString() |> printfn "%s"

Related information

  • Fable version: 4.6.0
@Freymaurer
Copy link
Author

I just saw, that i can use stop - start instead!

member this.Elapsed = 
    match this.StartTime, this.StopTime with
    | Some start, Some stop -> stop - start // this
    | _, _ -> failwith "Error. Unable to call `Elapsed` without calling `Start` and `Stop` before."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant