Table of Contents

Class DateTimeHandling

Namespace
ArcanaDevelopment.adTempus.Shared
Assembly
ArcanaDevelopment.adTempus.Shared.dll
public static class DateTimeHandling
Inheritance
object
DateTimeHandling

Properties

Now

Gets the current date/time

public static DateTimeOffset Now { get; }

Property Value

DateTimeOffset

Remarks

This method is used throughout the code rather than a direct call to DateTimeOffset.Now or DateTime.Now so that the time and/or timezone can be changed for testing.

UtcNow

Gets the current date/time

public static DateTimeOffset UtcNow { get; }

Property Value

DateTimeOffset

Remarks

This method is used throughout the code rather than a direct call to DateTimeOffset.Now or DateTime.Now so that the time and/or timezone can be changed for testing.

Methods

Abs(TimeSpan)

Returns the absolute value of the given TimeSpan (negative time is converted to positive)

public static TimeSpan Abs(TimeSpan value)

Parameters

NameTypeDescription
value TimeSpan

Returns

TimeSpan

CalculateEaster(int)

Calculates the date of Easter for a given year.

public static DateTime CalculateEaster(int year)

Parameters

NameTypeDescription
year int

Returns

DateTime

Remarks

Easter calculation from http://www.assa.org.au/edm.html#Computer by Ronald W. Mallen (Astronomical Society of South Australia)

ComDateToDateTime(object)

public static DateTime? ComDateToDateTime(object value)

Parameters

NameTypeDescription
value object

Returns

DateTime?

Combine(DateTime, DateTime)

Combines two DateTime values, using the date from one value and the time from the other.

public static DateTime Combine(DateTime datePortion, DateTime timePortion)

Parameters

NameTypeDescription
datePortion DateTime
timePortion DateTime

Returns

DateTime

Remarks

Returns a new DateTime value with the date set to the date from the datePortion, the hours and minutes set from the time portion, and seconds and milliseconds set to 0.

CompareDateTime(DateTimeOffset?, DateTimeOffset?)

Compares two nullable DateTime values

public static int CompareDateTime(DateTimeOffset? x, DateTimeOffset? y)

Parameters

NameTypeDescription
x DateTimeOffset?
y DateTimeOffset?

Returns

int

CompareDateTime(DateTime?, DateTime?)

Compares two nullable DateTime values

public static int CompareDateTime(DateTime? x, DateTime? y)

Parameters

NameTypeDescription
x DateTime?
y DateTime?

Returns

int

CompareTime(DateTime, DateTime)

Compares only the time portion of two DateTime values (hour and minute only; milliseconds are ignored).

public static int CompareTime(DateTime value1, DateTime value2)

Parameters

NameTypeDescription
value1 DateTime
value2 DateTime

Returns

int

ConvertTimeZone(DateTime, int)

public static DateTime ConvertTimeZone(DateTime src, int targetTZBias)

Parameters

NameTypeDescription
src DateTime
targetTZBias int

Returns

DateTime

ConvertTimeZone(DateTime, int, int)

public static DateTime ConvertTimeZone(DateTime src, int srcTZBias, int targetTZBias)

Parameters

NameTypeDescription
src DateTime
srcTZBias int
targetTZBias int

Returns

DateTime

CurrentTZBias()

public static int CurrentTZBias()

Returns

int

DateTimeFromXml(string)

public static DateTime? DateTimeFromXml(string readValue)

Parameters

NameTypeDescription
readValue string

Returns

DateTime?

DateTimeToComDate(DateTime)

public static DateTime DateTimeToComDate(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

DateTime

DateTimeToComDate(DateTimeOffset)

public static DateTime DateTimeToComDate(DateTimeOffset value)

Parameters

NameTypeDescription
value DateTimeOffset

Returns

DateTime

DateTimeToComDate(DateTimeOffset?)

public static object DateTimeToComDate(DateTimeOffset? value)

Parameters

NameTypeDescription
value DateTimeOffset?

Returns

object

DateTimeToComDate(DateTime?)

public static object DateTimeToComDate(DateTime? value)

Parameters

NameTypeDescription
value DateTime?

Returns

object

DateTimeToXml(DateTime)

public static string DateTimeToXml(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

string

DateTimeToXml(DateTimeOffset?)

public static string DateTimeToXml(DateTimeOffset? value)

Parameters

NameTypeDescription
value DateTimeOffset?

Returns

string

DayOfWeek(DateTime)

Returns an integer corresponding to the day of the week for the specified date, with Sunday=1, Monday=2, ..., Saturday=7

public static Weekday DayOfWeek(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

Weekday

Remarks

This method converts from the DayOfWeek value returned by DateTime.DayOfWeek, which uses Sunday=0.

DescribeDateRange(DateTime?, DateTime?)

public static string DescribeDateRange(DateTime? startDate, DateTime? endDate)

Parameters

NameTypeDescription
startDate DateTime?
endDate DateTime?

Returns

string

EndOfDay(DateTime)

Returns a DateTime value representing 11:59:59 PM on the source day.

public static DateTime EndOfDay(DateTime src)

Parameters

NameTypeDescription
src DateTime

Returns

DateTime

FiletimeToDateTime(FILETIME)

public static DateTime FiletimeToDateTime(FILETIME fileTime)

Parameters

NameTypeDescription
fileTime FILETIME

Returns

DateTime

FiletimeToTimeSpan(FILETIME)

public static TimeSpan FiletimeToTimeSpan(FILETIME fileTime)

Parameters

NameTypeDescription
fileTime FILETIME

Returns

TimeSpan

FirstDayOfMonth(DateTime)

Returns a DateTime value representing the first day of the month that includes the source date.

public static DateTime FirstDayOfMonth(DateTime date)

Parameters

NameTypeDescription
date DateTime

Returns

DateTime

FirstDayOfYear(DateTime)

Gets a DateTime representing January 1 of the year of a specified date.

public static DateTime FirstDayOfYear(DateTime date)

Parameters

NameTypeDescription
date DateTime

Returns

DateTime

FirstWeekdayOfMonth(DateTime)

Returns an integer indicating what day is the first day of the month in which the specified date occurs (Sunday=1...Saturday=7).

public static Weekday FirstWeekdayOfMonth(DateTime date)

Parameters

NameTypeDescription
date DateTime

Returns

Weekday

FormatSeconds(int)

Formats a number of seconds into a more readable format.

public static string FormatSeconds(int numberOfSeconds)

Parameters

NameTypeDescription
numberOfSeconds int

Returns

string

Remarks

FormatTimespan(TimeSpan?)

Formats a timespan into a readable format.

public static string FormatTimespan(TimeSpan? ts)

Parameters

NameTypeDescription
ts TimeSpan?

The timespan to format

Returns

string

Remarks

If ts.TotalSeconds is less than 60 and ts.Milliseconds=0, returns "Xs".
If ts.TotalSeconds is less than 60 and ts.Milliseconds!=0, returns "X.YYYs".
If ts.TotalSeconds is less than 3600, returns minutes and seconds "Xm:YYs".
If ts.TotalSeconds is greater than 3600, returns hours, minutes, and seconds "Xh:YYm:ZZs". If ts.TotalSeconds is greater than 24 hours, returns days, hours, minutes, and seconds "Dd:Xh:YYm:ZZs".

FormatTimestamp(DateTime, bool)

public static string FormatTimestamp(DateTime value, bool includeSeconds)

Parameters

NameTypeDescription
value DateTime
includeSeconds bool

Returns

string

FormatTimestamp(DateTimeOffset, bool)

public static string FormatTimestamp(DateTimeOffset value, bool includeSeconds)

Parameters

NameTypeDescription
value DateTimeOffset
includeSeconds bool

Returns

string

IsEndOfDay(DateTime)

Determines whether the time portion of a DateTime is 11:59pm (seconds ignored)

public static bool IsEndOfDay(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

bool

IsLastDayOfMonth(DateTime)

Determines whether a date value is the last day of the month.

public static bool IsLastDayOfMonth(DateTime val)

Parameters

NameTypeDescription
val DateTime

Returns

bool

IsLastOccurrenceInMonth(DateTime)

Indicates whether a date is the last occurrence of a given weekday in the month. For example, if the value is a Monday, this method returns True if the date is the last Monday of the month, or False if there are other Mondays after it.

public static bool IsLastOccurrenceInMonth(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

bool

IsStartOfDay(DateTime)

Determines whether the time portion of a DateTime is 12:00am (seconds ignored)

public static bool IsStartOfDay(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

bool

LastDayOfMonth(DateTime)

Returns a DateTime representing the last day of the month in which the specified date occurs.

public static DateTime LastDayOfMonth(DateTime date)

Parameters

NameTypeDescription
date DateTime

Returns

DateTime

LastDayOfMonth(int, int)

Returns a DateTime representing the last day of the specified month.

public static DateTime LastDayOfMonth(int year, int month)

Parameters

NameTypeDescription
year int
month int

Returns

DateTime

NextMinute(DateTime)

Gets the start of the next minute

public static DateTime NextMinute(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

DateTime

NextMinute(DateTimeOffset)

Gets the start of the next minute

public static DateTimeOffset NextMinute(DateTimeOffset value)

Parameters

NameTypeDescription
value DateTimeOffset

Returns

DateTimeOffset

NullComDate()

public static object NullComDate()

Returns

object

OccurrenceInMonth(DateTime)

Indicates what occurrence of a given day of the week the specified date represents. For example, if the value is a Monday, this method determines whether it is the first, second, third, fourth, or fifth Monday of the month.

public static int OccurrenceInMonth(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

int

ParseRFC822Offset(string)

Parses an RFC822 timezone offset and returns total minutes.

public static int ParseRFC822Offset(string offset)

Parameters

NameTypeDescription
offset string

Returns

int

Remarks

The source offset is in the HHmm, optionally preceded by a + or -.

ReadDateTimeOffset(DateTime?, int?)

Creates a DateTimeOffset from separate DateTime and timezone offset values used for persistence.

public static DateTimeOffset? ReadDateTimeOffset(DateTime? sourceDateTime, int? tzOffset)

Parameters

NameTypeDescription
sourceDateTime DateTime?
tzOffset int?

Returns

DateTimeOffset?

Remarks

DateTimeOffset values are stored in the database as a UTC date/time value and a timezone offset. This method re-creates the DateTimeOffset from the read values.

RemoveSeconds(DateTime)

Returns a new DateTime value identical to the source but with seconds set to 0.

public static DateTime RemoveSeconds(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

DateTime

RemoveSeconds(DateTimeOffset)

Returns a new DateTimeOffset value identical to the source but with seconds set to 0.

public static DateTimeOffset RemoveSeconds(DateTimeOffset value)

Parameters

NameTypeDescription
value DateTimeOffset

Returns

DateTimeOffset

RepresentsFullYear(DateTime?, DateTime?)

Indicates whether the specified pair of dates is January 1 to December 31 of the same year.

public static bool RepresentsFullYear(DateTime? StartDate, DateTime? EndDate)

Parameters

NameTypeDescription
StartDate DateTime?
EndDate DateTime?

Returns

bool

ShortDateTime(DateTime?)

public static string ShortDateTime(DateTime? val)

Parameters

NameTypeDescription
val DateTime?

Returns

string

StartOfDay(DateTime)

Returns a DateTime value representing 12:00:00 AM on the source day.

public static DateTime StartOfDay(DateTime src)

Parameters

NameTypeDescription
src DateTime

Returns

DateTime

TimeFromXml(string)

public static DateTime? TimeFromXml(string source)

Parameters

NameTypeDescription
source string

Returns

DateTime?

TimeString(DateTime)

Returns the time portion of the specified value, in short time format.

public static string TimeString(DateTime source)

Parameters

NameTypeDescription
source DateTime

Returns

string

TimeToXml(DateTime?)

public static string TimeToXml(DateTime? value)

Parameters

NameTypeDescription
value DateTime?

Returns

string

TimeValue(DateTime)

Returns a DateTime containing only the time portion from the source value. The date portion is set to January 1,1900.

public static DateTime TimeValue(DateTime src)

Parameters

NameTypeDescription
src DateTime

Returns

DateTime

TimeValue(int, int)

Returns a DateTime representing the time portion only for the specified hour and minute

public static DateTime TimeValue(int hour, int minute)

Parameters

NameTypeDescription
hour int
minute int

Returns

DateTime

Remarks

The Date portion of the value is set to January 1, 1900. Seconds and milliseconds are set to 0.

TimeValue(DateTime?)

public static DateTime? TimeValue(DateTime? src)

Parameters

NameTypeDescription
src DateTime?

Returns

DateTime?

ToUTCDateTime(DateTimeOffset)

Returns a DateTime value storing the sourceDateTime converted to UTC, but with the DateTimeKind==unspecified.

public static DateTime ToUTCDateTime(DateTimeOffset sourceDateTime)

Parameters

NameTypeDescription
sourceDateTime DateTimeOffset

Returns

DateTime

Remarks

If we use DateTimeOffset.UTCDateTime to create a UTC DateTime, it has the Kind set to UTC. If we later try to round-trip that DateTime back to a DateTimeOffset using ReadDateTimeOffset and a stored offset, DateTimeOffset throws an exception because it only accepts an offset of 0 for a UTC date time. So we have to do the conversion to UTC ourselves here.

WeekOfMonth(DateTime)

Indicates what week of the month a value falls in.

public static int WeekOfMonth(DateTime value)

Parameters

NameTypeDescription
value DateTime

Returns

int