Commit 815f5110 authored by Bas Lijnse's avatar Bas Lijnse

Added a Linux version of an interface to C's time.h, a Windows version is still in the making...

git-svn-id: https://svn.cs.ru.nl/repos/clean-platform/trunk@7 2afc29ad-3112-4e41-907a-9359c7e6e986
parent 85db20a1
#include <Clean.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define BUFSIZE 256
int clockC() {
return (int) clock();
}
int timeC() {
return (int) time(NULL);
}
void copyFromTmStruct(struct tm *t, CleanIntArray a) {
a[0] = t->tm_sec;
a[1] = t->tm_min;
a[2] = t->tm_hour;
a[3] = t->tm_mday;
a[4] = t->tm_mon;
a[5] = t->tm_year;
a[6] = t->tm_wday;
a[7] = t->tm_yday;
a[8] = t->tm_isdst;
}
void copyToTmStruct(struct tm *t, CleanIntArray a) {
t->tm_sec = a[0];
t->tm_min = a[1];
t->tm_hour = a[2];
t->tm_mday = a[3];
t->tm_mon = a[4];
t->tm_year = a[5];
t->tm_wday = a[6];
t->tm_yday = a[7];
t->tm_isdst = a[8];
}
void gmTimeC(CleanIntArray a) {
time_t t;
struct tm tm;
time(&t);
gmtime_r(&t, &tm);
copyFromTmStruct(&tm, a);
}
void localTimeC(CleanIntArray a) {
time_t t;
struct tm tm;
time(&t);
localtime_r(&t, &tm);
copyFromTmStruct(&tm, a);
}
int mkTimeC(CleanIntArray a) {
time_t t;
struct tm tm;
copyToTmStruct(&tm, a);
return (int) mktime(&tm);
}
int toStringTmC(CleanIntArray a, CleanString s) {
struct tm tm;
copyToTmStruct(&tm, a);
strncpy(CleanStringCharacters(s), asctime(&tm), 24);
return 0;
}
int toStringTimeC(int t, CleanString s) {
strncpy(CleanStringCharacters(s), ctime((time_t *) &t), 24);
return 0;
}
void strfTimeC(CleanString format, CleanIntArray a, int *len_out, int *ptr_out) {
struct tm tm;
char *s = calloc(BUFSIZE, sizeof(char));
copyToTmStruct(&tm, a);
*len_out = (int) strftime(s, BUFSIZE, CleanStringCharacters(format), &tm);
*ptr_out = (int) s;
}
int copyStringC(int len, int ptr, CleanString dest) {
char *src = (char*) ptr;
strncpy(CleanStringCharacters(dest), src, len);
free(src);
return 0;
}
definition module _TimeOS
CLK_PER_SEC :== 100
implementation module _TimeOS
definition module Time
/**
* This module provides an interface to the time definition of the
* C standard library.
*/
from StdString import class toString
from _TimeOS import CLK_PER_SEC
/**
* The Tm record structure contains date and time information
* in a broken down format.
*/
:: Tm = { sec :: Int // Seconds (0-60)
, min :: Int // Minutes (0-59)
, hour :: Int // Hour (0-23)
, mday :: Int // Day of the month (1-31)
, mon :: Int // Month (0-11)
, year :: Int // Years since 1900
, wday :: Int // Day of the week (0-6, 0 is Sunday)
, yday :: Int // Day of the year (0-365)
, isdst :: Bool // Daylight saving time flag
}
/**
* The time data type represents a number of seconds since the epoch (1-1-1970).
*/
:: Time = Time Int
/**
* The clock data type represents a number of CPU clock ticks.
*/
:: Clock = Clock Int
instance toString Tm
instance toString Time
instance toString Clock
/**
* Get the number of clock ticks since the process start
*/
clock :: !*World -> (!Clock, !*World)
/**
* Get the number of seconds since the epoch
*/
time :: !*World -> (!Time, !*World)
/**
* Get the current time as GMT
*/
gmTime :: !*World -> (!Tm, !*World)
/**
* Get the current time in the local timezone
*/
localTime :: !*World -> (!Tm, !*World)
/**
* Convert a Tm record to a Time value
*/
mkTime :: !Tm -> Time
/**
* Calculate the difference in seconds between two times
*/
diffTime :: !Time !Time -> Int
/**
* Format the time structure using the format defined by C's time.h
*/
strfTime :: !String !Tm -> String
implementation module Time
import StdString, StdArray, StdClass, StdOverloaded, StdInt
import code from "_TimeC.o"
instance toString Tm
where
toString tm
# string = createArray 24 '\0'
# ok = toStringTmC {tm.sec,tm.min,tm.hour,tm.mday,tm.mon,tm.year,tm.wday,tm.yday,if tm.isdst 1 0} string
= if (ok == 0) string ""
where
toStringTmC :: !{#Int} !{#Char} -> Int
toStringTmC a s = code {
ccall toStringTmC "AS:I"
}
instance toString Time
where
toString (Time t)
# string = createArray 24 '\0'
# ok = toStringTimeC t string
= if (ok == 0) string ""
where
toStringTimeC :: !Int !{#Char} -> Int
toStringTimeC t s = code {
ccall toStringTimeC "IS:I"
}
instance toString Clock
where
toString (Clock c) = toString c
clock :: !*World -> (!Clock, !*World)
clock world
# (c, world) = clockC world
= (Clock c, world)
where
clockC :: !*World -> (!Int, !*World)
clockC world = code {
ccall clockC ":I:A"
}
time :: !*World -> (!Time, !*World)
time world
# (t, world) = timeC world
= (Time t, world)
where
timeC :: !*World -> (!Int,!*World)
timeC world = code {
ccall timeC ":I:A"
}
gmTime :: !*World -> (!Tm, !*World)
gmTime world
# tm = createArray 9 0
# world = gmTimeC tm world
= ({ sec = tm.[0], min = tm.[1], hour = tm.[2]
, mday = tm.[3], mon = tm.[4], year = tm.[5]
, wday = tm.[6], yday = tm.[7], isdst = tm.[8] <> 0
}, world)
where
gmTimeC :: !{#Int} !*World -> *World
gmTimeC tm world = code {
ccall gmTimeC "A:V:A"
fill_a 0 1
pop_a 1
}
localTime :: !*World -> (!Tm, !*World)
localTime world
# tm = createArray 9 0
# world = localTimeC tm world
= ({ sec = tm.[0], min = tm.[1], hour = tm.[2]
, mday = tm.[3], mon = tm.[4], year = tm.[5]
, wday = tm.[6], yday = tm.[7], isdst = tm.[8] <> 0
}, world)
where
localTimeC :: !{#Int} !*World -> *World
localTimeC tm world = code {
ccall localTimeC "A:V:A"
fill_a 0 1
pop_a 1
}
mkTime :: !Tm -> Time
mkTime tm
# t = mkTimeC {tm.sec,tm.min,tm.hour,tm.mday,tm.mon,tm.year,tm.wday,tm.yday,if tm.isdst 1 0}
= Time t
where
mkTimeC :: !{#Int} -> Int
mkTimeC tm = code {
ccall mkTimeC "A:I"
}
diffTime :: !Time !Time -> Int
diffTime (Time t1) (Time t2) = t1 - t2
strfTime :: !String !Tm -> String
strfTime format tm
# format = format +++ "\0"
# (len,ptr) = strfTimeC format {tm.sec,tm.min,tm.hour,tm.mday,tm.mon,tm.year,tm.wday,tm.yday,if tm.isdst 1 0}
# string = createArray len '\0'
# ok = copyStringC len ptr string
= if (ok == 0) string ""
where
strfTimeC :: !String !{#Int} -> (!Int,!Int)
strfTimeC format tm = code {
ccall strfTimeC "SA:VII"
}
copyStringC :: !Int !Int !String -> Int
copyStringC len ptr s = code {
ccall copyStringC "IIS:I"
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment