Class DateTimeDecoratorExtensions
- Namespace
- Cuemon
- Assembly
- Cuemon.Core.dll
Extension methods for the DateTime struct hidden behind the IDecorator<T> interface.
public static class DateTimeDecoratorExtensions
- Inheritance
-
DateTimeDecoratorExtensions
Examples
DateTimeDecoratorExtensions provides extension methods for converting between DateTime and Unix epoch time and for switching DateTimeKind without altering the underlying ticks. This example retrieves the Unix epoch via Decorator.Syntactic<DateTime>().GetUnixEpoch(), converts DateTime.UtcNow to seconds since epoch with ToUnixEpochTime, and transforms DateTimeKind between Utc, Local, and Unspecified using ToUtcKind, ToLocalKind, and ToDefaultKind. Key setup includes creating UTC and local DateTime values, then verifying each kind-only transformation preserves the tick count. Console output shows the epoch value, Unix timestamp, and confirms Kind changes while Ticks == localTime.Ticks remains True.
using System;
using Cuemon;
namespace MyApp.DateTimeExamples
{
public class DateTimeDecoratorExtensionsExample
{
public void Demonstrate()
{
// Get the Unix epoch (January 1st, 1970 UTC)
var unixEpoch = Decorator.Syntactic<DateTime>().GetUnixEpoch();
Console.WriteLine(unixEpoch); // 1/1/1970 12:00:00 AM
// Convert a DateTime to Unix epoch time (seconds since 1970-01-01 UTC)
var utcNow = DateTime.UtcNow;
var unixTime = Decorator.Enclose(utcNow).ToUnixEpochTime();
Console.WriteLine(unixTime); // e.g., 1700000000
// Convert from local DateTime to UTC-kind DateTime (same ticks, different kind)
var localTime = new DateTime(2024, 6, 15, 12, 0, 0, DateTimeKind.Local);
var utcKind = Decorator.Enclose(localTime).ToUtcKind();
Console.WriteLine(utcKind.Kind); // Utc
Console.WriteLine(utcKind.Ticks == localTime.Ticks); // True
// Convert from UTC DateTime to local-kind DateTime
var utcTime = new DateTime(2024, 6, 15, 10, 0, 0, DateTimeKind.Utc);
var localKind = Decorator.Enclose(utcTime).ToLocalKind();
Console.WriteLine(localKind.Kind); // Local
Console.WriteLine(localKind.Ticks == utcTime.Ticks); // True
// Strip kind information (set to Unspecified)
var defaultKind = Decorator.Enclose(utcTime).ToDefaultKind();
Console.WriteLine(defaultKind.Kind); // Unspecified
Console.WriteLine(defaultKind.Ticks == utcTime.Ticks); // True
}}
}
Methods
GetUnixEpoch(IDecorator<DateTime>)
Gets a DateTime initialized to midnight, January 1st, 1970 in Coordinated Universal Time (UTC).
public static DateTime GetUnixEpoch(this IDecorator<DateTime> _)
Parameters
_IDecorator<DateTime>The IDecorator<T> to extend.
Returns
- DateTime
A a DateTime initialized to midnight, January 1st, 1970 in Coordinated Universal Time (UTC).
ToDefaultKind(IDecorator<DateTime>)
Converts the enclosed DateTime of the decorator to a representation that is not specified as either local time or UTC.
public static DateTime ToDefaultKind(this IDecorator<DateTime> decorator)
Parameters
decoratorIDecorator<DateTime>The IDecorator<T> to extend.
Returns
- DateTime
A new DateTime value initialized to Unspecified that has the same number of ticks as the enclosed DateTime of the
decorator.
Exceptions
- ArgumentNullException
decoratorcannot be null.
ToLocalKind(IDecorator<DateTime>)
Converts the enclosed DateTime of the decorator to a local time representation.
public static DateTime ToLocalKind(this IDecorator<DateTime> decorator)
Parameters
decoratorIDecorator<DateTime>The IDecorator<T> to extend.
Returns
- DateTime
A new DateTime value initialized to Local that has the same number of ticks as the enclosed DateTime of the
decorator.
Exceptions
- ArgumentNullException
decoratorcannot be null.
ToUnixEpochTime(IDecorator<DateTime>)
Converts the enclosed DateTime of the decorator to an equivalent UNIX Epoch time representation.
public static double ToUnixEpochTime(this IDecorator<DateTime> decorator)
Parameters
decoratorIDecorator<DateTime>The IDecorator<T> to extend.
Returns
Remarks
This implementation converts the enclosed DateTime of the decorator to an UTC representation ONLY if the Kind equals Local.
ToUtcKind(IDecorator<DateTime>)
Converts the enclosed DateTime of the decorator to a Coordinated Universal Time (UTC) representation.
public static DateTime ToUtcKind(this IDecorator<DateTime> decorator)
Parameters
decoratorIDecorator<DateTime>The IDecorator<T> to extend.
Returns
- DateTime
A new DateTime value initialized to Utc that has the same number of ticks as the object represented by the enclosed DateTime of the
decorator.
Exceptions
- ArgumentNullException
decoratorcannot be null.