FreeBASIC  0.91.0
drv_intl_getdateformat.c
Go to the documentation of this file.
1 /* get localized short DATE format */
2 
3 #include "../fb.h"
4 #include "fb_private_intl.h"
5 
6 int fb_DrvIntlGetDateFormat( char *buffer, size_t len )
7 {
8  char *pszName;
9  char achFormat[90];
10  char achOrder[3] = { 0 };
11  char achDayZero[2], *pszDayZero;
12  char achMonZero[2], *pszMonZero;
13  char achDate[2], *pszDate;
14  size_t i;
15 
16  DBG_ASSERT(buffer!=NULL);
17 
18  /* Can I use this? The problem is that it returns the date format
19  * with localized separators. */
20  pszName = fb_hGetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE,
21  achFormat, sizeof(achFormat) - 1 );
22  if( pszName!=NULL ) {
23  size_t uiNameSize = strlen(pszName);
24  if( uiNameSize < len ) {
25  strcpy( buffer, pszName );
26  return TRUE;
27  } else {
28  return FALSE;
29  }
30  }
31 
32 
33  /* Fall back for Win95 and WinNT < 4.0 */
34  pszDayZero = fb_hGetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_IDAYLZERO,
35  achDayZero, sizeof(achDayZero) );
36  pszMonZero = fb_hGetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_IMONLZERO,
37  achMonZero, sizeof(achMonZero) );
38  pszDate = fb_hGetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_IDATE,
39  achDate, sizeof(achDate) );
40  if( pszDate!=NULL && pszDayZero!=0 && pszMonZero!=0 ) {
41  switch( atoi( pszDate ) ) {
42  case 0:
43  FB_MEMCPY(achOrder, "mdy", 3);
44  break;
45  case 1:
46  FB_MEMCPY(achOrder, "dmy", 3);
47  break;
48  case 2:
49  FB_MEMCPY(achOrder, "ymd", 3);
50  break;
51  default:
52  break;
53  }
54 
55  if( achOrder[0]!=0 ) {
56  size_t remaining = len - 1;
57  int day_lead_zero = atoi( pszDayZero ) != 0;
58  int mon_lead_zero = atoi( pszMonZero ) != 0;
59  for(i=0; i!=3; ++i) {
60  const char *pszAdd = NULL;
61  size_t add_len;
62  switch ( achOrder[i] ) {
63  case 'm':
64  if( mon_lead_zero ) {
65  pszAdd = "MM";
66  } else {
67  pszAdd = "M";
68  }
69  break;
70  case 'd':
71  if( day_lead_zero ) {
72  pszAdd = "dd";
73  } else {
74  pszAdd = "d";
75  }
76  break;
77  case 'y':
78  pszAdd = "yyyy";
79  break;
80  }
81  add_len = strlen(pszAdd);
82  if( remaining < add_len )
83  return FALSE;
84  strcpy( buffer, pszAdd );
85  buffer += add_len;
86  remaining -= add_len;
87  if( i!=2 ) {
88  if( remaining==0 )
89  return FALSE;
90  strcpy( buffer, "/" );
91  buffer += 1;
92  remaining -= 1;
93  }
94  }
95  return TRUE;
96  }
97  }
98 
99  return FALSE;
100 }