Skip to content

Commit 96148c4

Browse files
committed
[Rust] Add new TimeSpan overloads
1 parent 086851f commit 96148c4

File tree

13 files changed

+364
-155
lines changed

13 files changed

+364
-155
lines changed

src/Fable.Cli/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
* [JS/TS/Python] Add new `TimSpan` overload support coming from .NET 9 (by @MangelMaxime)
12+
* [JS/TS/Python] Add new `TimeSpan` overloads support coming from .NET 9.0 (by @MangelMaxime)
13+
* [Rust] Add new `TimeSpan` overloads support coming from .NET 9.0 (by @ncave)
1314

1415
### Fixed
1516

src/Fable.Transforms/Rust/Replacements.fs

+61-48
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,7 @@ let ignoreFormatProvider compiledName args =
25082508
| _ -> args
25092509

25102510
let makeDateOrTimeMemberCall com ctx r t i moduleName memberName (thisArg: Expr option) (args: Expr list) =
2511+
let memberName = Naming.removeGetSetPrefix memberName |> Naming.lowerFirst
25112512
let args = ignoreFormatProvider i.CompiledName args
25122513

25132514
match thisArg with
@@ -2584,10 +2585,7 @@ let dateTimes (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
25842585
| "Subtract" ->
25852586
Operation(Binary(BinaryOperator.BinaryMinus, thisArg.Value, args.Head), Tags.empty, t, r)
25862587
|> Some
2587-
| meth ->
2588-
let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst
2589-
2590-
makeDateOrTimeMemberCall com ctx r t i "DateTime" meth thisArg args |> Some
2588+
| meth -> makeDateOrTimeMemberCall com ctx r t i "DateTime" meth thisArg args |> Some
25912589

25922590
let dateTimeOffsets (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
25932591
match i.CompiledName with
@@ -2598,24 +2596,30 @@ let dateTimeOffsets (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg:
25982596
| ExprType(DeclaredType(ent, [])) :: [] when ent.FullName = Types.datetime -> "new_datetime" |> Some
25992597
| ExprType(DeclaredType(ent, [])) :: _ when ent.FullName = Types.datetime -> "new_datetime2" |> Some
26002598
| ExprType(DeclaredType(ent, [])) :: _ when ent.FullName = Types.dateOnly -> "new_date_time" |> Some
2601-
| ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32,
2602-
_)) :: ExprType(Number(Int32,
2603-
_)) :: ExprType(Number(Int32,
2604-
_)) :: _offset :: [] ->
2605-
"new_ymdhms" |> Some
2606-
| ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32,
2607-
_)) :: ExprType(Number(Int32,
2608-
_)) :: ExprType(Number(Int32,
2609-
_)) :: ExprType(Number(Int32,
2610-
_)) :: _offset :: [] ->
2611-
"new_ymdhms_milli" |> Some
2612-
| ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32,
2613-
_)) :: ExprType(Number(Int32,
2614-
_)) :: ExprType(Number(Int32,
2615-
_)) :: ExprType(Number(Int32,
2616-
_)) :: ExprType(Number(Int32,
2617-
_)) :: _offset :: [] ->
2618-
"new_ymdhms_micro" |> Some
2599+
| [ ExprType(Number(Int32, _))
2600+
ExprType(Number(Int32, _))
2601+
ExprType(Number(Int32, _))
2602+
ExprType(Number(Int32, _))
2603+
ExprType(Number(Int32, _))
2604+
ExprType(Number(Int32, _))
2605+
_offset ] -> "new_ymdhms" |> Some
2606+
| [ ExprType(Number(Int32, _))
2607+
ExprType(Number(Int32, _))
2608+
ExprType(Number(Int32, _))
2609+
ExprType(Number(Int32, _))
2610+
ExprType(Number(Int32, _))
2611+
ExprType(Number(Int32, _))
2612+
ExprType(Number(Int32, _))
2613+
_offset ] -> "new_ymdhms_milli" |> Some
2614+
| [ ExprType(Number(Int32, _))
2615+
ExprType(Number(Int32, _))
2616+
ExprType(Number(Int32, _))
2617+
ExprType(Number(Int32, _))
2618+
ExprType(Number(Int32, _))
2619+
ExprType(Number(Int32, _))
2620+
ExprType(Number(Int32, _))
2621+
ExprType(Number(Int32, _))
2622+
_offset ] -> "new_ymdhms_micro" |> Some
26192623
| _ -> None
26202624
|> Option.map (fun meth -> makeStaticMemberCall com r t i "DateTimeOffset" meth args)
26212625
| "Compare"
@@ -2629,8 +2633,6 @@ let dateTimeOffsets (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg:
26292633
Operation(Binary(BinaryOperator.BinaryMinus, thisArg.Value, args.Head), Tags.empty, t, r)
26302634
|> Some
26312635
| meth ->
2632-
let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst
2633-
26342636
makeDateOrTimeMemberCall com ctx r t i "DateTimeOffset" meth thisArg args
26352637
|> Some
26362638

@@ -2646,10 +2648,7 @@ let dateOnly (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
26462648
| "Equals"
26472649
| "GetHashCode" -> valueTypes com ctx r t i thisArg args
26482650
| "ToDateTime" when args.Length = 2 -> makeInstanceCall r t i thisArg.Value "toDateTime2" args |> Some
2649-
| meth ->
2650-
let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst
2651-
2652-
makeDateOrTimeMemberCall com ctx r t i "DateOnly" meth thisArg args |> Some
2651+
| meth -> makeDateOrTimeMemberCall com ctx r t i "DateOnly" meth thisArg args |> Some
26532652

26542653
let timeOnly (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
26552654
match i.CompiledName with
@@ -2674,26 +2673,30 @@ let timeOnly (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
26742673
| "Equals"
26752674
| "GetHashCode" -> valueTypes com ctx r t i thisArg args
26762675
| "Add" when args.Length = 2 -> makeInstanceCall r t i thisArg.Value "add2" args |> Some
2677-
| meth ->
2678-
let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst
2679-
2680-
makeDateOrTimeMemberCall com ctx r t i "TimeOnly" meth thisArg args |> Some
2676+
| meth -> makeDateOrTimeMemberCall com ctx r t i "TimeOnly" meth thisArg args |> Some
26812677

26822678
let timeSpans (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
26832679
// let callee = match i.callee with Some c -> c | None -> i.args.Head
26842680
match i.CompiledName with
26852681
| ".ctor" ->
26862682
match args with
2687-
| ExprType(Number(Int64, _)) :: [] -> "new_ticks" |> Some
2688-
| ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: [] ->
2689-
"new_hms" |> Some
2690-
| ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32,
2691-
_)) :: [] ->
2692-
"new_dhms" |> Some
2693-
| ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32, _)) :: ExprType(Number(Int32,
2694-
_)) :: ExprType(Number(Int32,
2695-
_)) :: [] ->
2696-
"new_dhms_milli" |> Some
2683+
| [ ExprType(Number(Int64, _)) ] -> "new_ticks" |> Some
2684+
| [ ExprType(Number(Int32, _)); ExprType(Number(Int32, _)); ExprType(Number(Int32, _)) ] -> "new_hms" |> Some
2685+
| [ ExprType(Number(Int32, _))
2686+
ExprType(Number(Int32, _))
2687+
ExprType(Number(Int32, _))
2688+
ExprType(Number(Int32, _)) ] -> "new_dhms" |> Some
2689+
| [ ExprType(Number(Int32, _))
2690+
ExprType(Number(Int32, _))
2691+
ExprType(Number(Int32, _))
2692+
ExprType(Number(Int32, _))
2693+
ExprType(Number(Int32, _)) ] -> "new_dhms_milli" |> Some
2694+
| [ ExprType(Number(Int32, _))
2695+
ExprType(Number(Int32, _))
2696+
ExprType(Number(Int32, _))
2697+
ExprType(Number(Int32, _))
2698+
ExprType(Number(Int32, _))
2699+
ExprType(Number(Int32, _)) ] -> "new_dhms_micro" |> Some
26972700
| _ -> None
26982701
|> Option.map (fun meth -> makeStaticMemberCall com r t i "TimeSpan" meth args)
26992702
| "Compare"
@@ -2713,12 +2716,22 @@ let timeSpans (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
27132716
| "Divide" ->
27142717
Operation(Binary(BinaryOperator.BinaryDivide, thisArg.Value, args.Head), Tags.empty, t, r)
27152718
|> Some
2716-
| meth ->
2717-
let meth =
2718-
Naming.removeGetSetPrefix meth
2719-
|> Naming.applyCaseRule Fable.Core.CaseRules.SnakeCase
2720-
2721-
makeDateOrTimeMemberCall com ctx r t i "TimeSpan" meth thisArg args |> Some
2719+
| "FromDays"
2720+
| "FromHours"
2721+
| "FromMinutes"
2722+
| "FromSeconds"
2723+
| "FromMilliseconds"
2724+
| "FromMicroseconds" as meth ->
2725+
match args with
2726+
| [ ExprType(Number(Float64, _)) ] ->
2727+
// overloads that take a float
2728+
makeDateOrTimeMemberCall com ctx r t i "TimeSpan" meth thisArg args |> Some
2729+
| _ ->
2730+
// overloads with variable argument counts
2731+
let argCount = List.length args
2732+
let meth = meth + (string argCount)
2733+
makeDateOrTimeMemberCall com ctx r t i "TimeSpan" meth thisArg args |> Some
2734+
| meth -> makeDateOrTimeMemberCall com ctx r t i "TimeSpan" meth thisArg args |> Some
27222735

27232736
let timers (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
27242737
match i.CompiledName, thisArg, args with

src/fable-library-rust/src/DateTime.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub mod DateTime_ {
9595
}
9696

9797
pub fn new_ticks(ticks: i64) -> DateTime {
98-
Self::minValue().add(TimeSpan::from_ticks(ticks))
98+
Self::minValue().add(TimeSpan::fromTicks(ticks))
9999
}
100100

101101
pub fn new_ticks_kind(ticks: i64, kind: i32) -> DateTime {
@@ -281,7 +281,7 @@ pub mod DateTime_ {
281281

282282
pub fn subtract2(&self, other: DateTime) -> TimeSpan {
283283
let ticks = duration_to_ticks(self.ndt - other.ndt);
284-
TimeSpan::from_ticks(ticks)
284+
TimeSpan::fromTicks(ticks)
285285
}
286286

287287
pub fn kind(&self) -> i32 {
@@ -369,16 +369,16 @@ pub mod DateTime_ {
369369
}
370370

371371
pub fn microsecond(&self) -> i32 {
372-
self.ndt.and_utc().timestamp_subsec_micros() as i32
372+
(self.ndt.and_utc().timestamp_subsec_micros() % 1000) as i32
373373
}
374374

375375
pub fn nanosecond(&self) -> i32 {
376-
self.ndt.and_utc().timestamp_subsec_nanos() as i32
376+
(self.ndt.and_utc().timestamp_subsec_nanos() % 1000) as i32
377377
}
378378

379379
pub fn timeOfDay(&self) -> TimeSpan {
380380
let d = self.ndt.time() - NaiveTime::MIN;
381-
TimeSpan::from_ticks(duration_to_ticks(d))
381+
TimeSpan::fromTicks(duration_to_ticks(d))
382382
}
383383

384384
pub fn dayNumber(&self) -> i32 {
@@ -421,31 +421,31 @@ pub mod DateTime_ {
421421
}
422422

423423
pub fn addDays(&self, days: f64) -> DateTime {
424-
self.add(TimeSpan::from_days(days))
424+
self.add(TimeSpan::fromDays(days))
425425
}
426426

427427
pub fn addHours(&self, hours: f64) -> DateTime {
428-
self.add(TimeSpan::from_hours(hours))
428+
self.add(TimeSpan::fromHours(hours))
429429
}
430430

431431
pub fn addMinutes(&self, minutes: f64) -> DateTime {
432-
self.add(TimeSpan::from_minutes(minutes))
432+
self.add(TimeSpan::fromMinutes(minutes))
433433
}
434434

435435
pub fn addSeconds(&self, seconds: f64) -> DateTime {
436-
self.add(TimeSpan::from_seconds(seconds))
436+
self.add(TimeSpan::fromSeconds(seconds))
437437
}
438438

439439
pub fn addMilliseconds(&self, millis: f64) -> DateTime {
440-
self.add(TimeSpan::from_milliseconds(millis))
440+
self.add(TimeSpan::fromMilliseconds(millis))
441441
}
442442

443443
pub fn addMicroseconds(&self, micros: f64) -> DateTime {
444-
self.add(TimeSpan::from_microseconds(micros))
444+
self.add(TimeSpan::fromMicroseconds(micros))
445445
}
446446

447447
pub fn addTicks(&self, ticks: i64) -> DateTime {
448-
self.add(TimeSpan::from_ticks(ticks))
448+
self.add(TimeSpan::fromTicks(ticks))
449449
}
450450

451451
pub fn toString(&self, format: string) -> string {

src/fable-library-rust/src/DateTimeOffset.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ pub mod DateTimeOffset_ {
6767

6868
pub fn new_utc(ndt: NaiveDateTime, offset: TimeSpan) -> DateTimeOffset {
6969
Self::validate(ndt, offset);
70-
let ofs = FixedOffset::east_opt(offset.total_seconds() as i32).unwrap();
70+
let ofs = FixedOffset::east_opt(offset.totalSeconds() as i32).unwrap();
7171
let cdt = ofs.from_utc_datetime(&ndt);
7272
DateTimeOffset(cdt.into())
7373
}
7474

7575
pub fn new_local(ndt: NaiveDateTime, offset: TimeSpan) -> DateTimeOffset {
7676
Self::validate(ndt, offset);
77-
let ofs = FixedOffset::east_opt(offset.total_seconds() as i32).unwrap();
77+
let ofs = FixedOffset::east_opt(offset.totalSeconds() as i32).unwrap();
7878
let cdt = ofs.from_local_datetime(&ndt).unwrap();
7979
DateTimeOffset(cdt.into())
8080
}
@@ -88,7 +88,7 @@ pub mod DateTimeOffset_ {
8888
}
8989

9090
pub fn new_ticks(ticks: i64, offset: TimeSpan) -> DateTimeOffset {
91-
let ts = TimeSpan::from_ticks(ticks);
91+
let ts = TimeSpan::fromTicks(ticks);
9292
let ndt = Self::minValue().add(ts).0.naive_local();
9393
Self::new_local(ndt, offset)
9494
}
@@ -221,7 +221,7 @@ pub mod DateTimeOffset_ {
221221
}
222222

223223
pub fn subtract2(&self, other: DateTimeOffset) -> TimeSpan {
224-
TimeSpan::from_ticks(duration_to_ticks((self.0 - other.0)))
224+
TimeSpan::fromTicks(duration_to_ticks((self.0 - other.0)))
225225
}
226226

227227
pub fn equalsExact(&self, other: DateTimeOffset) -> bool {
@@ -230,7 +230,7 @@ pub mod DateTimeOffset_ {
230230

231231
pub fn offset(&self) -> TimeSpan {
232232
let seconds = self.0.offset().local_minus_utc();
233-
TimeSpan::from_seconds(seconds as f64)
233+
TimeSpan::fromSeconds(seconds as f64)
234234
}
235235

236236
pub fn ticks(&self) -> i64 {
@@ -306,16 +306,16 @@ pub mod DateTimeOffset_ {
306306
}
307307

308308
pub fn microsecond(&self) -> i32 {
309-
self.0.timestamp_subsec_micros() as i32
309+
(self.0.timestamp_subsec_micros() % 1000) as i32
310310
}
311311

312312
pub fn nanosecond(&self) -> i32 {
313-
self.0.timestamp_subsec_nanos() as i32
313+
(self.0.timestamp_subsec_nanos() % 1000) as i32
314314
}
315315

316316
pub fn timeOfDay(&self) -> TimeSpan {
317317
let d = self.0.time() - NaiveTime::MIN;
318-
TimeSpan::from_ticks(duration_to_ticks(d))
318+
TimeSpan::fromTicks(duration_to_ticks(d))
319319
}
320320

321321
// pub fn totalOffsetMinutes(&self) -> i32 {
@@ -360,31 +360,31 @@ pub mod DateTimeOffset_ {
360360
}
361361

362362
pub fn addDays(&self, days: f64) -> DateTimeOffset {
363-
self.add(TimeSpan::from_days(days))
363+
self.add(TimeSpan::fromDays(days))
364364
}
365365

366366
pub fn addHours(&self, hours: f64) -> DateTimeOffset {
367-
self.add(TimeSpan::from_hours(hours))
367+
self.add(TimeSpan::fromHours(hours))
368368
}
369369

370370
pub fn addMinutes(&self, minutes: f64) -> DateTimeOffset {
371-
self.add(TimeSpan::from_minutes(minutes))
371+
self.add(TimeSpan::fromMinutes(minutes))
372372
}
373373

374374
pub fn addSeconds(&self, seconds: f64) -> DateTimeOffset {
375-
self.add(TimeSpan::from_seconds(seconds))
375+
self.add(TimeSpan::fromSeconds(seconds))
376376
}
377377

378378
pub fn addMilliseconds(&self, millis: f64) -> DateTimeOffset {
379-
self.add(TimeSpan::from_milliseconds(millis))
379+
self.add(TimeSpan::fromMilliseconds(millis))
380380
}
381381

382382
pub fn addMicroseconds(&self, micros: f64) -> DateTimeOffset {
383-
self.add(TimeSpan::from_microseconds(micros))
383+
self.add(TimeSpan::fromMicroseconds(micros))
384384
}
385385

386386
pub fn addTicks(&self, ticks: i64) -> DateTimeOffset {
387-
self.add(TimeSpan::from_ticks(ticks))
387+
self.add(TimeSpan::fromTicks(ticks))
388388
}
389389

390390
pub fn toString(&self, format: string) -> string {

0 commit comments

Comments
 (0)