//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Globalization;

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HDateTime {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public const long MaxTicks = uint.MaxValue;

//--------------------------------------------------------------------------------------------------

public static long CurrentTicks =>
#if WIN32
   Win32_Core.GetTickCount();
#else // !WIN32
   DateTime.Now.Ticks;
#endif // !WIN32

public static int SystemTicksInaccuracy => 15;

//==================================================================================================

public static long TicksToMilliseconds(long ticks)
{
   return ticks / 10000;
}

public static long MillisecondsToTicks(long milliseconds)
{
   return milliseconds * 10000;
}

//==================================================================================================

public static long ElapsedMilliseconds(long sinceTicks)
{
   return ElapsedMilliseconds( sinceTicks, CurrentTicks );
}

public static long ElapsedMilliseconds(long fromTicks,long toTicks)
{
   return TicksToMilliseconds( fromTicks <= toTicks ?
      toTicks - fromTicks : MaxTicks - fromTicks + toTicks );
}

//--------------------------------------------------------------------------------------------------

public static long ElapsedMilliseconds(DateTime since)
{
   return ElapsedMilliseconds( since, DateTime.Now );
}

public static long ElapsedMilliseconds(DateTime from,DateTime to)
{
   return TicksToMilliseconds( (to-from).Ticks );
}

//==================================================================================================

public static bool IsPeriodEspired(long sinceTicks,long timeoutInMilliseconds)
{
   return IsPeriodEspired( sinceTicks, timeoutInMilliseconds, SystemTicksInaccuracy );
}

public static bool IsPeriodEspired(DateTime since,long timeoutInMilliseconds)
{
   return IsPeriodEspired( since, timeoutInMilliseconds, SystemTicksInaccuracy );
}

//--------------------------------------------------------------------------------------------------

public static bool IsPeriodEspired(long sinceTicks,long timeoutInMilliseconds,int ticksInaccuracy)
{
   return ElapsedMilliseconds( sinceTicks ) > timeoutInMilliseconds + ticksInaccuracy;
}

public static bool IsPeriodEspired(DateTime since,long timeoutInMilliseconds,int ticksInaccuracy)
{
   return ElapsedMilliseconds( since ) > timeoutInMilliseconds + ticksInaccuracy;
}

//==================================================================================================

static HDateTime()
{
   CommonHelpers_Core.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HDateTime
//**************************************************************************************************

} // CommonHelpers (.WASM)
