76 lines
2.8 KiB
C#
76 lines
2.8 KiB
C#
using System;
|
|
using System.Globalization;
|
|
|
|
namespace Xylem.Common.Utils.DateTimeServer
|
|
{
|
|
|
|
/// <summary>
|
|
/// Class for international time settings and conversions
|
|
/// </summary>
|
|
|
|
public static class DateTimeServer
|
|
{
|
|
/// <summary>
|
|
/// Convert date time offset to local date as string.
|
|
/// </summary>
|
|
/// <param name="dto">date time offset needed to extract the local date</param>
|
|
/// <param name="cI">culture info for local information of date and time offset</param>
|
|
/// <returns>Date string</returns>
|
|
/// <remarks date="2021-Feb-25" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public static String GetDateStringFromDateTimeOffset(DateTimeOffset dto, CultureInfo cI)
|
|
{
|
|
return dto.ToString("dd-MMM-yyyy", cI);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert date string to date time offset
|
|
/// </summary>
|
|
/// <param name="date">as string</param>
|
|
/// <returns>validation date as date time offset</returns>
|
|
/// <remarks date="2021-Feb-25" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public static DateTimeOffset GetDateTimeOffsetDateString(String date)
|
|
{
|
|
try
|
|
{
|
|
var dateTime = DateTime.Parse(date);
|
|
var localTimeOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
|
|
return new DateTimeOffset(dateTime.Year, dateTime.Month, dateTime.Day,
|
|
23, 59, 59, localTimeOffset);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return new DateTimeOffset(2222, 2, 22, 22, 22, 22, new TimeSpan(0, 0, 0));
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Set a validation date to the current local date time
|
|
/// </summary>
|
|
/// <param name="validDays">days to extend the validation</param>
|
|
/// <returns>validation date as date time offset</returns>
|
|
/// <remarks date="2021-Feb-25" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public static DateTimeOffset SetValidationDate(Double validDays)
|
|
{
|
|
try
|
|
{
|
|
var localDateTime = DateTime.Now;
|
|
var localTimeOffset = TimeZone.CurrentTimeZone.GetUtcOffset(localDateTime);
|
|
var todayValidDateTime = new DateTimeOffset(localDateTime.Year, localDateTime.Month, localDateTime.Day,
|
|
23, 59, 59, localTimeOffset);
|
|
|
|
// add the validate date to the current time span
|
|
return todayValidDateTime.AddDays(validDays);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return new DateTimeOffset(2222, 2, 22, 22, 22, 22, new TimeSpan(0, 0, 0));
|
|
}
|
|
}
|
|
}
|
|
}
|