summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/palmunits/src/datetime.pp
blob: c4023090b86d133938da3fda4f0428b75130b6f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
(******************************************************************************
 *
 * Copyright (c) 1995-2000 Palm, Inc. or its subsidiaries.
 * All rights reserved.
 *
 * File: DateTime.h
 *
 * Release: Palm OS SDK 4.0 (63220)
 *
 * Description:
 *    Date and Time calculations
 *
 * History:
 *    1/19/95  rsf - Created by Roger Flores
 *    7/15/99  rsf - moved some types in from Preferences.h
 *   12/23/99  jmp - eliminated bogus maxTime definition
 *    05/16/00 CS    Changed DayOfWeekType ot DayOfMonthType.
 *
 *****************************************************************************)
unit datetime;

interface

uses palmos, coretraps, chars, localemgr;

type
  TimeFormatType = Enum;

const
  tfColon = 0;
  tfColonAMPM = Succ(tfColon);    // 1:00 pm
  tfColon24h = Succ(tfColonAMPM); // 13:00
  tfDot = Succ(tfColon24h);
  tfDotAMPM = Succ(tfDot);        // 1.00 pm
  tfDot24h = Succ(tfDotAMPM);     // 13.00
  tfHoursAMPM = Succ(tfDot24h);   // 1 pm
  tfHours24h = Succ(tfHoursAMPM); // 13
  tfComma24h = Succ(tfHours24h);  // 13,00

type
  DaylightSavingsTypes = Enum;

const
  dsNone = 0;                                 // Daylight Savings Time not observed
  dsUSA = Succ(dsNone);                       // United States Daylight Savings Time
  dsAustralia = Succ(dsUSA);                  // Australian Daylight Savings Time
  dsWesternEuropean = Succ(dsAustralia);      // Western European Daylight Savings Time
  dsMiddleEuropean = Succ(dsWesternEuropean); // Middle European Daylight Savings Time
  dsEasternEuropean = Succ(dsMiddleEuropean); // Eastern European Daylight Savings Time
  dsGreatBritain = Succ(dsEasternEuropean);   // Great Britain and Eire Daylight Savings Time
  dsRumania = Succ(dsGreatBritain);           // Rumanian Daylight Savings Time
  dsTurkey = Succ(dsRumania);                 // Turkish Daylight Savings Time
  dsAustraliaShifted = Succ(dsTurkey);        // Australian Daylight Savings Time with shift in 1986

// pass a TimeFormatType
// Use24HourFormat(t) ((t) == tfColon24h || (t) == tfDot24h || (t) == tfHours24h || (t) == tfComma24h)
// TimeSeparator(t) ((Char) ( t <= tfColon24h ? ':' : (t <= tfDot24h ? '.' : ',')))

type
  DateFormatType = Enum;

const
  dfMDYWithSlashes = 0;                        // 12/31/95
  dfDMYWithSlashes = Succ(dfMDYWithSlashes);   // 31/12/95
  dfDMYWithDots = Succ(dfDMYWithSlashes);      // 31.12.95
  dfDMYWithDashes = Succ(dfDMYWithDots);       // 31-12-95
  dfYMDWithSlashes = Succ(dfDMYWithDashes);    // 95/12/31
  dfYMDWithDots = Succ(dfYMDWithSlashes);      // 95.12.31
  dfYMDWithDashes = Succ(dfYMDWithDots);       // 95-12-31

  dfMDYLongWithComma = Succ(dfYMDWithDashes);  // Dec 31, 1995
  dfDMYLong = Succ(dfMDYLongWithComma);        // 31 Dec 1995
  dfDMYLongWithDot = Succ(dfDMYLong);          // 31. Dec 1995
  dfDMYLongNoDay = Succ(dfDMYLongWithDot);     // Dec 1995
  dfDMYLongWithComma = Succ(dfDMYLongNoDay);   //  31 Dec, 1995
  dfYMDLongWithDot = Succ(dfDMYLongWithComma); //  1995.12.31
  dfYMDLongWithSpace = Succ(dfYMDLongWithDot); //  1995 Dec 31

  dfMYMed = Succ(dfYMDLongWithSpace);          //  Dec '95
  dfMYMedNoPost = Succ(dfMYMed);               //  Dec 95     (added for French 2.0 ROM)
  dfMDYWithDashes = Succ(dfMYMedNoPost);       // 12-31-95    (added for 4.0 ROM)

type
  DateTimeType = record
    second: Int16;
    minute: Int16;
    hour: Int16;
    day: Int16;
    month: Int16;
    year: Int16;
    weekDay: Int16; // Days since Sunday (0 to 6)
  end;

  DateTimePtr = ^DateTimeType;

// This is the time format.  Times are treated as words so don't
// change the order of the members in this structure.
//

  TimeType = record
    hours: UInt8;
    minutes: UInt8;
  end;

  TimePtr = ^TimeType;

const
  noTime = -1; // The entire TimeType is -1 if there isn't a time.


// This is the date format.  Dates are treated as words so don't
// change the order of the members in this structure.
//

type
  DateType = record
    Bits: UInt16;
{
    UInt16 year  :7;                   // years since 1904 (MAC format)
    UInt16 month :4;
    UInt16 day   :5;
}
  end;

  DatePtr = ^DateType;

(************************************************************
 * Date Time Constants
 *************************************************************)

// Maximum lengths of strings return by the date and time formating
// routine DateToAscii and TimeToAscii.
const
  timeStringLength     = 9;
  dateStringLength     = 9;
  longDateStrLength    = 15;
  dowDateStringLength  = 19;
  dowLongDateStrLength = 25;
  timeZoneStringLength = 50;

  firstYear            = 1904;
  numberOfYears        = 128;
  lastYear             = firstYear + numberOfYears - 1;

// Constants for time calculations
// Could change these from xIny to yPerX
  secondsInSeconds     = 1;
  minutesInSeconds     = 60;
  hoursInMinutes       = 60;
  hoursInSeconds       = hoursInMinutes * minutesInSeconds;
  hoursPerDay          = 24;
// daysInSeconds     ((Int32)(hoursPerDay) * ((Int32)hoursInSeconds))
  daysInSeconds        = $15180; // cc bug

  daysInWeek           = 7;
  daysInYear           = 365;
  daysInLeapYear       = 366;
  daysInFourYears      = daysInLeapYear + 3 * daysInYear;

  monthsInYear         = 12;

  maxDays              = UInt32(numberOfYears div 4 * daysInFourYears - 1);
  maxSeconds           = UInt32((maxDays+1) * daysInSeconds - 1);

// Values returned by DayOfWeek routine.
  sunday               = 0;
  monday               = 1;
  tuesday              = 2;
  wednesday            = 3;
  thursday             = 4;
  friday               = 5;
  saturday             = 6;

// Months of the year
  january              = 1;
  february             = 2;
  march                = 3;
  april                = 4;
  may                  = 5;
  june                 = 6;
  july                 = 7;
  august               = 8;
  september            = 9;
  october              = 10;
  november             = 11;
  december             = 12;

// Values returned by DayOfMonth routine.
type
  DayOfMonthType = Enum;

// It would have been cool to have a real DayOfWeekType, but we #define the
// following for compatibility with existing code.  Please use the new name
// (DayOfMonthType).
  DayOfWeekType = DayOfMonthType;

const
  dom1stSun = 0;
  dom1stMon = Succ(dom1stSun);
  dom1stTue = Succ(dom1stMon);
  dom1stWen = Succ(dom1stTue);
  dom1stThu = Succ(dom1stWen);
  dom1stFri = Succ(dom1stThu);
  dom1stSat = Succ(dom1stFri);
  dom2ndSun = Succ(dom1stSat);
  dom2ndMon = Succ(dom2ndSun);
  dom2ndTue = Succ(dom2ndMon);
  dom2ndWen = Succ(dom2ndTue);
  dom2ndThu = Succ(dom2ndWen);
  dom2ndFri = Succ(dom2ndThu);
  dom2ndSat = Succ(dom2ndFri);
  dom3rdSun = Succ(dom2ndSat);
  dom3rdMon = Succ(dom3rdSun);
  dom3rdTue = Succ(dom3rdMon);
  dom3rdWen = Succ(dom3rdTue);
  dom3rdThu = Succ(dom3rdWen);
  dom3rdFri = Succ(dom3rdThu);
  dom3rdSat = Succ(dom3rdFri);
  dom4thSun = Succ(dom3rdSat);
  dom4thMon = Succ(dom4thSun);
  dom4thTue = Succ(dom4thMon);
  dom4thWen = Succ(dom4thTue);
  dom4thThu = Succ(dom4thWen);
  dom4thFri = Succ(dom4thThu);
  dom4thSat = Succ(dom4thFri);
  domLastSun = Succ(dom4thSat);
  domLastMon = Succ(domLastSun);
  domLastTue = Succ(domLastMon);
  domLastWen = Succ(domLastTue);
  domLastThu = Succ(domLastWen);
  domLastFri = Succ(domLastThu);
  domLastSa = Succ(domLastFri);

// Values used by DateTemplateToAscii routine.
  dateTemplateChar = chrCircumflexAccent;

  dateTemplateDayNum = '0';
  dateTemplateDOWName = Succ(dateTemplateDayNum);
  dateTemplateMonthName = Succ(dateTemplateDOWName);
  dateTemplateMonthNum = Succ(dateTemplateMonthName);
  dateTemplateYearNum = Succ(dateTemplateMonthNum);

  dateTemplateShortModifier    = 's';
  dateTemplateRegularModifier  = 'r';
  dateTemplateLongModifier     = 'l';
  dateTemplateLeadZeroModifier = 'z';

//************************************************************
//* Date and Time macros
//***********************************************************

{
// Convert a date in a DateType structure to an UInt16.
  DateToInt(date) (*(UInt16 *) &date)


// Convert a date in a DateType structure to a signed int.
  TimeToInt(time) (*(Int16 *) &time)
}

//************************************************************
//* Date Time procedures
//************************************************************

procedure TimSecondsToDateTime(seconds: UInt32; dateTimeP: DateTimePtr); syscall sysTrapTimSecondsToDateTime;

function TimDateTimeToSeconds(dateTimeP: DateTimePtr): UInt32; syscall sysTrapTimDateTimeToSeconds;

procedure TimAdjust(dateTimeP: DateTimePtr; adjustment: Int32); syscall sysTrapTimAdjust;

procedure TimeToAscii(hours, minutes: UInt8; timeFormat: TimeFormatType; pString: PChar); syscall sysTrapTimeToAscii;

function TimTimeZoneToUTC(seconds: UInt32; timeZone: Int16; daylightSavingAdjustment: Int16): UInt32; syscall sysTrapTimTimeZoneToUTC;

function TimUTCToTimeZone(seconds: UInt32; timeZone: Int16; daylightSavingAdjustment: Int16): UInt32; syscall sysTrapTimUTCToTimeZone;

procedure TimeZoneToAscii(timeZone: Int16; localeP: LmLocalePtr; string_: PChar); syscall sysTrapTimeZoneToAscii;

function DaysInMonth(month, year: Int16): Int16; syscall sysTrapDaysInMonth;

function DayOfWeek(month, day, year: Int16): Int16; syscall sysTrapDayOfWeek;

function DayOfMonth(month, day, year: Int16): Int16; syscall sysTrapDayOfMonth;

// Date routines.
procedure DateSecondsToDate(seconds: UInt32; date: DatePtr); syscall sysTrapDateSecondsToDate;

procedure DateDaysToDate(days: UInt32; date: DatePtr); syscall sysTrapDateDaysToDate;

function DateToDays(date: DateType): UInt32; syscall sysTrapDateToDays;

procedure DateAdjust(dateP: DatePtr; adjustment: Int32); syscall sysTrapDateAdjust;

procedure DateToAscii(months, days: UInt8; years: UInt16;
                      dateFormat: DateFormatType; pString: PChar); syscall sysTrapDateToAscii;

procedure DateToDOWDMFormat(months, days: UInt8; years: UInt16;
                            dateFormat: DateFormatType; pString: PChar); syscall sysTrapDateToDOWDMFormat;

function DateTemplateToAscii(const templateP: PChar; months, days: UInt8;
                             years: UInt16; stringP: PChar; stringLen: Int16): UInt16; syscall sysTrapDateTemplateToAscii;

implementation

end.