1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2011-2021 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library. If not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <mailutils/datetime.h>
22 #include <mailutils/stream.h>
23 #include <mailutils/errno.h>
24 #include <mailutils/cstr.h>
25
26 /* A locale-independent version of strftime */
27 size_t
mu_strftime(char * buf,size_t size,const char * format,struct tm * tm)28 mu_strftime (char *buf, size_t size, const char *format, struct tm *tm)
29 {
30 int rc;
31 mu_stream_t str;
32 mu_stream_stat_buffer stat;
33
34 if (mu_fixed_memory_stream_create (&str, buf, size, MU_STREAM_WRITE))
35 return 0;
36 mu_stream_set_stat (str, MU_STREAM_STAT_MASK (MU_STREAM_STAT_OUT), stat);
37 rc = mu_c_streamftime (str, format, tm, NULL);
38 if (rc == 0)
39 rc = mu_stream_write (str, "", 1, NULL);
40 mu_stream_unref (str);
41 return rc ? 0 : stat[MU_STREAM_STAT_OUT] - 1;
42 }
43
44
45