- Tabs to spaces.
[AROS.git] / workbench / libs / locale / locstrtodate.c
blob283342004879ac0f95653eeb5ed0e2fe134c4129
1 /*
2 Copyright © 1995-2011, 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 #define YEAR_FORMAT "%y"
24 #ifndef FORMAT_DEF
25 #define FORMAT_DEF 4
26 #endif
28 static const TEXT long_dos_time_format[] = "%H:%M:%S";
29 static const TEXT short_dos_time_format[] = "%H:%M";
31 AROS_UFH3(ULONG, LocStrToDateGetCharFunc,
32 AROS_UFHA(struct Hook *, hook, A0),
33 AROS_UFHA(struct Locale *, locale, A2),
34 AROS_UFHA(ULONG, null, A1))
36 AROS_USERFUNC_INIT
38 STRPTR *buf = (STRPTR *)hook->h_Data;
40 return *(*buf)++;
42 AROS_USERFUNC_EXIT
45 /*****************************************************************************
47 NAME */
48 #include <proto/locale.h>
50 AROS_PLH1(LONG, LocStrToDate,
52 /* SYNOPSIS */
53 AROS_LHA(struct DateTime *, datetime, D1),
55 /* LOCATION */
56 struct DosLibrary *, DOSBase, 37, Locale)
58 /* FUNCTION
59 See dos.library/StrToDate
61 INPUTS
62 See dos.library/StrToDate
64 RESULT
66 NOTES
67 This function is not called by apps directly. Instead
68 dos.library/StrToDate() is patched to use this function. This means
69 that the LocaleBase parameter above actually points to DOSBase, so we
70 make use of the global LocaleBase variable. This function is marked as
71 private, thus the headers generator won't mind the different basename
72 in the header.
74 EXAMPLE
76 BUGS
78 SEE ALSO
79 dos.library/StrToDate, locale.library/ParseDate.
81 INTERNALS
83 *****************************************************************************/
85 AROS_LIBFUNC_INIT
87 struct Locale *loc;
88 struct Hook hook;
89 CONST_STRPTR buf, fstring, altfstring;
90 LONG days;
91 LONG retval = TRUE;
93 hook.h_Entry = (HOOKFUNC) AROS_ASMSYMNAME(LocStrToDateGetCharFunc);
94 hook.h_Data = &buf;
96 REPLACEMENT_LOCK;
98 loc = (struct Locale *)IntLB(LocaleBase)->lb_CurrentLocale;
100 if (datetime->dat_StrDate)
102 struct DateStamp curr;
104 buf = datetime->dat_StrDate;
106 DateStamp(&curr);
108 if (!strnicmp(buf, GetLocaleStr(loc, YESTERDAYSTR),
109 strlen(GetLocaleStr(loc, YESTERDAYSTR))))
111 datetime->dat_Stamp.ds_Days = curr.ds_Days - 1;
113 else if (!strnicmp(buf, GetLocaleStr(loc, TODAYSTR),
114 strlen(GetLocaleStr(loc, TODAYSTR))))
116 datetime->dat_Stamp.ds_Days = curr.ds_Days;
118 else if (!strnicmp(buf, GetLocaleStr(loc, TOMORROWSTR),
119 strlen(GetLocaleStr(loc, TOMORROWSTR))))
121 datetime->dat_Stamp.ds_Days = curr.ds_Days + 1;
123 else
125 WORD i;
127 for (i = 0; i < 7; i++)
129 if (!strnicmp(buf, GetLocaleStr(loc, DAY_1 + i),
130 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;
234 if (retval && datetime->dat_StrTime)
236 struct DateStamp ds;
238 buf = datetime->dat_StrTime;
240 switch (datetime->dat_Format)
242 case FORMAT_DEF:
243 fstring = loc->loc_ShortTimeFormat;
244 break;
246 default:
247 if (ParseDate(loc, NULL, long_dos_time_format, &hook))
248 fstring = long_dos_time_format;
249 else
250 fstring = short_dos_time_format;
251 buf = datetime->dat_StrTime;
252 break;
255 if (ParseDate(loc, &ds, fstring, &hook))
257 datetime->dat_Stamp.ds_Minute = ds.ds_Minute;
258 datetime->dat_Stamp.ds_Tick = ds.ds_Tick;
260 else
262 retval = FALSE;
266 REPLACEMENT_UNLOCK;
268 return retval;
270 AROS_LIBFUNC_EXIT