[Haskell-beginners] Real World Haskell: From Portable to System.Win32

Paulo Pocinho pocinho at gmail.com
Sat Oct 8 07:47:15 CEST 2011


Hello Haskellers.

I'm having a bit of trouble solving the challenge in chapter 9 of the
book Real World Haskell [1]. I understand the portable code and have
no difficulty with it. More specifically, I want to know how to get a
file's last modified date. Perhaps in understanding this bit I may
also extrapolate to similar parts of System.Win32 more easily.

A simple example using portable code, and also formats the date:

getFileModifiedDate :: FilePath -> IO String
getFileModifiedDate f = do
    m <- getModificationTime f
    c <- toCalendarTime m
    return $ formatCalendarTime undefined "%Y-%m-%d %T" c

However, the exercise is to "port the code from this chapter to your
platform's native API", which in my case is System.Win32. Using the
simple example, I know that I can use getFileTime from System.Win32
[2]:

getFileTime :: HANDLE -> IO (FILETIME, FILETIME, FILETIME)

I suppose that means getFileTime takes a file handle (with type
HANDLE) and returns an IO type with CreationTime LastAccessTime
LastWriteTime [3].

The thing is I don't know how to get a HANDLE type. I understand I can
get a file handle by opening it. I've been trying the following:

openFile :: FilePath -> IOMode -> IO Handle
fh <- openFile "myfile.hs" ReadMode

Right now I cannot use getFileTime on it because this has type Handle:

Couldn't match expected type `HANDLE' with actual type `Handle'
In the first argument of `getFileTime', namely `fh'
In a stmt of an interactive GHCi command: nf <- getFileTime fh

I think I got lost at inspecting the type HANDLE = Ptr ()

If anyone could make a useful example I would appreciate it very much.
Perhaps it's just me going around in circles.


Regards,
P. M. Pocinho

--
[1] http://book.realworldhaskell.org/read/io-case-study-a-library-for-searching-the-filesystem.html
[2] http://hackage.haskell.org/packages/archive/Win32/2.2.1.0/doc/html/System-Win32-Time.html#t:FILETIME
[3] http://msdn.microsoft.com/en-us/library/windows/desktop/ms724320%28v=VS.85%29.aspx



More information about the Beginners mailing list