Skip to content

Instantly share code, notes, and snippets.

View bjartwolf's full-sized avatar

Bjørn Einar Bjartnes bjartwolf

View GitHub Profile
type timestamp= NorwegianTimeStamp of ZonedDateTime | UtcTimeStamp of Instant
type reading = { TimeStamp: timestamp;
Value: decimal}
type reading<'A> = { TimeStamp: 'A;
Value: decimal}
type norTimestamp = ZonedDateTime
type utcTimestamp = Instant
type meterReading= { Val: decimal } // Contains more stuff, shortend for clarity
type utcReading = utcTimestamp * meterReading
type norReading = norTimestamp * meterReading
type hourReading = int * meterReading
let getLastSundays year =
let lastSundays = Dictionary<int, int>()
// find all lastsundays
[for month in 1..12->
[for day in System.DateTime.DaysInMonth(year,month).. -1.. 1->
System.DateTime(year,month,day).DayOfWeek,(month,day)]
|> Seq.find (fun (dayOfWeek,_) -> dayOfWeek = System.DayOfWeek.Sunday) ]
|> Seq.map (fun ((_,x)) -> x) |> Seq.iter lastSundays.Add
lastSundays
let findHoursInNorwegianDay (year:int) (month:int) (day: int) :int =
let zone = DateTimeZoneProviders.Tzdb.GetZoneOrNull("Europe/Oslo")
let t1 = new LocalDateTime(year, month,day,0,0)
let t2 = t1.PlusDays(1)
let z1 = t1.InZoneLeniently(zone)
let z2 = t2.InZoneLeniently(zone)
let interval = z2.ToInstant()- z1.ToInstant()
int (System.Math.Round(TimeSpan.FromTicks(interval.Ticks).TotalHours,0))
let datetimetest (date1 :System.DateTime) (date2: System.DateTime) =
let inverseDate1 = DateFormatInverter.InverseDateTime(date1)
let inverseDate2 = DateFormatInverter.InverseDateTime(date2)
if (date1 > date2) then
inverseDate1 < inverseDate2
else if (date1 < date2) then
inverseDate1 > inverseDate2
else
inverseDate1 = inverseDate2
Check.QuickThrowOnFailure datetimetest
/// <summary>
/// Encodes for storing in Azure Table store to get correct ordering
/// </summary>
/// <param name="date">Use UTC</param>
public static string InverseDateTime(this DateTime date)
{
var newDate = DateTime.MaxValue.Ticks - date.Ticks;
return newDate.ToString(CultureInfo.InvariantCulture);
}
@bjartwolf
bjartwolf / initial.fs
Last active August 29, 2015 14:20
initial
type symbol = Number of int | Plus | Minus
let rec calc (s: symbol list) : int =
match s with
| [] -> 0
| [Number x] -> x
| Number x :: Number y :: rest -> calc(Number ((int)(sprintf "%d%d" x y)) :: rest)
| Number x :: rest -> x + calc(rest)
| Plus :: rest -> + calc(rest)
| Minus :: rest -> - calc(rest)
type symbol = Number of int | Plus of int * symbol | Minus of int * symbol | Noop of int * symbol
let merge (i1: int) (i2:int) = (int)(sprintf "%d%d" i1 i2)
let rec flattenNoop ((i,s): (int*symbol)) =
match s with
| Number n -> Number (merge i n)
| Plus (n,t) -> Plus (merge i n,t)
| Minus (n,t) -> Minus (merge i n,t)
| Noop (n,t) -> flattenNoop (merge i n,t)