Klaus F. Ãstergaard wrote:
type TimeStamp = {@@packed} record DateValid, TimeValid : Boolean; Year : Integer; Month : 1 .. 12; Day : 1 .. 31; DayOfWeek : 0 .. 6; { 0 means Sunday } Hour : 0 .. 23; Minute : 0 .. 59; Second : 0 .. 61; { to allow for leap seconds } MicroSecond : 0 .. 999999 end;
Is there a function/procedure that calculates the DateValid and TimeValid. Does it proof the DayOfWeek?
Not yet. Currently, routines that set the time (e.g. GetTimeStamp) just set them to True after storing the current date/time and to False if there was any error.
A routine to check a given date (with or without checking DayOfWeek) would be nice, and I might write it someday (too busy right now), but if you have such a routine to contribute, let me know...
What is leap seconds and how does one calculate with leap seconds?
Leap seconds are introduced once every few years (I think) to keep the official time in sync with the motion of the earth. Unless you're into very high precision timing operations, you shouldn't have to worry about them. It's just that some moments in time have to be represented with second values > 59. Note, many (most?) OSs don't support leap seconds at all (especially on PCs -- their timers are far more inaccurate, anyway).
Does any one know of standard libs/rutines for doing calculations/manipulations with the timestamp, like calculating the day of the week
GetDayOfWeek (unit GPC).
or finding time between two timestamps, with corrections to the diffent length of months ect.
Convert to Unix time (number of seconds since midnight UTC 1970-01-01 which is much more convenient to calculate with), do your calculations, convert back. (Despite its name, Unix time is supported on all platforms, not only Unix.) See UnixTimeToTimeStamp and TimeStampToUnixTime.
E.g., I use the following routine to increment by a number of days, leaving the time unchanged. This takes into account possible DST changes (otherwise you could just increment by multiples of 24*60*60 seconds, but when crossing the beginning/ending of DST, this would be off by 1 hour, and might even land in the wrong day if the time was within the last/first hour of the day).
procedure IncDateDays (var Date : TimeStamp; Days : Integer); var DateSave : TimeStamp; begin DateSave := Date; Date.TimeValid := True; Date.Hour := 12; { to avoid DST problems } UnixTimeToTimeStamp (TimeStampToUnixTime (Date) + Days * 24 * 60 * 60, Date); Date.TimeValid := DateSave.TimeValid; Date.Hour := DateSave.Hour; Date.Minute := DateSave.Minute; Date.Second := DateSave.Second; Date.MicroSecond := DateSave.MicroSecond end;
To output date/time in (almost) any conceivable format, see FormatTime (in recent GPC versions).
Frank