add casts to zune macros to silence some warnings
[tangerine.git] / workbench / libs / locale / locstrtodate.c
blob9664c97cafdec5634600a31625eb09bf3b9ab8f2
1 /*
2 Copyright © 1995-2001, 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 AROS_UFH3(ULONG, LocStrToDateGetCharFunc,
33 AROS_UFHA(struct Hook *, hook, A0),
34 AROS_UFHA(struct Locale *, locale, A2),
35 AROS_UFHA(ULONG, null, A1))
37 AROS_USERFUNC_INIT
39 STRPTR *buf = (STRPTR *)hook->h_Data;
41 return *(*buf)++;
43 AROS_USERFUNC_EXIT
46 /*****************************************************************************
48 NAME */
49 #include <proto/locale.h>
51 AROS_PLH1(LONG, LocStrToDate,
53 /* SYNOPSIS */
54 AROS_LHA(struct DateTime *, datetime, D1),
56 /* LOCATION */
57 struct DosLibrary *, DOSBase, 37, Locale)
59 /* FUNCTION
60 See dos.library/StrToDate
62 INPUTS
63 See dos.library/StrToDate
65 RESULT
67 NOTES
68 This function is not called by apps directly. Instead dos.library/DosGet-
69 LocalizedString is patched to use this function. This means, that the
70 LocaleBase parameter above actually points to DOSBase, so we make use of
71 the global LocaleBase variable. This function is marked as private,
72 thus the headers generator won't mind the different basename in the header.
74 EXAMPLE
76 BUGS
78 SEE ALSO
79 dos.library/StrToDate, locale.library/ParseDate.
81 INTERNALS
83 HISTORY
85 *****************************************************************************/
87 AROS_LIBFUNC_INIT
89 struct Locale *loc;
90 struct Hook hook;
91 STRPTR buf, fstring;
92 LONG days;
93 LONG retval = TRUE;
95 hook.h_Entry = (HOOKFUNC)AROS_ASMSYMNAME(LocStrToDateGetCharFunc);
96 hook.h_Data = &buf;
98 REPLACEMENT_LOCK;
100 loc = (struct Locale *)IntLB(LocaleBase)->lb_CurrentLocale;
102 if (datetime->dat_StrDate)
104 struct DateStamp curr;
106 buf = datetime->dat_StrDate;
108 DateStamp(&curr);
110 if (!strnicmp(buf, GetLocaleStr(loc, YESTERDAYSTR), strlen(GetLocaleStr(loc, YESTERDAYSTR))))
112 datetime->dat_Stamp.ds_Days = curr.ds_Days - 1;
114 else if (!strnicmp(buf, GetLocaleStr(loc, TODAYSTR), strlen(GetLocaleStr(loc, TODAYSTR))))
116 datetime->dat_Stamp.ds_Days = curr.ds_Days;
118 else if (!strnicmp(buf, GetLocaleStr(loc, TOMORROWSTR), strlen(GetLocaleStr(loc, TOMORROWSTR))))
120 datetime->dat_Stamp.ds_Days = curr.ds_Days + 1;
122 else
124 WORD i;
126 for(i = 0; i < 7; i++)
128 if (!strnicmp(buf, GetLocaleStr(loc, DAY_1 + i), strlen(GetLocaleStr(loc, DAY_1 + i))))
129 break;
132 if (i != 7)
134 #if 1
135 LONG diffdays;
137 days = curr.ds_Days;
139 diffdays = i - (days % 7);
141 if (datetime->dat_Flags & DTF_FUTURE)
143 if (diffdays > 0)
145 days += diffdays;
147 else
149 days += 7 + diffdays;
152 else
154 if (diffdays < 0)
156 days += diffdays;
158 else
160 days += diffdays - 7;
163 datetime->dat_Stamp.ds_Days = days;
164 #else
165 days = curr.ds_Days;
167 if ((days %7) == 0)
168 days -= 7;
169 else
170 days -= (days % 7);
172 days += i;
174 if (datetime->dat_Flags & DTF_FUTURE)
175 days += 7;
177 datetime->dat_Stamp.ds_Days = days;
178 #endif
180 else
183 switch(datetime->dat_Format)
185 case FORMAT_INT:
186 fstring = YEAR_FORMAT "-%b-%d";
187 break;
189 case FORMAT_USA:
190 fstring = "%m-%d-" YEAR_FORMAT;
191 break;
193 case FORMAT_CDN:
194 fstring = "%d-%m-" YEAR_FORMAT;
195 break;
197 case FORMAT_DEF:
198 fstring = loc->loc_ShortDateFormat;
199 break;
201 default: /* FORMAT_DOS */
202 fstring = "%d-%b-" YEAR_FORMAT;
203 break;
207 if (ParseDate(loc, &curr, fstring, &hook))
209 datetime->dat_Stamp.ds_Days = curr.ds_Days;
211 else
213 retval = FALSE;
220 } /* if (datetime->dat_StrDate) */
223 if (retval && datetime->dat_StrTime)
225 struct DateStamp ds;
227 buf = datetime->dat_StrTime;
229 switch(datetime->dat_Format)
231 case FORMAT_DEF:
232 fstring = loc->loc_ShortTimeFormat;
233 break;
235 default:
236 fstring = "%H:%M:%S";
237 break;
240 if (ParseDate(loc, &ds, fstring, &hook))
242 datetime->dat_Stamp.ds_Minute = ds.ds_Minute;
243 datetime->dat_Stamp.ds_Tick = ds.ds_Tick;
245 else
247 retval = FALSE;
251 REPLACEMENT_UNLOCK;
253 return retval;
255 AROS_LIBFUNC_EXIT
257 } /* LocStrToDate */