Changeset 17042 for src/Pugs/Prim.hs

Show
Ignore:
Timestamp:
07/14/07 03:52:25 (17 months ago)
Author:
audreyt
Message:

* Switch from System.Time to the newer Data.Time modules

for localtime(), time(), the :M filetest, etc.
(No user-visible changes.)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim.hs

    r16625 r17042  
    5656import GHC.Unicode 
    5757import qualified Data.HashTable as H 
     58import Data.Time.LocalTime 
     59import Data.Time.Calendar.OrdinalDate 
     60import Data.Time.Calendar.MonthDay 
    5861 
    5962constMacro :: Exp -> [Val] -> Eval Val 
     
    7275op0 "False" = constMacro . Val $ VBool False 
    7376op0 "time"  = const $ do 
    74     clkt <- guardIO getClockTime 
     77    clkt <- guardIO getCurrentTime 
    7578    return $ VRat $ pugsTimeSpec clkt 
    7679op0 "times"  = const $ do 
     
    420423    x <- fromVal v :: Eval VNum 
    421424    guardIO $ do 
    422         TOD t0s t0ps <- getClockTime 
     425        start   <- getCurrentTime 
    423426        threadDelay (round $ x * clocksPerSecond) 
    424         TOD t1s t1ps <- getClockTime 
    425         return $ VRat ((fromInteger $ t1ps - t0ps) 
    426                       / (clocksPerSecond * clocksPerSecond) -- 10^12 
    427                       + (fromInteger $ t1s - t0s)) 
     427        finish  <- getCurrentTime 
     428        return $ VRat (toRational $ diffUTCTime start finish) 
    428429op1 "mkdir" = guardedIO createDirectory 
    429430op1 "rmdir" = guardedIO removeDirectory 
     
    699700op1 "stat" = \x -> opPerl5 "require File::stat; File::stat::stat" [x] 
    700701op1 "lstat" = \x -> opPerl5 "require File::stat; File::stat::lstat" [x] 
     702op1 "Pugs::Internals::localtime"  = \x -> do 
     703    tz  <- io getCurrentTimeZone 
     704    tm  <- fromVal x    -- seconds since Perl's epoch 
     705    let utc   = posixSecondsToUTCTime (fromInteger tm + offset) 
     706        local = utcToLocalTime tz utc 
     707        day   = localDay local 
     708        tod   = localTimeOfDay local 
     709        (year, month, dayOfMonth)   = toGregorian day  
     710        (sec, pico)                 = properFraction $ todSec tod 
     711        (_, dayOfWeek)              = sundayStartWeek day 
     712    -- if wantString then return . VStr $ formatTime "%c" (ZonedTime local tz) else 
     713    retSeq [ vI    $ year 
     714           , vI    $ month 
     715           , vI    $ dayOfMonth 
     716           , vI    $ todHour tod 
     717           , vI    $ todMin tod 
     718           , VInt  $ sec 
     719           , vI    $ fromEnum (pico * 1000000000000) 
     720           , vI    $ dayOfWeek + 1 
     721           , vI    $ (monthAndDayToDayOfYear (isLeapYear year) month dayOfMonth) - 1 
     722           , VStr  $ timeZoneName tz 
     723           , vI    $ timeZoneMinutes tz * 60 
     724           , VBool $ timeZoneSummerOnly tz 
     725           ] 
     726    where 
     727    offset :: NominalDiffTime 
     728    offset = 946684800 -- diff between Haskell and Perl epochs (seconds) 
     729    vI :: Integral a => a -> Val 
     730    vI = VInt . toInteger 
    701731 
    702732op1 other   = \_ -> fail ("Unimplemented unaryOp: " ++ other) 
     
    13241354    return $ VObject o{ objAttrs = attrs', objId = uniq } 
    13251355 
    1326 op3 "Pugs::Internals::localtime"  = \x y z -> do 
    1327     wantString <- fromVal x 
    1328     sec <- fromVal y 
    1329     pico <- fromVal z 
    1330     c <- guardIO $ toCalendarTime $ TOD (offset + sec) pico 
    1331     if wantString then return $ VStr $ calendarTimeToString c else 
    1332         retSeq $ [ vI $ ctYear c 
    1333                  , vI $ (1+) $ fromEnum $ ctMonth c 
    1334                  , vI $ ctDay c 
    1335                  , vI $ ctHour c 
    1336                  , vI $ ctMin c 
    1337                  , vI $ ctSec c 
    1338                  , VInt $ ctPicosec c 
    1339                  , vI $ (1+) $ fromEnum $ ctWDay c 
    1340                  , vI $ ctYDay c 
    1341                  , VStr $ ctTZName c 
    1342                  , vI $ ctTZ c 
    1343                  , VBool $ ctIsDST c 
    1344                  ] 
    1345     where 
    1346        offset = 946684800 :: Integer -- diff between Haskell and Perl epochs (seconds) 
    1347        vI = VInt . toInteger 
    13481356op3 "Pugs::Internals::hSeek" = \x y z -> do 
    13491357    handle <- fromVal x 
     
    19711979\\n   Num       pre     time    safe   ()\ 
    19721980\\n   List      pre     times   safe   ()\ 
    1973 \\n   List      pre     Pugs::Internals::localtime   safe   (Bool, Int, Int)\ 
     1981\\n   List      pre     Pugs::Internals::localtime   safe   (Num)\ 
    19741982\\n   Str       pre     want    safe   ()\ 
    19751983\\n   Str       pre     File::Spec::cwd     unsafe ()\