Add an implementation of wcsftime() (wide character version of strftime()).

This commit is contained in:
Tim J. Robbins 2002-09-11 08:57:11 +00:00
parent 6baa450726
commit 47794211c8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=103218
4 changed files with 155 additions and 2 deletions

View File

@ -101,6 +101,7 @@ typedef __wint_t wint_t;
#endif
struct __sFILE;
struct tm;
__BEGIN_DECLS
wint_t btowc(int);
@ -127,6 +128,8 @@ wchar_t *wcschr(const wchar_t *, wchar_t);
int wcscmp(const wchar_t *, const wchar_t *);
wchar_t *wcscpy(wchar_t * __restrict, const wchar_t * __restrict);
size_t wcscspn(const wchar_t *, const wchar_t *);
size_t wcsftime(wchar_t * __restrict, size_t, const wchar_t * __restrict,
const struct tm * __restrict);
size_t wcslcat(wchar_t *, const wchar_t *, size_t);
size_t wcslcpy(wchar_t *, const wchar_t *, size_t);
size_t wcslen(const wchar_t *);

View File

@ -11,7 +11,8 @@ SRCS+= big5.c btowc.c collate.c collcmp.c euc.c fix_grouping.c frune.c \
mbrtowc.c mbrune.c mbsinit.c mbsrtowcs.c mbtowc.c mbstowcs.c \
mskanji.c nl_langinfo.c nomacros.c none.c rune.c \
runetype.c setinvalidrune.c setlocale.c setrunelocale.c table.c \
tolower.c toupper.c utf2.c wcrtomb.c wcsrtombs.c wcstol.c wcstombs.c \
tolower.c toupper.c utf2.c wcrtomb.c wcsrtombs.c wcsftime.c wcstol.c \
wcstombs.c \
wcstoul.c wctob.c wctomb.c wctrans.c wctype.c wcwidth.c
.if ${LIB} == "c"
@ -22,7 +23,8 @@ MAN+= btowc.3 \
mbsrtowcs.3 multibyte.3 \
nl_langinfo.3 \
rune.3 \
setlocale.3 toascii.3 tolower.3 toupper.3 towlower.3 wcrtomb.3 \
setlocale.3 toascii.3 tolower.3 toupper.3 towlower.3 wcsftime.3 \
wcrtomb.3 \
wcsrtombs.3 wcstol.3 \
wctrans.3 wctype.3 wcwidth.3
MAN+= euc.4 utf2.4

View File

@ -0,0 +1,61 @@
.\" Copyright (c) 2002 Tim J. Robbins
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd September 8, 2002
.Dt WCSFTIME 3
.Os
.Sh NAME
.Nm wcsftime
.Nd "convert date and time to a wide-character string"
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In wchar.h
.Ft size_t
.Fn wcsftime "wchar_t * restrict wcs" "size_t maxsize" "const wchar_t * restrict format" "const struct tm * restrict timeptr"
.Sh DESCRIPTION
The
.Fn wcsftime
function is equivalent to the
.Fn strftime
function except for the types of its arguments.
Refer to
.Xr strftime 3
for a detailed description.
.Sh COMPATIBILITY
Some early implementations of
.Fn wcsftime
had a format argument with type
.Vt "const char *"
instead of
.Vt "const wchar_t *" .
.Sh SEE ALSO
.Xr strftime 3
.Sh STANDARDS
The
.Fn wcsftime
function conforms to
.St -isoC-99 .

View File

@ -0,0 +1,87 @@
/*-
* Copyright (c) 2002 Tim J. Robbins
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <wchar.h>
/*
* Convert date and time to a wide-character string.
*
* This is the wide-character counterpart of strftime(). So that we do not
* have to duplicate the code of strftime(), we convert the format string to
* multibyte, call strftime(), then convert the result back into wide
* characters.
*
* This technique loses in the presence of stateful multibyte encoding if any
* of the conversions in the format string change conversion state. When
* stateful encoding is implemented, we will need to reset the state between
* format specifications in the format string.
*/
size_t
wcsftime(wchar_t * __restrict wcs, size_t maxsize,
const wchar_t * __restrict format, const struct tm * __restrict timeptr)
{
static const mbstate_t initial;
mbstate_t state;
char *dst, *sformat;
size_t n, sflen;
/*
* Convert the supplied format string to a multibyte representation
* for strftime(), which only handles single-byte characters.
*/
state = initial;
sflen = wcsrtombs(NULL, &format, 0, &state);
if (sflen == (size_t)-1)
return (0);
sformat = alloca(sflen + 1);
state = initial;
wcsrtombs(sformat, &format, sflen + 1, &state);
/*
* Allocate memory for longest multibyte sequence that will fit
* into the caller's buffer and call strftime() to fill it.
* Then, copy and convert the result back into wide characters in
* the caller's buffer.
*/
if (SIZE_T_MAX / MB_CUR_MAX <= maxsize)
/* maxsize is prepostorously large - avoid int. overflow. */
return (0);
dst = alloca(maxsize * MB_CUR_MAX);
if (strftime(dst, maxsize, sformat, timeptr) == 0)
return (0);
state = initial;
n = mbsrtowcs(wcs, (const char **)&dst, maxsize, &state);
if (n == (size_t)-2 || n == (size_t)-1 || dst != NULL)
return (0);
return (n);
}