Time
Ketil Malde
ketil+haskell at ii.uib.no
Tue Jan 25 02:56:48 EST 2005
Just a thought:
I think we all agree on having Time as an Integer or similar
representing time with some particular resolution. The question seems
to be how to deal with CalendarTime, defined as a record with fields
for the various components found in a human-type time stamp.
I wonder if it would be worthwhile to pursue a slightly different
approach? The rationale goes as such: we use a calendar and clock
not necessarily to mark points in time, but to be able to point to
some particular day, month, hour, etc. Would it be an idea to define
something like (very rough draft here):
data T = Y Int | M Int T | Wy Int T | Wm Int T | Dy Int T | Dm Int T
| Dw Int T | Hr Int T | Min Int T
deriving (Eq,Show)
The idea is that time can be a year, or a month in some year, or a
week in some year (yes, I know it's ambigous), week in month (in
year), day in year, day in month in year, and so on.
Clearly, deriving Eq is not a good idea - I think Eq should consider
a Dy and Dm equal if they refer to the same day. Now, some functions
to construct values of Time could be
year y = Y y
month m (Y y) = M m (Y y)
And why not create multiple values in one go:
months (Y y) = [M m (Y y) | m <- [1..12]]
days (Y y) = [Dy d (Y y) | d <- [1..(if y `mod` 4 == 0 then 366 else 365)]]
days (M m yy@(Y y))
| m `elem` [1,3,5,7,8,10,12] = [Dm d (M m yy) | d <- [1..31]]
| m == 2 = [Dm d (M m yy) | d <- [1..if y `mod` 4 == 0 then 29 else 28]]
| otherwise = [Dm d (M m yy) | d <- [1..30]]
Now it's easy to get all the days of December this year:
*Main> days $ last $ months $ year 2005
[Dm 1 (M 12 (Y 2005)),Dm 2 (M 12 (Y 2005)),Dm 3 (M 12 (Y 2005)),Dm 4
(M 12 (Y 2005)),Dm 5 (M 12 (Y 2005)),Dm 6 (M 12 (Y 2005)),Dm 7 (M 12
(Y 2005)),Dm 8 (M 12 (Y 2005)) [...]
So, if my local Linux user group has meetings the last Thursday each
month, this can be written something like
map (last . filter isThursday . days) (months $ year 2005)
It's just an idea, but perhaps worth considering? Comments welcome!
-kzm
--
If I haven't seen further, it is by standing in the footprints of giants
More information about the Libraries
mailing list