Skip to content

Commit e715c80

Browse files
authored
TimeSpan-Style TimeDuration Constructors in C# Bindings (#2778)
# Description of Changes This change adds the following `System.TimeSpan`-style static construction methods to `SpacetimeDB.TimeDuration`: * `static TimeDuration FromMilliseconds(double milliseconds)` * `static TimeDuration FromSeconds(double seconds)` * `static TimeDuration FromMinutes(double minutes)` * `static TimeDuration FromHours(double hours)` * `static TimeDuration FromDays(double days)` These mirror the equivalently named static methods on `System.TimeSpan` and dramatically improve usability and familiarity for experienced C# users with no more overhead than the user performing the multiplication themselves. Wish I'd thought to do this before v1.1.2 got released. Ah well. # API and ABI breaking changes None. Convenience methods added in bindings only. # Expected complexity level and risk 1 (potentially up to a low 2 if cleanup is desired elsewhere in the bindings to leverage these new methods). # Testing <!-- Describe any testing you've done, and any testing you'd like your reviewers to do, so that you're confident that all the changes work as expected! --> - [x] Ensure the changes build. <!-- maybe a test you want to do --> - [ ] New contributor check! Review to make sure repo style & substance standards are complied with. <!-- maybe a test you want a reviewer to do, so they can check it off when they're satisfied. -->
1 parent f9c8e72 commit e715c80

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

crates/bindings-csharp/BSATN.Runtime/Builtins.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,33 @@ public record struct TimeDuration(long Microseconds) : IStructuralReadWrite
432432
{
433433
public static readonly TimeDuration ZERO = new(0);
434434

435+
/// <summary>
436+
/// Returns a <see cref="TimeDuration"/> that represents a specified number of <paramref name="milliseconds"/>, accurate to the nearest microsecond.
437+
/// </summary>
438+
public static TimeDuration FromMilliseconds(double milliseconds) =>
439+
new((long)(milliseconds * 1000L));
440+
441+
/// <summary>
442+
/// Returns a <see cref="TimeDuration"/> that represents a specified number of <paramref name="seconds"/>, accurate to the nearest microsecond.
443+
/// </summary>
444+
public static TimeDuration FromSeconds(double seconds) =>
445+
new((long)(seconds * Util.MicrosecondsPerSecond));
446+
447+
/// <summary>
448+
/// Returns a <see cref="TimeDuration"/> that represents a specified number of 60-second <paramref name="minutes"/>.
449+
/// </summary>
450+
public static TimeDuration FromMinutes(double minutes) => FromSeconds(minutes * 60);
451+
452+
/// <summary>
453+
/// Returns a <see cref="TimeDuration"/> that represents a specified number of 60-minute <paramref name="hours"/>.
454+
/// </summary>
455+
public static TimeDuration FromHours(double hours) => FromMinutes(hours * 60);
456+
457+
/// <summary>
458+
/// Returns a <see cref="TimeDuration"/> that represents a specified number of 24-hour <paramref name="days"/>.
459+
/// </summary>
460+
public static TimeDuration FromDays(double days) => FromHours(days * 24);
461+
435462
public static implicit operator TimeSpan(TimeDuration d) =>
436463
new(d.Microseconds * Util.TicksPerMicrosecond);
437464

0 commit comments

Comments
 (0)