-
Notifications
You must be signed in to change notification settings - Fork 171
/
Extensions.cs
40 lines (35 loc) · 1.56 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using CacheCow.Samples.Common;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CacheCow.Samples.Common
{
public static class Extensions
{
public static string ToETagString(this DateTimeOffset dateTimeOffset)
{
return TurnDatetimeOffsetToETag(dateTimeOffset);
}
public static string ToETagString(this DateTimeOffset dateTimeOffset, int count)
{
return TurnDatetimeOffsetAndCountToETag(dateTimeOffset, count);
}
private static string TurnDatetimeOffsetToETag(DateTimeOffset dateTimeOffset)
{
var dateBytes = BitConverter.GetBytes(dateTimeOffset.UtcDateTime.Ticks);
var offsetBytes = BitConverter.GetBytes((Int16)dateTimeOffset.Offset.TotalHours);
return Convert.ToBase64String(dateBytes.Concat(offsetBytes).ToArray());
}
private static string TurnDatetimeOffsetAndCountToETag(DateTimeOffset dateTimeOffset, int count)
{
var dateBytes = BitConverter.GetBytes(dateTimeOffset.UtcDateTime.Ticks);
var offsetBytes = BitConverter.GetBytes((Int16)dateTimeOffset.Offset.TotalHours);
var allBytes = dateBytes.Concat(offsetBytes).Concat(BitConverter.GetBytes(count)).ToArray();
return Convert.ToBase64String(allBytes);
}
public static DateTimeOffset GetMaxLastModified(this IEnumerable<Car> cars)
{
return cars.Aggregate(DateTimeOffset.MinValue, (seed, car) => seed > car.LastModified ? seed : car.LastModified);
}
}
}