Thursday 2 June 2016

Haskell: Working with file access and modification times

Haskell System.Directory module provides following funcitons to get and set file (or) directory access and modification times. Following table summarizes the functions.

Function
Signature
Description
getAccessTime
FilePath -> IO UTCTime
Get the time at which the file or directory was last accessed.
getModificationTime
FilePath -> IO UTCTime
Get the time at which the file or directory was last modified.
setAccessTime
FilePath -> UTCTime -> IO () Source
Change the time at which the file or directory was last accessed.
setModificationTime
FilePath -> UTCTime -> IO ()
Change the time at which the file or directory was last modified.

Above functions may fail with following errors.

isPermissionError if the user is not permitted to alter the access time.
isDoesNotExistError if the file or directory does not exist.


FileTimeUtil.hs
import System.Directory
import System.IO

main = do
    putStrLn "Enter file (or) directory"
    file <- getLine

    accessTime <- getAccessTime file
    modificationTime <- getModificationTime file

    putStrLn $ "access time : " ++ (show accessTime)
    putStrLn $ "modification time : " ++ (show modificationTime)

$ runghc FileTimeUtil.hs 
Enter file (or) directory
/Users/harikrishna_gurram/
access time : 2016-04-07 09:36:16 UTC
modification time : 2016-04-07 05:45:44 UTC



Previous                                                 Next                                                 Home

No comments:

Post a Comment