Gallery/Gallery.Share/Util/Extensions.cs

78 lines
2.0 KiB
C#

using System;
using Gallery.Util.Model;
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;
}
public static bool SourceEquals(this GalleryItem a, GalleryItem b)
{
if (a == null || b == null)
{
return false;
}
return a.Id == b.Id && a.Source == b.Source;
}
}
}