Skip to content

Instantly share code, notes, and snippets.

View bjartwolf's full-sized avatar

Bjørn Einar Bjartnes bjartwolf

View GitHub Profile
/// <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);
}
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
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 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
type hourReading = int * meterReading
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 reading<'A> = { TimeStamp: 'A;
Value: decimal}
type timestamp= NorwegianTimeStamp of ZonedDateTime | UtcTimeStamp of Instant
type reading = { TimeStamp: timestamp;
Value: decimal}
@bjartwolf
bjartwolf / noworks.fs
Last active August 29, 2015 14:20
would be nice if it worked
type foo<'A> = { Name: string; things : 'A list}
let bar = { Name = "Bjorn"; things = [1;2;3;4]}
let baz = {Name = bar.Name; things = bar.things |> List.map (fun x -> x.ToString())}
let baznoworky = { bar with things = (bar.things |> List.map (fun x -> x.ToString()))}
open System
// We can not work with radius of the diamond outside 0 to 25
let CreateRadius (i:int) =
match i with
| i when i = 0 && i < 26 -> Some i
| _ -> None
let radius = CreateRadius 20 |> Option.get
let axis = [|-radius .. radius|]