using System;
using System.Globalization;
namespace Xylem.Common.Utils.DateTimeServer
{
///
/// Class for international time settings and conversions
///
[Serializable]
public static class DateTimeServer
{
///
/// Convert date time offset to local date as string.
///
/// date time offset needed to extract the local date
/// culture info for local information of date and time offset
/// Date string
///
/// - Initial.
///
public static String GetDateStringFromDateTimeOffset(DateTimeOffset dto, CultureInfo cI)
{
var date = dto.Date;
return date.ToShortDateString();
}
///
/// Convert date time to sortable string.
///
/// date time offset needed to extract the local date
/// culture info for local information of date and time offset
/// Date string
///
/// - Initial.
///
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);
}
///
/// Convert date string to date time.
///
/// as string
///
/// validation date as date time offset
///
/// - Initial.
///
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;
}
}
///
/// Convert date string to date time offset adding always 23:59:59
///
/// as string
/// validation date as date time offset
///
/// - Initial.
///
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;
}
}
///
/// Set a validation date to the current local date time
///
/// days to extend the validation
/// validation date as date time offset
///
/// - Initial.
///
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;
}
}
///
/// 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!
///
/// date time of today until midnight
///
/// - Initial.
///
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;
}
}
}
}