Added memory-stored debug output to safe mode.
[cake.git] / workbench / libs / locale / locstrtodate.c
blob56b5280098e4c3d42b5136366ac1a2c78235a968
1 /*
2 Copyright © 1995-2008, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: LocStrToDate - locale.library's private replacement
6 of dos.library/StrToDate function. IPrefs will install
7 the patch.
9 Lang: english
12 #include <exec/types.h>
13 #include <dos/datetime.h>
14 #include <proto/exec.h>
15 #include <proto/dos.h>
16 #include <proto/locale.h>
17 #include "locale_intern.h"
18 #include <aros/asmcall.h>
20 #include <string.h>
22 #if (AROS_FLAVOUR & AROS_FLAVOUR_BINCOMPAT)
23 #define YEAR_FORMAT "%y"
24 #else
25 #define YEAR_FORMAT "%Y"
26 #endif
28 #ifndef FORMAT_DEF
29 #define FORMAT_DEF 4
30 #endif
32 static const TEXT long_dos_time_format[] = "%H:%M:%S";
33 static const TEXT short_dos_time_format[] = "%H:%M";
35 AROS_UFH3(ULONG, LocStrToDateGetCharFunc,
36 AROS_UFHA(struct Hook *, hook, A0),
37 AROS_UFHA(struct Locale *, locale, A2),
38 AROS_UFHA(ULONG, null, A1))
40 AROS_USERFUNC_INIT
42 STRPTR *buf = (STRPTR *)hook->h_Data;
44 return *(*buf)++;
46 AROS_USERFUNC_EXIT
49 /*****************************************************************************
51 NAME */
52 #include <proto/locale.h>
54 AROS_PLH1(LONG, LocStrToDate,
56 /* SYNOPSIS */
57 AROS_LHA(struct DateTime *, datetime, D1),
59 /* LOCATION */
60 struct DosLibrary *, DOSBase, 37, Locale)
62 /* FUNCTION
63 See dos.library/StrToDate
65 INPUTS
66 See dos.library/StrToDate
68 RESULT
70 NOTES
71 This function is not called by apps directly. Instead
72 dos.library/StrToDate() is patched to use this function. This means
73 that the LocaleBase parameter above actually points to DOSBase, so we
74 make use of the global LocaleBase variable. This function is marked as
75 private, thus the headers generator won't mind the different basename
76 in the header.
78 EXAMPLE
80 BUGS
82 SEE ALSO
83 dos.library/StrToDate, locale.library/ParseDate.
85 INTERNALS
87 *****************************************************************************/
89 AROS_LIBFUNC_INIT
91 struct Locale *loc;
92 struct Hook hook;
93 CONST_STRPTR buf, fstring, altfstring;
94 LONG days;
95 LONG retval = TRUE;
97 hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(LocStrToDateGetCharFunc);
98 hook.h_Data = &buf;
100 REPLACEMENT_LOCK;
102 loc = (struct Locale *)IntLB(LocaleBase)->lb_CurrentLocale;
104 if (datetime->dat_StrDate)
106 struct DateStamp curr;
108 buf = datetime->dat_StrDate;
110 DateStamp(&curr);
112 if (!strnicmp(buf, GetLocaleStr(loc, YESTERDAYSTR), strlen(GetLocaleStr(loc, YESTERDAYSTR))))
114 datetime->dat_Stamp.ds_Days = curr.ds_Days - 1;
116 else if (!strnicmp(buf, GetLocaleStr(loc, TODAYSTR), strlen(GetLocaleStr(loc, TODAYSTR))))
118 datetime->dat_Stamp.ds_Days = curr.ds_Days;
120 else if (!strnicmp(buf, GetLocaleStr(loc, TOMORROWSTR), strlen(GetLocaleStr(loc, TOMORROWSTR))))
122 datetime->dat_Stamp.ds_Days = curr.ds_Days + 1;
124 else
126 WORD i;
128 for(i = 0; i < 7; i++)
130 if (!strnicmp(buf, GetLocaleStr(loc, DAY_1 + i), strlen(GetLocaleStr(loc, DAY_1 + i))))
131 break;
134 if (i != 7)
136 #if 1
137 LONG diffdays;
139 days = curr.ds_Days;
141 diffdays = i - (days % 7);
143 if (datetime->dat_Flags & DTF_FUTURE)
145 if (diffdays > 0)
147 days += diffdays;
149 else
151 days += 7 + diffdays;
154 else
156 if (diffdays < 0)
158 days += diffdays;
160 else
162 days += diffdays - 7;
165 datetime->dat_Stamp.ds_Days = days;
166 #else
167 days = curr.ds_Days;
169 if ((days %7) == 0)
170 days -= 7;
171 else
172 days -= (days % 7);
174 days += i;
176 if (datetime->dat_Flags & DTF_FUTURE)
177 days += 7;
179 datetime->dat_Stamp.ds_Days = days;
180 #endif
182 else
185 switch(datetime->dat_Format)
187 case FORMAT_INT:
188 fstring = YEAR_FORMAT "-%b-%d";
189 altfstring = YEAR_FORMAT "-%m-%d";
190 break;
192 case FORMAT_USA:
193 altfstring = fstring = "%m-%d-" YEAR_FORMAT;
194 break;
196 case FORMAT_CDN:
197 altfstring = fstring = "%d-%m-" YEAR_FORMAT;
198 break;
200 case FORMAT_DEF:
201 altfstring = fstring = loc->loc_ShortDateFormat;
202 break;
204 default: /* FORMAT_DOS */
205 fstring = "%d-%b-" YEAR_FORMAT;
206 altfstring = "%d-%m-" YEAR_FORMAT;
207 break;
211 if (ParseDate(loc, &curr, fstring, &hook))
213 datetime->dat_Stamp.ds_Days = curr.ds_Days;
215 else
217 buf = datetime->dat_StrDate;
218 if (ParseDate(loc, &curr, altfstring, &hook))
220 datetime->dat_Stamp.ds_Days = curr.ds_Days;
222 else
224 retval = FALSE;
232 } /* if (datetime->dat_StrDate) */
235 if (retval && datetime->dat_StrTime)
237 struct DateStamp ds;
239 buf = datetime->dat_StrTime;
241 switch(datetime->dat_Format)
243 case FORMAT_DEF:
244 fstring = loc->loc_ShortTimeFormat;
245 break;
247 default:
248 if (ParseDate(loc, NULL, long_dos_time_format, &hook))
249 fstring = long_dos_time_format;
250 else
251 fstring = short_dos_time_format;
252 buf = datetime->dat_StrTime;
253 break;
256 if (ParseDate(loc, &ds, fstring, &hook))
258 datetime->dat_Stamp.ds_Minute = ds.ds_Minute;
259 datetime->dat_Stamp.ds_Tick = ds.ds_Tick;
261 else
263 retval = FALSE;
267 REPLACEMENT_UNLOCK;
269 return retval;
271 AROS_LIBFUNC_EXIT
273 } /* LocStrToDate */