68 lines
1.8 KiB
C#
68 lines
1.8 KiB
C#
using System;
|
|
|
|
namespace Gallery.Util
|
|
{
|
|
public static class Extensions
|
|
{
|
|
public static int IndexOf<T>(this T[] array, Predicate<T> predicate)
|
|
{
|
|
for (var i = 0; i < array.Length; i++)
|
|
{
|
|
if (predicate(array[i]))
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public static int LastIndexOf<T>(this T[] array, Predicate<T> predicate)
|
|
{
|
|
for (var i = array.Length - 1; i >= 0; i--)
|
|
{
|
|
if (predicate(array[i]))
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public static bool All<T>(this T[] array, Predicate<T> predicate)
|
|
{
|
|
for (var i = 0; i < array.Length; i++)
|
|
{
|
|
if (!predicate(array[i]))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static bool AnyFor<T>(this T[] array, int from, int to, Predicate<T> predicate)
|
|
{
|
|
for (var i = from; i <= to; i++)
|
|
{
|
|
if (predicate(array[i]))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static DateTime ToLocalTime(this long time)
|
|
{
|
|
//return new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(time).ToLocalTime();
|
|
return new DateTime(621355968000000000L + time * 10000000).ToLocalTime();
|
|
}
|
|
|
|
public static long ToTimestamp(this DateTime datetime)
|
|
{
|
|
var ticks = datetime.Ticks;
|
|
return (ticks - 621355968000000000L) / 10000000;
|
|
}
|
|
}
|
|
}
|