Difference between revisions of "Cookbook/Dates And Time"
< Cookbook
Jump to navigation
Jump to search
m (Fixed broken link) |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | = Finding today's date = |
+ | == Finding today's date == |
<haskell> |
<haskell> |
||
Line 8: | Line 8: | ||
</haskell> |
</haskell> |
||
− | = Adding to or subtracting from a date = |
+ | == Adding to or subtracting from a date == |
{| class="wikitable" |
{| class="wikitable" |
||
Line 19: | Line 19: | ||
| [http://hackage.haskell.org/packages/archive/time/latest/doc/html/Data-Time-Calendar.html#v%3AaddDays addDays] |
| [http://hackage.haskell.org/packages/archive/time/latest/doc/html/Data-Time-Calendar.html#v%3AaddDays addDays] |
||
|<haskell> |
|<haskell> |
||
− | import |
+ | import Data.Time |
a = fromGregorian 2009 12 31 --> 2009-12-31 |
a = fromGregorian 2009 12 31 --> 2009-12-31 |
||
b = addDays 1 a --> 2010-01-01 |
b = addDays 1 a --> 2010-01-01 |
||
Line 27: | Line 27: | ||
| [http://hackage.haskell.org/packages/archive/time/latest/doc/html/Data-Time-Calendar.html#v%3AaddDays addDays] |
| [http://hackage.haskell.org/packages/archive/time/latest/doc/html/Data-Time-Calendar.html#v%3AaddDays addDays] |
||
|<haskell> |
|<haskell> |
||
− | import |
+ | import Data.Time |
a = fromGregorian 2009 12 31 --> 2009-12-31 |
a = fromGregorian 2009 12 31 --> 2009-12-31 |
||
b = addDays (-7) a --> 2009-12-24 |
b = addDays (-7) a --> 2009-12-24 |
||
Line 33: | Line 33: | ||
|} |
|} |
||
− | = Difference of two dates = |
+ | == Difference of two dates == |
{| class="wikitable" |
{| class="wikitable" |
||
Line 44: | Line 44: | ||
| [http://hackage.haskell.org/packages/archive/time/latest/doc/html/Data-Time-Calendar.html#v%3AdiffDays diffDays] |
| [http://hackage.haskell.org/packages/archive/time/latest/doc/html/Data-Time-Calendar.html#v%3AdiffDays diffDays] |
||
|<haskell> |
|<haskell> |
||
− | import |
+ | import Data.Time |
a = fromGregorian 2009 12 31 --> 2009-12-31 |
a = fromGregorian 2009 12 31 --> 2009-12-31 |
||
− | b = fromGregorian 2010 12 |
+ | b = fromGregorian 2010 12 31 --> 2010-12-31 |
diffDays b a --> 365 |
diffDays b a --> 365 |
||
</haskell> |
</haskell> |
||
|} |
|} |
||
− | = CPU time = |
+ | == CPU time == |
− | Use [http://www.haskell.org/ghc/docs/latest/html/libraries/base/System-CPUTime.html#v%3AgetCPUTime System.CPUTime.getCPUTime] to get the CPU time in picoseconds. |
+ | Use [http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/System-CPUTime.html#v%3AgetCPUTime System.CPUTime.getCPUTime] to get the CPU time in picoseconds. |
You can time a computation like this |
You can time a computation like this |
Latest revision as of 21:06, 6 January 2019
Finding today's date
import Data.Time
c <- getCurrentTime --> 2009-04-21 14:25:29.5585588 UTC
(y,m,d) = toGregorian $ utctDay c --> (2009,4,21)
Adding to or subtracting from a date
Problem | Solution | Examples |
---|---|---|
adding days to a date | addDays | import Data.Time
a = fromGregorian 2009 12 31 --> 2009-12-31
b = addDays 1 a --> 2010-01-01
|
subtracting days from a date | addDays | import Data.Time
a = fromGregorian 2009 12 31 --> 2009-12-31
b = addDays (-7) a --> 2009-12-24
|
Difference of two dates
Problem | Solution | Examples |
---|---|---|
calculating the difference of two dates | diffDays | import Data.Time
a = fromGregorian 2009 12 31 --> 2009-12-31
b = fromGregorian 2010 12 31 --> 2010-12-31
diffDays b a --> 365
|
CPU time
Use System.CPUTime.getCPUTime to get the CPU time in picoseconds.
You can time a computation like this
getCPUTimeDouble :: IO Double
getCPUTimeDouble = do t <- System.CPUTime.getCPUTime; return ((fromInteger t) * 1e-12)
main = do
t1 <- getCPUTimeDouble
print (fib 30)
t2 <- getCPUTimeDouble
print (t2-t1)