CoordinatedUniversalTime Class
Definition
- Namespace
- Codebelt.SharedKernel
- Assemblies
- Codebelt.SharedKernel.dll
Represents a CoordinatedUniversalTime (UTC) object that can be used when you need a timestamp that is based on an absolute time.
public record CoordinatedUniversalTime : ComparableValueObject<DateTime>, IEquatable<ValueObject>, IEquatable<SingleValueObject<DateTime>>, IComparisonOperators<ComparableValueObject<DateTime>, ComparableValueObject<DateTime>, bool>, IEqualityOperators<ComparableValueObject<DateTime>, ComparableValueObject<DateTime>, bool>, IEquatable<ComparableValueObject<DateTime>>, IEquatable<CoordinatedUniversalTime>
- Inheritance
-
objectCoordinatedUniversalTime
- Implements
- Inherited Members
-
SingleValueObject<DateTime>.GetHashCode()SingleValueObject<DateTime>.Equals(object)SingleValueObject<DateTime>.Equals(ValueObject)SingleValueObject<DateTime>.Equals(SingleValueObject<DateTime>)SingleValueObject<DateTime>.<Clone>$()SingleValueObject<DateTime>.EqualityContract
Examples
MinValue is 0001-01-01T00:00:00.0000000Z on every host. Its ticks are zero and its DateTime.Kind is Utc; the local time zone does not shift this boundary.
The following example captures the current UTC moment as a CoordinatedUniversalTime and converts it back to a DateTime for downstream consumers, and parses a wire-format ISO 8601 string the same way. Use CoordinatedUniversalTime.Now for "now" snapshots and FromString when an external system hands you a UTC timestamp that still needs validation.
using System;
using Codebelt.SharedKernel;
namespace EventPublishingSample;
public static class EventTimestamps
{
public static DateTime CaptureAndRepublish(string wireTimestamp)
{
CoordinatedUniversalTime current = CoordinatedUniversalTime.Now();
CoordinatedUniversalTime parsed = CoordinatedUniversalTime.FromString(wireTimestamp);
// Both values are guaranteed to be DateTimeKind.Utc and can flow into JSON or storage as DateTime.
return current > parsed ? current : parsed;
}
}
Remarks
Constructors
| Name | Description |
|---|---|
| CoordinatedUniversalTime(DateTime) | Initializes a new instance of the CoordinatedUniversalTime class. |
Fields
| Name | Description |
|---|---|
| MaxValue | Denotes the largest possible value of CoordinatedUniversalTime. |
| MinValue | Denotes the smallest possible value of CoordinatedUniversalTime, at midnight on January 1, 0001 UTC, regardless of the local time zone. |
Methods
| Name | Description |
|---|---|
| FromString(string) | Converts the string representation of a date and time to its CoordinatedUniversalTime> equivalent. |
| Now() | Returns a CoordinatedUniversalTime whose value is the current UTC date and time. |
| ToString() | Returns a string> that represents this instance and complies with ISO 8601. |
Operators
| Name | Description |
|---|---|
| implicit operator DateTime(CoordinatedUniversalTime) | Performs an implicit conversion from CoordinatedUniversalTime to DateTime. |
| implicit operator CoordinatedUniversalTime(DateTime) | Performs an implicit conversion from DateTime to CoordinatedUniversalTime. |
| implicit operator CoordinatedUniversalTime(DateTimeOffset) | Performs an implicit conversion from DateTimeOffset to CoordinatedUniversalTime. |