Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Units of Measure ... Is there an example of TIME?

Is TIME used in F# Units of Measure?

like image 891
Art Scott Avatar asked Aug 27 '09 22:08

Art Scott


2 Answers

there is a standard SI unit 'second' that you can use for representing time. You'll need to reference FSharp.PowerPack.dll and then you can use the following:

open Microsoft.FSharp.Math

let minute = 60<SI.s>
let twoMinutes = minute * 2

There are no built-in units for minutes/hours etc., because these are not standard units (and are actually only seconds multiplied by some number). However, you can define minutes too if you want:

[<Measure>]
type mins =
  static member of_seconds(sec) = sec / 60<SI.s> * 1<mins>

// converting seconds to minutes 
let timeInMinuts = mins.of_seconds(twoMinutes)

You can't use units with the standard System.DateTime type if that's you'r question, but you could create a wrapper that exposes the actual time (seconds/minutes) in the units you want:

open System

type DateTimeUnits(dt:DateTime) = 
  static member Now = new DateTimeUnits(DateTime.Now)
  member x.Second = dt.Second * 1<SI.s>
  member x.Minute = dt.Minute * 1<mins>
like image 95
Tomas Petricek Avatar answered Sep 30 '22 00:09

Tomas Petricek


It appears that time can be in it:

http://blogs.msdn.com/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing-units.aspx

but, even it it isn't, you can write your own units.

This article from MS also may be useful, depending on which units you want. http://msdn.microsoft.com/en-us/library/dd233243%28VS.100%29.aspx

like image 20
James Black Avatar answered Sep 30 '22 00:09

James Black