1 /* strftime - loadable builtin interface to strftime(3) */
3 /* See Makefile for compilation details. */
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
8 This file is part of GNU Bash.
9 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #if defined (HAVE_UNISTD_H)
29 #include "bashtypes.h"
30 #include "posixtime.h"
39 strftime_builtin (list
)
55 if (no_options (list
))
58 format
= list
->word
->word
;
59 if (format
== 0 || *format
== 0)
62 return (EXECUTION_SUCCESS
);
67 if (list
&& list
->word
->word
)
69 n
= legal_number (list
->word
->word
, &i
);
70 if (n
== 0 || i
< 0 || i
!= (time_t)i
)
72 sh_invalidnum (list
->word
->word
);
73 return (EXECUTION_FAILURE
);
81 t
= localtime (&secs
);
83 tbsize
= strlen (format
) * 4;
86 /* Now try to figure out how big the buffer should really be. strftime(3)
87 will return the number of bytes placed in the buffer unless it's greater
88 than MAXSIZE, in which case it returns 0. */
89 for (n
= 1; n
< 4; n
++)
91 tbuf
= xrealloc (tbuf
, tbsize
* n
);
92 tsize
= strftime (tbuf
, tbsize
* n
, format
, t
);
97 printf ("%s\n", tbuf
);
100 return (EXECUTION_SUCCESS
);
103 /* An array of strings forming the `long' documentation for a builtin xxx,
104 which is printed by `help xxx'. It must end with a NULL. */
105 char *strftime_doc
[] = {
106 "Display formatted time.",
108 "Converts date and time format to a string and displays it on the",
109 "standard output. If the optional second argument is supplied, it",
110 "is used as the number of seconds since the epoch to use in the",
111 "conversion, otherwise the current time is used.",
115 /* The standard structure describing a builtin command. bash keeps an array
116 of these structures. The flags must include BUILTIN_ENABLED so the
117 builtin can be used. */
118 struct builtin strftime_struct
= {
119 "strftime", /* builtin name */
120 strftime_builtin
, /* function implementing the builtin */
121 BUILTIN_ENABLED
, /* initial flags for builtin */
122 strftime_doc
, /* array of long documentation strings. */
123 "strftime format [seconds]", /* usage synopsis; becomes short_doc */
124 0 /* reserved for internal use */