Changeset 17042

Show
Ignore:
Timestamp:
07/14/07 03:52:25 (1 year 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:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/Pugs/Compat.hs

    r16498 r17042  
    5858import System.Cmd 
    5959import System.Posix.Types 
    60 import System.Time 
     60import Data.Time 
    6161 
    6262#ifdef PUGS_HAVE_POSIX 
     
    336336        peekCString =<< peekElemOff argv 0 
    337337 
     338epochY2K :: UTCTime 
     339epochY2K = UTCTime 
     340    { utctDay     = fromGregorian 2000 01 01 
     341    , utctDayTime = 0 
     342    } 
     343 
    338344{-| 
    339345Convert an internal @ClockTime@ to a Pugs-style fractional time. 
    340346Used by op0 "time", @Pugs.Run.prepareEnv@, and the file time tests. 
    341347-} 
    342 pugsTimeSpec :: ClockTime -> Rational 
    343 pugsTimeSpec clkt = fdiff $ diffClockTimes clkt epochClkT 
    344     where 
    345        epochClkT = toClockTime epoch 
    346        epoch = CalendarTime 2000 January 1 0 0 0 0 Saturday 0 "UTC" 0 False 
    347        -- 10^12 is expanded because the alternatives tried gave type warnings. 
    348        fdiff = \d -> (fromInteger $ tdPicosec d) 
    349                    / (clocksPerSecond * clocksPerSecond) 
    350                    + (fromIntegral $ tdSec d) 
     348pugsTimeSpec :: UTCTime -> Rational 
     349pugsTimeSpec clkt = toRational (diffUTCTime clkt epochY2K) 
  • src/Pugs/Internals.hs

    r16600 r17042  
    5151import Data.Sequence          as X (Seq, singleton) 
    5252import Data.Set               as X (Set) 
     53import Data.Time              as X  
     54import Data.Time.Clock.POSIX  as X  
    5355import Data.Tree              as X  
    5456import Data.Unique            as X  
     
    7375import System.Process         as X  
    7476import System.Random          as X hiding (split) 
    75 import System.Time            as X  
    7677 
    7778-- Instances. 
  • 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 ()\ 
  • src/Pugs/Prim/FileTest.hs

    r15616 r17042  
    6161fileTime :: (FilePath -> IO Integer) -> Val -> Eval Val 
    6262fileTime test f = do 
    63     t <- fileTestIO (fileTestDo test) f 
     63    t   <- fileTestIO (fileTestDo test) f 
    6464    if (t == undef) then return VUndef else do 
    65     t' <- fromVal t 
    66     b <- (readVar $ cast "$*BASETIME") >>= fromVal 
    67     return $ VRat $ (b - (pugsTimeSpec $ TOD t' 0)) / 86400 
     65    t' <- fromVal t :: Eval Integer 
     66    b   <- fromVal =<< readVar (cast "$*BASETIME") 
     67    return $ VRat $ (b - (pugsTimeSpec . posixSecondsToUTCTime $ fromIntegral t')) / 86400 
    6868 
    6969fileTestIO :: (Value n) => (n -> IO Val) -> Val -> Eval Val 
  • src/Pugs/Run.hs

    r16627 r17042  
    114114    autoSV  <- newScalar undef 
    115115    classes <- initClassObjects (MkObjectId $ -1) [] initTree 
    116     strictSV <- newScalar $ VBool (name /= "-e") 
    117     baset <- getClockTime 
     116    strictSV<- newScalar $ VBool (name /= "-e") 
     117    baset   <- getCurrentTime 
    118118#if defined(PUGS_HAVE_HSPLUGINS) 
    119119    hspluginsSV <- newScalar (VInt 1) 
  • src/perl6/Prelude.pm

    r16642 r17042  
    564564        is primitive is builtin is safe { 
    565565    my $res; 
    566     my $sec = int $when; 
    567     my $pico = ($when - int $when) * 10**12; 
     566    #my $sec = int $when; 
     567    #my $pico = ($when - int $when) * 10**12; 
    568568    # XXX: waiting on a better want 
    569569    #if want ~~ rx:P5/^Item/ { 
    570570    #    $res = Pugs::Internals::localtime(Bool::True, $sec, $pico); 
    571571    #} else { 
    572         my @tm = Pugs::Internals::localtime(Bool::False, $sec, $pico); 
     572        my @tm = Pugs::Internals::localtime($when); # Bool::False, $sec, $pico); 
    573573 
    574574        # FIXME: this is how it oughta look, with @ids being class level.