Tuesday, May 24, 2011

[wqwlglhn] Date manipulation in Haskell

Two quick programs developed during the Prime Day calculator:

Difference between 2 dates

module Main where{
  import Data.Time;
  import System.Environment;
  main = do {
    args<-getArgs;
    print$diffDays (m (drop 3 args)) (m (take 3 args));
    };
  m :: [String] -> Day;
  m [yr,mo,dy] = fromGregorian (read yr) (read mo) (read dy);
  m x = error ("expecting [year,month,day], but got " ++ (show x));
  }

Add an offset to a date:

module Main where{
  import Data.Time;
  import System.Environment;
  main = do {
    args<-getArgs;
    print$ toGregorian $ addDays  (read$head args) (m (tail args));
    };
  m :: [String] -> Day;
  m [yr,mo,dy] = fromGregorian (read yr) (read mo) (read dy);
  m x = error ("expecting [year,month,day], but got " ++ (show x));
  }

No comments :