Add czech.ct locale for WiMP (tools)
[AROS.git] / rom / dos / datetostr.c
blob781adcadcd4d6284333ea966aa123b3046b7cbf4
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Convert a DateTime struct into strings.
6 Lang: english
7 */
8 #include "dos_intern.h"
9 #include "date.h"
11 #ifdef TEST
12 #include <proto/dos.h>
13 #include <stdio.h>
14 #undef AROS_LH1
15 #undef DateToStr
16 #undef AROS_LHA
17 #define AROS_LH1(ret,name,arg,type,base,offset,libname) \
18 ret name (arg)
19 #define AROS_LHA(type,name,reg) type name
20 #endif
22 const ULONG Dos_DayTable[]=
24 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
27 const char *const Dos_MonthTable[]=
29 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
30 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
33 const char *const Dos_WeekTable[]=
35 "Sunday", "Monday", "Tuesday", "Wednesday",
36 "Thursday", "Friday", "Saturday"
39 const char *const Dos_SubstDateTable[]=
41 "Future", "Tomorrow", "Today", "Yesterday"
44 /*****************************************************************************
46 NAME */
47 #include <dos/datetime.h>
48 #include <proto/dos.h>
50 AROS_LH1(BOOL, DateToStr,
52 /* SYNOPSIS */
53 AROS_LHA(struct DateTime *, datetime, D1),
55 /* LOCATION */
56 struct DosLibrary *, DOSBase, 124, Dos)
58 /* FUNCTION
59 DateToStr converts an AmigaDOS DateStamp to a human
60 readable ASCII string as requested by your settings in the
61 DateTime structure.
63 INPUTS
64 DateTime - a pointer to an initialized DateTime structure. The
65 DateTime structure should be initialized as follows:
67 dat_Stamp: The datestamp to convert to ascii
69 dat_Format: How to convert the datestamp into
70 dat_StrDate. Can be any of the following:
72 FORMAT_DOS: AmigaDOS format (dd-mmm-yy). This
73 is the default if you specify something other
74 than any entry in this list.
76 FORMAT_INT: International format (yy-mmm-dd).
78 FORMAT_USA: American format (mm-dd-yy).
80 FORMAT_CDN: Canadian format (dd-mm-yy).
82 FORMAT_DEF default format for locale.
85 dat_Flags: Modifies dat_Format. The only flag
86 used by this function is DTF_SUBST. If set, then
87 a string like "Today" or "Monday" is generated
88 instead of the normal format if possible.
90 dat_StrDay: Pointer to a buffer to receive the day of
91 the week string. (Monday, Tuesday, etc.). If null,
92 this string will not be generated.
94 dat_StrDate: Pointer to a buffer to receive the date
95 string, in the format requested by dat_Format,
96 subject to possible modifications by DTF_SUBST. If
97 null, this string will not be generated.
99 dat_StrTime: Pointer to a buffer to receive the time
100 of day string. If NULL, this will not be generated.
103 RESULT
104 A zero return indicates that the DateStamp was invalid, and could
105 not be converted. Non-zero indicates that the call succeeded.
107 NOTES
109 EXAMPLE
111 BUGS
113 SEE ALSO
114 DateStamp(), StrtoDate()
116 INTERNALS
118 *****************************************************************************/
120 AROS_LIBFUNC_INIT
122 /* Starting days of the months in a leap year. */
124 STRPTR buf, fstring;
125 const UBYTE * name;
127 LONG year, month, days, mins, tick, leap=1;
129 /* Read time. */
130 days=datetime->dat_Stamp.ds_Days;
131 mins=datetime->dat_Stamp.ds_Minute;
132 tick=datetime->dat_Stamp.ds_Tick;
135 Check if timestamp is correct. Correct timestamps lie
136 between the 1.1.1978 0:00:00 and the 11.7.5881588 23:59:59.
138 if(days<0||(ULONG)mins>=24*60||(ULONG)tick>=TICKS_PER_SECOND*60)
139 return DOSFALSE;
141 if(datetime->dat_StrDay!=NULL)
143 /* Get weekday name. The 1.1.1978 was a sunday. */
144 buf=datetime->dat_StrDay;
145 name=Dos_WeekTable[days%7];
146 while((*buf++=*name++)!=0)
150 if(datetime->dat_StrDate!=NULL)
153 Calculate year and the days in the year. Use a short method
154 if the date is between the 1.1.1978 and the 1.1.2100:
155 Every year even divisible by 4 in this time span is a leap year.
156 There are 92 normal and 30 leap years there.
158 if(days<92*365+30*366)
161 1976 was a leap year so use it as a base to divide the days
162 into 4-year blocks (each beginning with a leap year).
164 days+=366+365;
165 year=4*(days/(366+3*365))+1976;
166 days%=(366+3*365);
168 /* Now divide the 4-year blocks into single years. */
169 if (days>=366)
171 leap=0;
172 days--;
173 year+=days/365;
174 days%=365;
177 else
180 The rule for calendar calculations are:
181 1. Every year even divisible by 4 is a leap year.
182 2. As an exception from rule 1 every year even divisible by
183 100 is not.
184 3. Every year even divisible by 400 is a leap year as an
185 exception from rule 2.
186 So 1996, 2000 and 2004 are leap years - 1900 and 1999 are not.
188 Use 2000 as a base to devide the days into 400 year blocks,
189 those into 100 year blocks and so on...
191 days-=17*365+5*366;
192 year=400*(days/(97*366+303*365))+2000;
193 days%=(97*366+303*365);
195 if(days>=366)
197 leap=0;
198 days--;
199 year+=100*(days/(24*366+76*365));
200 days%=(24*366+76*365);
202 if(days>=365)
204 leap=1;
205 days++;
206 year+=4*(days/(366+3*365));
207 days%=(366+3*365);
209 if(days>=366)
211 leap=0;
212 days--;
213 year+=days/365;
214 days%=365;
220 The starting-day table assumes a leap year - so add one day if
221 the date is after february 28th and the year is no leap year.
223 if(!leap&&days>=31+28)
224 days++;
226 for(month=11;;month--)
228 if(days>=Dos_DayTable[month])
230 days-=Dos_DayTable[month];
231 break;
235 /* Remember that 0 means 1.1.1978. */
236 days++;
237 month++;
239 /* Build date string */
240 buf=datetime->dat_StrDate;
242 switch (datetime->dat_Format)
244 case FORMAT_INT: fstring="y-M-d"; break;
245 case FORMAT_USA: fstring="m-d-y"; break;
246 case FORMAT_CDN: fstring="d-m-y"; break;
247 default: fstring="d-M-y"; break;
250 if (datetime->dat_Flags & DTF_SUBST)
252 struct DateStamp curr;
254 DateStamp (&curr);
256 curr.ds_Days -= datetime->dat_Stamp.ds_Days;
258 if (curr.ds_Days <= 7)
260 fstring = "";
262 if ((curr.ds_Days <= 1) && (curr.ds_Days >= -1))
263 name = Dos_SubstDateTable[curr.ds_Days+2];
264 else if (curr.ds_Days < -1)
265 name = Dos_SubstDateTable[0];
266 else
267 name = Dos_WeekTable[datetime->dat_Stamp.ds_Days%7];
269 while ((*buf++ = *name++) != 0);
273 if (*fstring)
275 while (*fstring)
277 switch(*fstring)
279 case 'y':
280 *buf++ = year/10%10+'0';
281 *buf++ = year%10+'0';
282 break;
283 case 'm':
284 name=Dos_MonthTable[month-1];
285 while(*name)
286 *buf++=*name++;
287 break;
288 case 'M':
289 *buf++=month/10%10+'0';
290 *buf++=month%10+'0';
291 break;
292 case 'd':
293 *buf++=days/10%10+'0';
294 *buf++=days%10+'0';
295 break;
296 default:
297 *buf++=*fstring;
298 break;
301 fstring ++;
304 *buf = 0;
308 if(datetime->dat_StrTime!=NULL)
310 /* Build time string */
311 datetime->dat_StrTime[0] = mins/(10*60)+'0';
312 datetime->dat_StrTime[1] = mins/60%10+'0';
313 datetime->dat_StrTime[2] = ':';
314 datetime->dat_StrTime[3] = mins/10%6+'0';
315 datetime->dat_StrTime[4] = mins%10+'0';
316 datetime->dat_StrTime[5] = ':';
317 datetime->dat_StrTime[6] = tick/(10*TICKS_PER_SECOND)+'0';
318 datetime->dat_StrTime[7] = tick/TICKS_PER_SECOND%10+'0';
319 datetime->dat_StrTime[8] = 0;
322 /* All done. Return OK. */
323 return DOSTRUE;
324 AROS_LIBFUNC_EXIT
325 } /* DateToStr */
327 #ifdef TEST
329 # include <stdio.h>
330 # include <dos/datetime.h>
331 # include <proto/dos.h>
333 int main (int argc, char ** argv)
335 struct DateTime curr;
336 char day[LEN_DATSTRING];
337 char time[LEN_DATSTRING];
338 char date[LEN_DATSTRING];
340 DateStamp (&curr.dat_Stamp);
342 curr.dat_Format = FORMAT_DOS;
343 curr.dat_Flags = 0;
344 curr.dat_StrDay = day;
345 curr.dat_StrDate = date;
346 curr.dat_StrTime = time;
348 DateToStr (&curr);
350 printf ("Today is %s, %s. It's %s\n"
351 , day
352 , date
353 , time
356 curr.dat_Format = FORMAT_DEF;
358 DateToStr (&curr);
360 printf ("Local date: %s\n", date);
362 curr.dat_Flags = DTF_SUBST;
364 DateToStr (&curr);
366 printf ("Date with DTF_SUBST: %s\n", date);
368 curr.dat_Stamp.ds_Days ++;
370 DateToStr (&curr);
372 printf ("Date +1 with DTF_SUBST: %s\n", date);
374 curr.dat_Stamp.ds_Days ++;
376 DateToStr (&curr);
378 printf ("Date +2 with DTF_SUBST: %s\n", date);
380 curr.dat_Stamp.ds_Days ++;
382 DateToStr (&curr);
384 printf ("Date +3 with DTF_SUBST: %s\n", date);
386 curr.dat_Stamp.ds_Days -= 4;
388 DateToStr (&curr);
390 printf ("Date -1 with DTF_SUBST: %s\n", date);
392 curr.dat_Stamp.ds_Days --;
394 DateToStr (&curr);
396 printf ("Date -2 with DTF_SUBST: %s\n", date);
398 curr.dat_Stamp.ds_Days --;
400 DateToStr (&curr);
402 printf ("Date -3 with DTF_SUBST: %s\n", date);
404 curr.dat_Stamp.ds_Days = 0;
405 curr.dat_Stamp.ds_Minute = 0;
406 curr.dat_Stamp.ds_Tick = 0;
408 DateToStr (&curr);
410 printf ("First Date: %s, %s. Time: %s\n", day, date, time);
412 curr.dat_Stamp.ds_Days = 0;
413 curr.dat_Stamp.ds_Minute = 1;
414 curr.dat_Stamp.ds_Tick = 0;
416 DateToStr (&curr);
418 printf ("First Date + 1 Minute: %s, %s. Time: %s\n", day, date, time);
420 curr.dat_Stamp.ds_Days = 0;
421 curr.dat_Stamp.ds_Minute = 0;
422 curr.dat_Stamp.ds_Tick = 153;
424 DateToStr (&curr);
426 printf ("First Date + 153 Ticks: %s, %s. Time: %s\n", day, date, time);
428 curr.dat_Stamp.ds_Days = 1;
429 curr.dat_Stamp.ds_Minute = 0;
430 curr.dat_Stamp.ds_Tick = 0;
432 DateToStr (&curr);
434 printf ("First Date: %s, %s. Time: %s\n", day, date, time);
436 return 0;
437 } /* main */
439 #endif /* TEST */