📄 wcsftime.c
字号:
*/
/***
*_W_expandtime() - Expand the conversion specifier
*
*Purpose:
* Expand the given wcsftime conversion specifier using the time struct
* and store it in the supplied buffer.
*
* The expansion is locale-dependent.
*
* *** For internal use with wcsftime() only ***
*
*Entry:
* wchar_t specifier = wcsftime conversion specifier to expand
* const struct tm *tmptr = pointer to time/date structure
* wchar_t **string = address of pointer to output string
* size_t *count = address of char count (space in output area)
* struct __lc_time_data *lc_time = pointer to locale-specific info
*
*Exit:
* BOOL true for success, false for failure
*
*Exceptions:
*
*******************************************************************************/
static BOOL __cdecl _W_expandtime (
_locale_t plocinfo,
wchar_t specifier,
const struct tm *timeptr,
wchar_t **string,
size_t *left,
struct __lc_time_data *lc_time,
unsigned alternate_form
)
{
unsigned temp; /* temps */
int wdaytemp;
/* Use a copy of the appropriate __lc_time_data pointer. This
should prevent the necessity of locking/unlocking in mthread
code (if we can guarantee that the various __lc_time data
structures are always in the same segment). contents of time
strings structure can now change, so thus we do use locking */
switch(specifier) { /* switch on specifier */
case(L'a'): /* abbreviated weekday name */
{
_VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
_W_store_str((wchar_t *)(lc_time->_W_wday_abbr[timeptr->tm_wday]),
string, left);
break;
}
case(L'A'): /* full weekday name */
{
_VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
_W_store_str((wchar_t *)(lc_time->_W_wday[timeptr->tm_wday]),
string, left);
break;
}
case(L'b'): /* abbreviated month name */
{
_VALIDATE_RETURN( ( ( timeptr->tm_mon >=0 ) && ( timeptr->tm_mon <= 11 ) ), EINVAL, FALSE)
_W_store_str((wchar_t *)(lc_time->_W_month_abbr[timeptr->tm_mon]),
string, left);
break;
}
case(L'B'): /* full month name */
{
_VALIDATE_RETURN( ( ( timeptr->tm_mon >=0 ) && ( timeptr->tm_mon <= 11 ) ), EINVAL, FALSE)
_W_store_str((wchar_t *)(lc_time->_W_month[timeptr->tm_mon]),
string, left);
break;
}
case(L'c'): /* date and time display */
if (alternate_form)
{
if(!_W_store_winword( plocinfo,
WW_LDATEFMT,
timeptr,
string,
left,
lc_time))
{
return FALSE;
}
if (*left == 0)
return FALSE;
*(*string)++ = L' ';
(*left)--;
if(!_W_store_winword( plocinfo,
WW_TIMEFMT,
timeptr,
string,
left,
lc_time))
{
return FALSE;
}
}
else {
if(!_W_store_winword( plocinfo,
WW_SDATEFMT,
timeptr,
string,
left,
lc_time))
{
return FALSE;
}
if (*left == 0)
return FALSE;
*(*string)++ = L' ';
(*left)--;
if(!_W_store_winword( plocinfo,
WW_TIMEFMT,
timeptr,
string,
left,
lc_time))
{
return FALSE;
}
}
break;
case(L'd'): /* mday in decimal (01-31) */
/* pass alternate_form as the no leading zeros flag */
{
_VALIDATE_RETURN( ( ( timeptr->tm_mday >=1 ) && ( timeptr->tm_mday <= 31 ) ), EINVAL, FALSE)
_W_store_num(timeptr->tm_mday, 2, string, left,
alternate_form);
break;
}
case(L'H'): /* 24-hour decimal (00-23) */
/* pass alternate_form as the no leading zeros flag */
{
_VALIDATE_RETURN( ( ( timeptr->tm_hour >=0 ) && ( timeptr->tm_hour <= 23 ) ), EINVAL, FALSE)
_W_store_num(timeptr->tm_hour, 2, string, left,
alternate_form);
break;
}
case(L'I'): /* 12-hour decimal (01-12) */
{
_VALIDATE_RETURN( ( ( timeptr->tm_hour >=0 ) && ( timeptr->tm_hour <= 23 ) ), EINVAL, FALSE)
if (!(temp = timeptr->tm_hour%12))
temp=12;
/* pass alternate_form as the no leading zeros flag */
_W_store_num(temp, 2, string, left, alternate_form);
break;
}
case(L'j'): /* yday in decimal (001-366) */
/* pass alternate_form as the no leading zeros flag */
{
_VALIDATE_RETURN( ( ( timeptr->tm_yday >=0 ) && ( timeptr->tm_yday <= 365 ) ), EINVAL, FALSE)
_W_store_num(timeptr->tm_yday+1, 3, string, left,
alternate_form);
break;
}
case(L'm'): /* month in decimal (01-12) */
/* pass alternate_form as the no leading zeros flag */
{
_VALIDATE_RETURN( ( ( timeptr->tm_mon >=0 ) && ( timeptr->tm_mon <= 11 ) ), EINVAL, FALSE)
_W_store_num(timeptr->tm_mon+1, 2, string, left,
alternate_form);
break;
}
case(L'M'): /* minute in decimal (00-59) */
/* pass alternate_form as the no leading zeros flag */
{
_VALIDATE_RETURN( ( ( timeptr->tm_min >=0 ) && ( timeptr->tm_min <= 59 ) ), EINVAL, FALSE)
_W_store_num(timeptr->tm_min, 2, string, left,
alternate_form);
break;
}
case(L'p'): /* AM/PM designation */
{
_VALIDATE_RETURN( ( ( timeptr->tm_hour >=0 ) && ( timeptr->tm_hour <= 23 ) ), EINVAL, FALSE)
if (timeptr->tm_hour <= 11)
_W_store_str((wchar_t *)(lc_time->_W_ampm[0]), string, left);
else
_W_store_str((wchar_t *)(lc_time->_W_ampm[1]), string, left);
break;
}
case(L'S'): /* secs in decimal (00-59) */
/* pass alternate_form as the no leading zeros flag */
{
_VALIDATE_RETURN( ( ( timeptr->tm_sec >=0 ) && ( timeptr->tm_sec <= 59 ) ), EINVAL, FALSE)
_W_store_num(timeptr->tm_sec, 2, string, left,
alternate_form);
break;
}
case(L'U'): /* sunday week number (00-53) */
_VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
wdaytemp = timeptr->tm_wday;
goto weeknum; /* join common code */
case(L'w'): /* week day in decimal (0-6) */
/* pass alternate_form as the no leading zeros flag */
{
_VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
_W_store_num(timeptr->tm_wday, 1, string, left,
alternate_form);
break;
}
case(L'W'): /* monday week number (00-53) */
_VALIDATE_RETURN( ( ( timeptr->tm_wday >=0 ) && ( timeptr->tm_wday <= 6 ) ), EINVAL, FALSE)
if (timeptr->tm_wday == 0) /* monday based */
wdaytemp = 6;
else
wdaytemp = timeptr->tm_wday-1;
weeknum:
_VALIDATE_RETURN( ( ( timeptr->tm_yday >=0 ) && ( timeptr->tm_yday <= 365 ) ), EINVAL, FALSE)
if (timeptr->tm_yday < wdaytemp)
temp = 0;
else {
temp = timeptr->tm_yday/7;
if ((timeptr->tm_yday%7) >= wdaytemp)
temp++;
}
/* pass alternate_form as the no leading zeros flag */
_W_store_num(temp, 2, string, left, alternate_form);
break;
case(L'x'): /* date display */
if (alternate_form)
{
if(!_W_store_winword( plocinfo,
WW_LDATEFMT,
timeptr,
string,
left,
lc_time))
{
return FALSE;
}
}
else
{
if(!_W_store_winword( plocinfo,
WW_SDATEFMT,
timeptr,
string,
left,
lc_time))
{
return FALSE;
}
}
break;
case(L'X'): /* time display */
if(!_W_store_winword( plocinfo,
WW_TIMEFMT,
timeptr,
string,
left,
lc_time))
{
return FALSE;
}
break;
case(L'y'): /* year w/o century (00-99) */
{
_VALIDATE_RETURN( ( timeptr->tm_year >=0 ), EINVAL, FALSE)
temp = timeptr->tm_year%100;
/* pass alternate_form as the no leading zeros flag */
_W_store_num(temp, 2, string, left, alternate_form);
break;
}
case(L'Y'): /* year w/ century */
{
_VALIDATE_RETURN( ( timeptr->tm_year >= -1900 ) && ( timeptr->tm_year <= 8099 ), EINVAL, FALSE)
temp = (((timeptr->tm_year/100)+19)*100) +
(timeptr->tm_year%100);
/* pass alternate_form as the no leading zeros flag */
_W_store_num(temp, 4, string, left, alternate_form);
break;
}
case(L'Z'): /* time zone name, if any */
case(L'z'): /* time zone name, if any */
__tzset(); /* Set time zone info */
_BEGIN_SECURE_CRT_DEPRECATION_DISABLE
{
size_t wnum = 0;
_ERRCHECK(_mbstowcs_s_l(&wnum, *string, *left,
_tzname[((timeptr->tm_isdst)?1:0)], _TRUNCATE, plocinfo));
*string += wnum - 1;
*left -= wnum - 1;
}
_END_SECURE_CRT_DEPRECATION_DISABLE
break;
case(L'%'): /* percent sign */
*(*string)++ = L'%';
(*left)--;
break;
case(L'\004'): /* Workaround issue in older RogueWave libraries */
case(L'\015'):
break;
default: /* unknown format directive */
/* ignore the directive and continue */
/* [ANSI: Behavior is undefined.] */
_ASSERTE( ( "Invalid format directive" , 0 ) );
return FALSE;
break;
} /* end % switch */
return TRUE;
}
/***
*_W_store_str() - Copy a time string
*
*Purpose:
* Copy the supplied time string into the output string until
* (1) we hit a null in the time string, or (2) the given count
* goes to 0.
*
* *** For internal use with wcsftime() only ***
*
*Entry:
* wchar_t *in = pointer to null terminated time string
* wchar_t **out = address of pointer to output string
* size_t *count = address of char count (space in output area)
*
*Exit:
* none
*Exceptions:
*
*******************************************************************************/
static void __cdecl _W_store_str (
wchar_t *in,
wchar_t **out,
size_t *count
)
{
while ((*count != 0) && (*in != L'\0')) {
*(*out)++ = *in++;
(*count)--;
}
}
/***
*_W_store_num() - Convert a number to ascii and copy it
*
*Purpose:
* Convert the supplied number to decimal and store
* in the output buffer. Update both the count and
* buffer pointers.
*
* *** For internal use with wcsftime() only ***
*
*Entry:
* int num = pointer to integer value
* int digits = # of ascii digits to put into string
* wchar_t **out = address of pointer to output string
* size_t *count = address of char count (space in output area)
* unsigned no_lead_zeros = flag indicating that padding by leading
* zeros is not necessary
*
*Exit:
* none
*Exceptions:
*
*******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -