Saxonite v1.1.5

This commit is contained in:
2021-08-19 17:31:34 +01:00
parent c1a2e2d097
commit 7665068107
3 changed files with 69 additions and 56 deletions
@@ -45,10 +45,10 @@ namespace Sarand.Net.Standard.Base.Core
/// Add item to the list.
/// <para>Null items are ignored.</para>
/// </summary>
public new void Add (T item)
public new void Add(T item)
{
if (item != null)
base.Add(item);
base.Add(item);
}
//-------------------------------------------------------------------
+66 -54
View File
@@ -18,25 +18,28 @@ namespace Sarand.Net.Standard.Base.Utils
public static String Format_XmlTime { get; } = "HH:mm:ss";
public static String Delim_XmlTime { get; } = "T";
public static String Format_XmlDateTime { get; } = Format_XmlDate + Delim_XmlTime + Format_XmlTime;
public static String Format_JavascriptDate { get; } = "yyyy-MM-dd";
public static String Format_JavascriptTime { get; } = "HH:mm:ss";
public static String Delim_JavascriptTime { get; } = "T";
//-------------------------------------------------------------------------
// P o p u l a t i o n
//-------------------------------------------------------------------------
/// <summary>Combine date and time.</summary>
public static DateTime SetTime (this DateTime value, DateTime time)
public static DateTime SetTime(this DateTime value, DateTime time)
{
if (!IsValid(value)) return value;
return value.Date.Add(time.TimeOfDay);
}
/// <summary>Combine date and time.</summary>
public static DateTime SetTime (this DateTime value, TimeSpan time)
public static DateTime SetTime(this DateTime value, TimeSpan time)
{
if (!IsValid(value)) return value;
return value.Date + time;
}
//-------------------------------------------------------------------------
public static DateTime ClearSeconds (this DateTime value)
public static DateTime ClearSeconds(this DateTime value)
{
return new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute, 0, value.Kind);
}
@@ -56,7 +59,7 @@ namespace Sarand.Net.Standard.Base.Utils
/// <para>Seconds can be included in the algorythm but are cleared in the return value.</para>
/// <para>Interval minutes are capped at 60.</para>
/// </summary>
public static DateTime RoundTime (this DateTime value, ushort intervalMinutes, RoundingDirection direction, bool ignoreSeconds = true)
public static DateTime RoundTime(this DateTime value, ushort intervalMinutes, RoundingDirection direction, bool ignoreSeconds = true)
{
intervalMinutes = (ushort)Math.Min(60, (int)intervalMinutes);
// Number of minutes over the interval.
@@ -87,28 +90,28 @@ namespace Sarand.Net.Standard.Base.Utils
/// Parse date value.
/// <para>Format: eg. 'yyyyMMdd', 'dd/MM/yyyy'</para>
/// </summary>
public static DateTime ParseExact (String value, String format)
public static DateTime ParseExact(String value, String format, CultureInfo culture = null)
{
try
{
return DateTime.ParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.None);
return DateTime.ParseExact(value, format, culture ?? CultureInfo.InvariantCulture, DateTimeStyles.None);
}
catch (Exception) { }
return DateTime.MinValue;
}
//-------------------------------------------------------------------------
/// <summary>Parse XML date: 'yyyy-MM-dd'</summary>
public static DateTime ParseXmlDate (String value)
public static DateTime ParseXmlDate(String value)
{
return ParseExact(value, Format_XmlDate);
}
/// <summary>Parse XML time: 'HH:mm:ss'</summary>
public static DateTime ParseXmlTime (String value)
public static DateTime ParseXmlTime(String value)
{
return ParseExact(value, Format_XmlTime);
}
/// <summary>Parse XML datetime: 'yyyy-MM-ddTHH:mm:ss'</summary>
public static DateTime ParseXmlDateTime (String value)
public static DateTime ParseXmlDateTime(String value)
{
return ParseExact(value, Format_XmlDateTime);
}
@@ -117,21 +120,21 @@ namespace Sarand.Net.Standard.Base.Utils
// V a l i d a t i o n
//-------------------------------------------------------------------------
/// <summary>Vaidate that date is between MinDate & MaxDate.</summary>
public static bool IsValid (this DateTime value)
public static bool IsValid(this DateTime value)
{
if (value <= DateTime.MinValue) return false;
if (value >= DateTime.MaxValue) return false;
return true;
}
/// <summary>Vaidate that date is between MinDate & MaxDate and after null date.</summary>
public static bool IsValid (this DateTime value, DateTime nullDate)
public static bool IsValid(this DateTime value, DateTime nullDate)
{
if (!IsValid(value)) return false;
if (value <= nullDate) return false;
return true;
}
/// <summary>Vaidate that date is not null and between MinDate & MaxDate.</summary>
public static bool IsValid (this DateTime? value)
public static bool IsValid(this DateTime? value)
{
return (value != null && IsValid((DateTime)value));
}
@@ -139,7 +142,7 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------------
// I n v a l i d
//-------------------------------------------------------------------------
public static DateTime OnInvalid (this DateTime value, DateTime defaultValue)
public static DateTime OnInvalid(this DateTime value, DateTime defaultValue)
{
if (!IsValid(value)) return defaultValue;
return value;
@@ -148,11 +151,11 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------------
// C o m p a r i s o n
//-------------------------------------------------------------------------
public static DateTime Min (DateTime time1, DateTime time2)
public static DateTime Min(DateTime time1, DateTime time2)
{
return new DateTime(Math.Min(time1.Ticks, time2.Ticks), time1.Kind);
}
public static DateTime Max (DateTime time1, DateTime time2)
public static DateTime Max(DateTime time1, DateTime time2)
{
return new DateTime(Math.Max(time1.Ticks, time2.Ticks), time1.Kind);
}
@@ -182,7 +185,7 @@ namespace Sarand.Net.Standard.Base.Utils
YearCurrent,
YearPrevious,
}
public static String GetPeriodCaption (Period period)
public static String GetPeriodCaption(Period period)
{
switch (period)
{
@@ -227,7 +230,7 @@ namespace Sarand.Net.Standard.Base.Utils
}
return period.ToString();
}
public static bool IsPeriodFromFixed (Period period)
public static bool IsPeriodFromFixed(Period period)
{
switch (period)
{
@@ -242,7 +245,7 @@ namespace Sarand.Net.Standard.Base.Utils
}
return true;
}
public static bool IsPeriodToFixed (Period period)
public static bool IsPeriodToFixed(Period period)
{
switch (period)
{
@@ -254,35 +257,35 @@ namespace Sarand.Net.Standard.Base.Utils
public class ComboBoxItemPeriod
{
public DateUtil.Period Period { get; }
public ComboBoxItemPeriod (DateUtil.Period period)
public ComboBoxItemPeriod(DateUtil.Period period)
{
Period = period;
}
public override String ToString ()
public override String ToString()
{
return GetPeriodCaption(Period);
}
// Comparison.
public override int GetHashCode ()
public override int GetHashCode()
{
return 23 + Period.GetHashCode();
}
public override bool Equals (object obj)
public override bool Equals(object obj)
{
if (obj is ComboBoxItemPeriod) return Equals((ComboBoxItemPeriod)obj);
if (obj is Period) return Equals((Period)obj);
return base.Equals(obj);
}
public bool Equals (ComboBoxItemPeriod item)
public bool Equals(ComboBoxItemPeriod item)
{
return (Period == item.Period);
}
public bool Equals (Period period)
public bool Equals(Period period)
{
return (Period == period);
}
}
public static DateTime StartOfPeriod (Period period, DateTime from, DateTime to)
public static DateTime StartOfPeriod(Period period, DateTime from, DateTime to)
{
switch (period)
{
@@ -327,9 +330,9 @@ namespace Sarand.Net.Standard.Base.Utils
}
return DateTime.Today;
}
public static DateTime EndOfPeriod (Period period, DateTime from, DateTime to)
public static DateTime EndOfPeriod(Period period, DateTime from, DateTime to)
{
DateTime startOfPeriod = StartOfPeriod(period,from,to);
DateTime startOfPeriod = StartOfPeriod(period, from, to);
switch (period)
{
case Period.Period:
@@ -366,19 +369,19 @@ namespace Sarand.Net.Standard.Base.Utils
// R e l a t i v e D a t e s
//-------------------------------------------------------------------
// Relative dates - day.
public static DateTime PreviousDay (this DateTime value)
public static DateTime PreviousDay(this DateTime value)
{
return value.AddDays(-1);
}
public static DateTime NextDay (this DateTime value)
public static DateTime NextDay(this DateTime value)
{
return value.AddDays(1);
}
public static DateTime IncrementDate (this DateTime value, int days)
public static DateTime IncrementDate(this DateTime value, int days)
{
return value.AddDays(days);
}
public static DateTime IncrementMonth (this DateTime value, int months)
public static DateTime IncrementMonth(this DateTime value, int months)
{
return value.AddMonths(months);
}
@@ -390,12 +393,12 @@ namespace Sarand.Net.Standard.Base.Utils
/// </summary>
/// <param name="value">Date in week for which starting Sunday is required</param>
/// <returns></returns>
public static DateTime StartOfWeek (this DateTime value)
public static DateTime StartOfWeek(this DateTime value)
{
// value.DayOfWeek is number of days to go back to Sunday (0 for Sunday, 1 for Monday etc).
return value.AddDays(-(int)value.DayOfWeek);
}
public static DateTime StartOfWorkingWeek (this DateTime value)
public static DateTime StartOfWorkingWeek(this DateTime value)
{
// Go back number to nearest Monday.
return value.AddDays(-((((int)value.DayOfWeek) + 6) % 7));
@@ -403,30 +406,30 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// Relative dates - month.
// Ref. http://stackoverflow.com/questions/24245523/getting-the-first-and-last-day-of-a-month-using-a-given-datetime-object for performance analysis.
public static DateTime StartOfMonth (this DateTime value)
public static DateTime StartOfMonth(this DateTime value)
{
return new DateTime(value.Year, value.Month, 1, 0, 0, 0, value.Kind);
}
public static int DaysInMonth (this DateTime value)
public static int DaysInMonth(this DateTime value)
{
return DateTime.DaysInMonth(value.Year, value.Month);
}
public static DateTime EndOfMonth (this DateTime value)
public static DateTime EndOfMonth(this DateTime value)
{
return new DateTime(value.Year, value.Month, value.DaysInMonth(), 0, 0, 0, value.Kind);
}
//-------------------------------------------------------------------
// Relative dates - quarter.
public static DateTime StartOfQuarter (this DateTime value)
public static DateTime StartOfQuarter(this DateTime value)
{
return new DateTime(value.Year, (((value.Month - 1) / 3) * 3) + 1, 1, 0, 0, 0, value.Kind);
}
public static int DaysInQuarter (this DateTime value)
public static int DaysInQuarter(this DateTime value)
{
DateTime startOfQuarter = value.StartOfQuarter();
return DateTime.DaysInMonth(startOfQuarter.Year, startOfQuarter.Month) + DateTime.DaysInMonth(startOfQuarter.Year, startOfQuarter.Month + 1) + DateTime.DaysInMonth(startOfQuarter.Year, startOfQuarter.Month + 2);
}
public static DateTime EndOfQuarter (this DateTime value)
public static DateTime EndOfQuarter(this DateTime value)
{
DateTime startOfQuarter = value.StartOfQuarter();
return startOfQuarter.EndOfMonth().IncrementDate(1).EndOfMonth().IncrementDate(1).EndOfMonth();
@@ -434,16 +437,16 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// Relative dates - year.
public static DateTime StartOfYear (this DateTime value)
public static DateTime StartOfYear(this DateTime value)
{
return new DateTime(value.Year, 1, 1, 0, 0, 0, value.Kind);
}
public static int DaysInYear (this DateTime value)
public static int DaysInYear(this DateTime value)
{
// To take leap year into account.
return 365 - 28 + DateTime.DaysInMonth(value.Year, 2);
}
public static DateTime EndOfYear (this DateTime value)
public static DateTime EndOfYear(this DateTime value)
{
return new DateTime(value.Year, 12, 31, 0, 0, 0, value.Kind);
}
@@ -452,7 +455,7 @@ namespace Sarand.Net.Standard.Base.Utils
// Age calculation.
//-------------------------------------------------------------------
// Age calculation (returns 0 as a result of any exceptions):
public static ushort CalculateAge (DateTime dob, DateTime ageOn)
public static ushort CalculateAge(DateTime dob, DateTime ageOn)
{
int ageInYears = 0;
if (IsValid(dob) && IsValid(ageOn) && dob < ageOn)
@@ -466,7 +469,7 @@ namespace Sarand.Net.Standard.Base.Utils
return (ushort)ageInYears;
}
public static ushort CalculateAgeMonths (DateTime dob, DateTime ageOn)
public static ushort CalculateAgeMonths(DateTime dob, DateTime ageOn)
{
int ageInMonths = 0;
if (IsValid(dob) && IsValid(ageOn) && dob < ageOn)
@@ -488,7 +491,7 @@ namespace Sarand.Net.Standard.Base.Utils
/// Convert Date (yyyyMMdd) to Int32 (C++ long).
/// <para>Used by legacy database columns to store date as 4 byte integer.</para>
/// </summary>
public static Int32 DateToInt32 (this DateTime value)
public static Int32 DateToInt32(this DateTime value)
{
if (value.Equals(DateTime.MinValue)) return 0;
Int32 dateValue = (value.Year << 16) + (value.Month << 8) + value.Day;
@@ -498,7 +501,7 @@ namespace Sarand.Net.Standard.Base.Utils
/// Convert Int32 (C++ long) to Date (yyyyMMdd).
/// <para>Used by legacy database columns to store date as 4 byte integer.</para>
/// </summary>
public static DateTime Int32ToDate (Int32 dateValue)
public static DateTime Int32ToDate(Int32 dateValue)
{
if (dateValue == 0) return DateTime.MinValue;
int year = ((dateValue >> 16) & 0xffff);
@@ -512,7 +515,7 @@ namespace Sarand.Net.Standard.Base.Utils
/// Convert Time (HHmmssnnn) to Int32 (C++ long).
/// <para>Used by legacy database columns to store time as 4 byte integer.</para>
/// </summary>
public static Int32 TimeToInt32 (this DateTime value)
public static Int32 TimeToInt32(this DateTime value)
{
if (value.Equals(DateTime.MinValue)) return 0;
Int32 timeValue = (value.Hour << 24) + (value.Minute << 16) + (value.Second << 8) + (value.Millisecond / 10);
@@ -522,7 +525,7 @@ namespace Sarand.Net.Standard.Base.Utils
/// Convert Int32 (C++ long) to Time (HHmmssnnn).
/// <para>Used by legacy database columns to store time as 4 byte integer.</para>
/// </summary>
public static DateTime Int32ToTime (Int32 timeValue)
public static DateTime Int32ToTime(Int32 timeValue)
{
if (timeValue == 0) return DateTime.MinValue;
int year = DateTime.Today.Year;
@@ -559,14 +562,16 @@ namespace Sarand.Net.Standard.Base.Utils
/// <param name="value"></param>
/// <param name="format"></param>
/// <returns></returns>
public static String Format (this DateTime value, String format)
public static String Format(this DateTime value, String format, CultureInfo cultureInfo = null)
{
if (!IsValid(value)) return String.Empty;
try
{
if (!String.IsNullOrEmpty(format))
return value.ToString(format);
return value.ToString();
{
return value.ToString(format, cultureInfo ?? CultureInfo.InvariantCulture);
}
return value.ToString(cultureInfo ?? CultureInfo.InvariantCulture);
}
catch (Exception exc)
{
@@ -609,9 +614,16 @@ namespace Sarand.Net.Standard.Base.Utils
return Format(value, Format_XmlDateTime);
}
//-------------------------------------------------------------------
/// <summary>Format suitable for parsing as javascript date: 'yyyy-MM-dd'</summary>
public static String FormatDateForJavascript(this DateTime value)
{
return Format(value, Format_JavascriptDate);
}
//-------------------------------------------------------------------
/// <summary>Format number of seconds.</summary>
public static String FormatNumberOfSeconds (int numberOfSeconds)
public static String FormatNumberOfSeconds(int numberOfSeconds)
{
int numberOfMinutes = numberOfSeconds / 60;
int numberOfHours = numberOfMinutes / 60;
@@ -656,12 +668,12 @@ namespace Sarand.Net.Standard.Base.Utils
//-------------------------------------------------------------------
// L i s t S o r t i n g
//-------------------------------------------------------------------
public static void SortAscending (this List<DateTime> list)
public static void SortAscending(this List<DateTime> list)
{
list.Sort((a, b) => a.CompareTo(b));
}
public static void SortDescending (this List<DateTime> list)
public static void SortDescending(this List<DateTime> list)
{
list.Sort((a, b) => b.CompareTo(a));
}
@@ -23,6 +23,7 @@ namespace Sarand.Net.Standard.Base.Utils
/// <summary>Safely get value from Dictionary with default if not defined.</summary>
public static TValue GetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
if (key == null) return defaultValue;
TValue value;
return dictionary.TryGetValue(key, out value) ? value : defaultValue;
}