153 lines
5.6 KiB
C#
153 lines
5.6 KiB
C#
using System;
|
|
using System.Globalization;
|
|
|
|
namespace Xylem.Common.Utils.DateTimeServer
|
|
{
|
|
|
|
/// <summary>
|
|
/// Class for international time settings and conversions
|
|
/// </summary>
|
|
[Serializable]
|
|
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)
|
|
{
|
|
var date = dto.Date;
|
|
return date.ToShortDateString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert date time to sortable string.
|
|
/// </summary>
|
|
/// <param name="dt">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="2022-Jun-30" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public static String GetSortableDateStringFromDateTime(DateTime dt, CultureInfo cI)
|
|
{
|
|
var culturePattern = @"yyyy/MM/dd";
|
|
if (cI.IetfLanguageTag.Contains("de-"))
|
|
{
|
|
culturePattern = @"yyyy-MM-dd";
|
|
}
|
|
return dt.ToString(culturePattern);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert date string to date time.
|
|
/// </summary>
|
|
/// <param name="date">as string</param>
|
|
/// <param name="cI"></param>
|
|
/// <returns>validation date as date time offset</returns>
|
|
/// <remarks date="2021-Mar-23" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public static DateTime GetDateTimeFromDateString(String date, CultureInfo cI)
|
|
{
|
|
try
|
|
{
|
|
if (DateTime.TryParse(date, out var localDateTime))
|
|
{
|
|
var localTimeOffset = TimeZone.CurrentTimeZone.GetUtcOffset(localDateTime);
|
|
var dateTimeOffset = new DateTimeOffset(localDateTime, localTimeOffset);
|
|
var dateTime = dateTimeOffset.DateTime;
|
|
return dateTime;
|
|
}
|
|
|
|
return DateTime.Now;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return DateTime.Now;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert date string to date time offset adding always 23:59:59
|
|
/// </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 GetDateTimeOffsetFromDateString(String date)
|
|
{
|
|
try
|
|
{
|
|
if (DateTime.TryParse(date, out var dateTime))
|
|
{
|
|
var localTimeOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
|
|
return new DateTimeOffset(dateTime.Year, dateTime.Month, dateTime.Day,
|
|
23, 59, 59, localTimeOffset);
|
|
}
|
|
return DateTimeOffset.Now;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return DateTimeOffset.Now;
|
|
}
|
|
}
|
|
/// <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 DateTimeOffset.Now;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Get the data time until midnight for date time validation purposes as otherwise
|
|
/// the time may count up and today is always false in comparison wit DataTime.Now!
|
|
/// </summary>
|
|
/// <returns>date time of today until midnight</returns>
|
|
/// <remarks date="2021-Apr-14" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
public static DateTime GetDateTimeTodayUntilMidnight()
|
|
{
|
|
try
|
|
{
|
|
var localDateTime = DateTime.Now;
|
|
var localTimeOffset = TimeZone.CurrentTimeZone.GetUtcOffset(localDateTime);
|
|
var todayUntilMidnightDateTime = new DateTimeOffset(localDateTime.Year, localDateTime.Month,
|
|
localDateTime.Day, 23, 59, 59, localTimeOffset);
|
|
|
|
var dateTime = todayUntilMidnightDateTime.DateTime;
|
|
return dateTime;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return DateTime.Now;
|
|
}
|
|
}
|
|
}
|
|
}
|