value · System

TimeSpan

public value TimeSpan : IHashable<TimeSpan>, IComparable<TimeSpan>

A length of time (negative too): ticks of 100 ns. a + b, a - b, -a, a * 2.5, a / 2, a / b, and the comparisons work on it; as text it is [-][d.]hh:mm:ss[.fffffff].

Kind
value
Namespace
System
Assembly
System.Core
Access
public
Source file
Time.ant

Inheritance

Interfaces
IHashable<TSelf> IComparable<TSelf>

Constructors

new(i64 ticks)
#
new(i32 hours, i32 minutes, i32 seconds)
#
new(i32 days, i32 hours, i32 minutes, i32 seconds)
#
new(i32 days, i32 hours, i32 minutes, i32 seconds, i32 milliseconds)
#

Properties

static i64 TicksPerMicrosecond { get; }
#

static read-only

static i64 TicksPerMillisecond { get; }
#

static read-only

static i64 TicksPerSecond { get; }
#

static read-only

static i64 TicksPerMinute { get; }
#

static read-only

static i64 TicksPerHour { get; }
#

static read-only

static i64 TicksPerDay { get; }
#

static read-only

static TimeSpan Zero { get; }
#

static read-only

static TimeSpan MaxValue { get; }
#

static read-only

static TimeSpan MinValue { get; }
#

static read-only

i64 Ticks { get; }
#

read-only

i32 Days { get; }
#

read-only

The parts: whole days, then hours (0–23), minutes, seconds, milliseconds, microseconds, nanoseconds of the rest (negative for a negative length).

i32 Hours { get; }
#

read-only

i32 Minutes { get; }
#

read-only

i32 Seconds { get; }
#

read-only

i32 Milliseconds { get; }
#

read-only

i32 Microseconds { get; }
#

read-only

i32 Nanoseconds { get; }
#

read-only

f64 TotalDays { get; }
#

read-only

The whole length in one unit.

f64 TotalHours { get; }
#

read-only

f64 TotalMinutes { get; }
#

read-only

f64 TotalSeconds { get; }
#

read-only

f64 TotalMilliseconds { get; }
#

read-only

(Held to ±922337203685477, as .NET does.)

f64 TotalMicroseconds { get; }
#

read-only

f64 TotalNanoseconds { get; }
#

read-only

Methods

static FromDays(i64 days) : TimeSpan
#

static

A length in a unit: whole units exactly, fractions rounded toward zero to a tick.

static FromHours(i64 hours) : TimeSpan
#

static

static FromMinutes(i64 minutes) : TimeSpan
#

static

static FromSeconds(i64 seconds) : TimeSpan
#

static

static FromMilliseconds(i64 milliseconds) : TimeSpan
#

static

static FromMicroseconds(i64 microseconds) : TimeSpan
#

static

static FromDays(f64 days) : TimeSpan
#

static

static FromHours(f64 hours) : TimeSpan
#

static

static FromMinutes(f64 minutes) : TimeSpan
#

static

static FromSeconds(f64 seconds) : TimeSpan
#

static

static FromMilliseconds(f64 milliseconds) : TimeSpan
#

static

static FromMicroseconds(f64 microseconds) : TimeSpan
#

static

static FromTicks(i64 ticks) : TimeSpan
#

static

Add(TimeSpan other) : TimeSpan
#
Subtract(TimeSpan other) : TimeSpan
#
Negate() : TimeSpan
#
Duration() : TimeSpan
#

The length without its sign.

Multiply(f64 factor) : TimeSpan
#

Times a number, and divided by one (to the nearest tick, halves to even).

Divide(f64 divisor) : TimeSpan
#
Divide(TimeSpan other) : f64
#

How many times other fits in this length.

Equals(TimeSpan other) : bool
#
Hash() : u64
#
CompareTo(TimeSpan other) : i32
#
static Compare(TimeSpan a, TimeSpan b) : i32
#

static

ToString() : str
#

[-][d.]hh:mm:ss[.fffffff] (the "c" format).

ToString(str format) : str
#

As text by a format: "c" ([-][d.]hh:mm:ss[.fffffff]), "g" ([-][d:]h:mm:ss[.FFFFFFF]), "G" ([-]d:hh:mm:ss.fffffff), or a custom one of d…, h, hh, m, mm, s, ss, f…, F…, 'text' and \x.

static Parse(str text) : TimeSpan
#

static

The length text writes: [-]d, [-][d.]h:mm[:ss[.fffffff]] or [-]d:h:mm:ss[.fffffff], spaces around allowed; a FormatError when it writes none.

static TryParse(str text) : TimeSpan?
#

static

The length text writes, or null when it writes none.

Operators and conversions

op +(TimeSpan a, TimeSpan b) : TimeSpan
#

static

op -(TimeSpan a, TimeSpan b) : TimeSpan
#

static

op -(TimeSpan a) : TimeSpan
#

static

op +(TimeSpan a) : TimeSpan
#

static

op *(TimeSpan a, f64 factor) : TimeSpan
#

static

op *(f64 factor, TimeSpan a) : TimeSpan
#

static

op /(TimeSpan a, f64 divisor) : TimeSpan
#

static

op /(TimeSpan a, TimeSpan b) : f64
#

static

op ==(TimeSpan a, TimeSpan b) : bool
#

static

op !=(TimeSpan a, TimeSpan b) : bool
#

static

op <(TimeSpan a, TimeSpan b) : bool
#

static

op <=(TimeSpan a, TimeSpan b) : bool
#

static

op >(TimeSpan a, TimeSpan b) : bool
#

static

op >=(TimeSpan a, TimeSpan b) : bool
#

static

Private and internal members (5)

i64 ticks
#

private

static TooLong()
#

private static

static NotANumber()
#

private static

static Interval(f64 value, f64 scale) : TimeSpan
#

private static

value units of scale ticks, rounded toward zero.

static Whole(i64 value, i64 scale) : TimeSpan
#

private static

value whole units of scale ticks.

Source

TimeSpan as Antril, decompiled from its Larva HIR.

System.TimeSpan.ant
// System.TimeSpan — System.Core (decompiled from Larva HIR)

into namespace System
{
	/// A length of time (negative too): ticks of 100 ns. `a + b`, `a - b`, `-a`, `a * 2.5`, `a / 2`, `a / b`, and the
	/// comparisons work on it; as text it is `[-][d.]hh:mm:ss[.fffffff]`.
	public value TimeSpan : IHashable<TimeSpan>, IComparable<TimeSpan>
	{
		i64 ticks;

		[NoInline]
		static TooLong()
		{
			throw new OverflowError("TimeSpan: the length is too long");
		}

		[NoInline]
		static NotANumber()
		{
			throw new ArgumentError("TimeSpan: the length is not a number");
		}

		/// `value` units of `scale` ticks, rounded toward zero.
		static Interval(f64 value, f64 scale) : TimeSpan
		{
			if (value != value)
				NotANumber();
			f64 count = value * scale;
			if (count >= 9.223372036854776E+18 || count < -9.223372036854776E+18)
			{
				if (count == 9.223372036854776E+18)
					return new TimeSpan(9223372036854775807);
				TooLong();
			}
			return new TimeSpan((i64)count);
		}

		/// `value` whole units of `scale` ticks.
		static Whole(i64 value, i64 scale) : TimeSpan
		{
			if (value > 9223372036854775807 / scale || value < -9223372036854775808 / scale)
				TooLong();
			return new TimeSpan(value * scale);
		}

		public:
			new(i64 ticks)
			{
				this.ticks = ticks;
			}

			new(i32 hours, i32 minutes, i32 seconds) : (0, hours, minutes, seconds, 0)
			{
			}

			new(i32 days, i32 hours, i32 minutes, i32 seconds) : (days, hours, minutes, seconds, 0)
			{
			}

			new(i32 days, i32 hours, i32 minutes, i32 seconds, i32 milliseconds)
			{
				i64 total = (((i64)days * 24 + (i64)hours) * 60 + (i64)minutes) * 60000 + (i64)seconds * 1000 + (i64)milliseconds;
				if (total > 922337203685477 || total < -922337203685477)
					TooLong();
				ticks = total * 10000;
			}

			/// A length in a unit: whole units exactly, fractions rounded toward zero to a tick.
			static FromDays(i64 days) : TimeSpan => Whole(days, 864000000000);

			static FromHours(i64 hours) : TimeSpan => Whole(hours, 36000000000);

			static FromMinutes(i64 minutes) : TimeSpan => Whole(minutes, 600000000);

			static FromSeconds(i64 seconds) : TimeSpan => Whole(seconds, 10000000);

			static FromMilliseconds(i64 milliseconds) : TimeSpan => Whole(milliseconds, 10000);

			static FromMicroseconds(i64 microseconds) : TimeSpan => Whole(microseconds, 10);

			static FromDays(f64 days) : TimeSpan => Interval(days, 864000000000.0);

			static FromHours(f64 hours) : TimeSpan => Interval(hours, 36000000000.0);

			static FromMinutes(f64 minutes) : TimeSpan => Interval(minutes, 600000000.0);

			static FromSeconds(f64 seconds) : TimeSpan => Interval(seconds, 10000000.0);

			static FromMilliseconds(f64 milliseconds) : TimeSpan => Interval(milliseconds, 10000.0);

			static FromMicroseconds(f64 microseconds) : TimeSpan => Interval(microseconds, 10.0);

			static FromTicks(i64 ticks) : TimeSpan => new TimeSpan(ticks);

			Add(TimeSpan other) : TimeSpan
			{
				i64 sum = ticks + other.ticks;
				if (((ticks ^ sum) & (other.ticks ^ sum)) < 0)
					TooLong();
				return new TimeSpan(sum);
			}

			Subtract(TimeSpan other) : TimeSpan
			{
				i64 difference = ticks - other.ticks;
				if (((ticks ^ other.ticks) & (ticks ^ difference)) < 0)
					TooLong();
				return new TimeSpan(difference);
			}

			Negate() : TimeSpan
			{
				if (ticks == -9223372036854775808)
					TooLong();
				return new TimeSpan(-ticks);
			}

			/// The length without its sign.
			Duration() : TimeSpan => ticks < 0 ? Negate() : this;

			/// Times a number, and divided by one (to the nearest tick, halves to even).
			Multiply(f64 factor) : TimeSpan
			{
				if (factor != factor)
					NotANumber();
				return Interval(Math.Round((f64)ticks * factor), 1.0);
			}

			Divide(f64 divisor) : TimeSpan
			{
				if (divisor != divisor)
					NotANumber();
				return Interval(Math.Round((f64)ticks / divisor), 1.0);
			}

			/// How many times `other` fits in this length.
			Divide(TimeSpan other) : f64 => (f64)ticks / (f64)other.ticks;

			op +(TimeSpan a, TimeSpan b) : TimeSpan => a.Add(b);

			op -(TimeSpan a, TimeSpan b) : TimeSpan => a.Subtract(b);

			op -(TimeSpan a) : TimeSpan => a.Negate();

			op +(TimeSpan a) : TimeSpan => a;

			op *(TimeSpan a, f64 factor) : TimeSpan => a.Multiply(factor);

			op *(f64 factor, TimeSpan a) : TimeSpan => a.Multiply(factor);

			op /(TimeSpan a, f64 divisor) : TimeSpan => a.Divide(divisor);

			op /(TimeSpan a, TimeSpan b) : f64 => a.Divide(b);

			op ==(TimeSpan a, TimeSpan b) : bool => a.ticks == b.ticks;

			op !=(TimeSpan a, TimeSpan b) : bool => a.ticks != b.ticks;

			op <(TimeSpan a, TimeSpan b) : bool => a.ticks < b.ticks;

			op <=(TimeSpan a, TimeSpan b) : bool => a.ticks <= b.ticks;

			op >(TimeSpan a, TimeSpan b) : bool => a.ticks > b.ticks;

			op >=(TimeSpan a, TimeSpan b) : bool => a.ticks >= b.ticks;

			Equals(TimeSpan other) : bool => ticks == other.ticks;

			Hash() : u64 => (u64)ticks * 11400714819323198485;

			CompareTo(TimeSpan other) : i32 => ticks < other.ticks ? -1 : ticks > other.ticks ? 1 : 0;

			static Compare(TimeSpan a, TimeSpan b) : i32 => a.CompareTo(b);

			/// `[-][d.]hh:mm:ss[.fffffff]` (the "c" format).
			ToString() : str => TimeFormat.Span(this, "c");

			/// As text by a format: "c" (`[-][d.]hh:mm:ss[.fffffff]`), "g" (`[-][d:]h:mm:ss[.FFFFFFF]`), "G"
			/// (`[-]d:hh:mm:ss.fffffff`), or a custom one of d…, h, hh, m, mm, s, ss, f…, F…, 'text' and \x.
			ToString(str format) : str => TimeFormat.Span(this, format);

			/// The length `text` writes: `[-]d`, `[-][d.]h:mm[:ss[.fffffff]]` or `[-]d:h:mm:ss[.fffffff]`, spaces around
			/// allowed; a FormatError when it writes none.
			static Parse(str text) : TimeSpan
			{
				TimeSpan? read = TimeFormat.ReadSpan(text);
				if (read == null)
					throw new FormatError("String '" + text + "' was not recognized as a valid TimeSpan.");
				return read.Value;
			}

			/// The length `text` writes, or null when it writes none.
			static TryParse(str text) : TimeSpan? => TimeFormat.ReadSpan(text);

			static i64 TicksPerMicrosecond
			{
				get => 10;
			}

			static i64 TicksPerMillisecond
			{
				get => 10000;
			}

			static i64 TicksPerSecond
			{
				get => 10000000;
			}

			static i64 TicksPerMinute
			{
				get => 600000000;
			}

			static i64 TicksPerHour
			{
				get => 36000000000;
			}

			static i64 TicksPerDay
			{
				get => 864000000000;
			}

			static TimeSpan Zero
			{
				get => new TimeSpan(0);
			}

			static TimeSpan MaxValue
			{
				get => new TimeSpan(9223372036854775807);
			}

			static TimeSpan MinValue
			{
				get => new TimeSpan(-9223372036854775808);
			}

			i64 Ticks
			{
				get => ticks;
			}

			/// The parts: whole days, then hours (0–23), minutes, seconds, milliseconds, microseconds, nanoseconds of the
			/// rest (negative for a negative length).
			i32 Days
			{
				get => (i32)(ticks / 864000000000);
			}

			i32 Hours
			{
				get => (i32)(ticks / 36000000000 % 24);
			}

			i32 Minutes
			{
				get => (i32)(ticks / 600000000 % 60);
			}

			i32 Seconds
			{
				get => (i32)(ticks / 10000000 % 60);
			}

			i32 Milliseconds
			{
				get => (i32)(ticks / 10000 % 1000);
			}

			i32 Microseconds
			{
				get => (i32)(ticks / 10 % 1000);
			}

			i32 Nanoseconds
			{
				get => (i32)(ticks % 10) * 100;
			}

			/// The whole length in one unit.
			f64 TotalDays
			{
				get => (f64)ticks / 864000000000.0;
			}

			f64 TotalHours
			{
				get => (f64)ticks / 36000000000.0;
			}

			f64 TotalMinutes
			{
				get => (f64)ticks / 600000000.0;
			}

			f64 TotalSeconds
			{
				get => (f64)ticks / 10000000.0;
			}

			/// (Held to ±922337203685477, as .NET does.)
			f64 TotalMilliseconds
			{
				get
				{
					f64 total = (f64)ticks / 10000.0;
					if (total > 922337203685477.0)
						return 922337203685477.0;
					if (total < -922337203685477.0)
						return -922337203685477.0;
					return total;
				}
			}

			f64 TotalMicroseconds
			{
				get => (f64)ticks / 10.0;
			}

			f64 TotalNanoseconds
			{
				get => (f64)ticks * 100.0;
			}
	}
}
Esc