views:

163

answers:

2

Is TIME used in F# Units of Measure?

+1  A: 

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

James Black
+7  A: 

Hi, 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>
Tomas Petricek