Commit 5af1d3a7 5af1d3a7bd8149139ad59baa11501e3d11b138a7 by Sergey Poznyakoff

Move date/time declarations into a separate header: mailutils/datetime.h

Additionally, do not typedef mu_timezone, leave it in struct namespace.
1 parent 6301266f
......@@ -29,6 +29,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <termios.h>
......
......@@ -20,7 +20,7 @@
struct _temp_envelope
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
char *sender;
};
......
......@@ -959,7 +959,7 @@ _frt_internaldate (struct fetch_function_closure *ffc,
const char *date;
mu_envelope_t env = NULL;
struct tm tm, *tmp = NULL;
mu_timezone tz;
struct mu_timezone tz;
mu_message_get_envelope (frt->msg, &env);
if (mu_envelope_sget_date (env, &date) == 0
......
......@@ -336,7 +336,7 @@ add2set (size_t ** set, int *n, unsigned long val)
}
static void
adjust_tm (struct tm *tm, mu_timezone *tz, enum datetime_parse_mode flag)
adjust_tm (struct tm *tm, struct mu_timezone *tz, enum datetime_parse_mode flag)
{
switch (flag)
{
......@@ -371,7 +371,7 @@ util_parse_internal_date (char *date, time_t *timep,
enum datetime_parse_mode flag)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
time_t time;
if (mu_scan_datetime (date, MU_DATETIME_INTERNALDATE, &tm, &tz, NULL))
......@@ -392,7 +392,7 @@ util_parse_822_date (const char *date, time_t *timep,
enum datetime_parse_mode flag)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
const char *p = date;
if (mu_parse822_date_time (&p, date + strlen (date), &tm, &tz) == 0)
......@@ -409,7 +409,7 @@ util_parse_ctime_date (const char *date, time_t *timep,
enum datetime_parse_mode flag)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (mu_scan_datetime (date, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
{
......
......@@ -38,6 +38,7 @@ pkginclude_HEADERS = \
cfg.h\
cidr.h\
cstr.h\
datetime.h\
daemon.h\
dbm.h\
debug.h\
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifndef _MAILUTILS_DATETIME_H
#define _MAILUTILS_DATETIME_H
#include <time.h>
#include <mailutils/types.h>
/* ----------------------- */
/* Date & time functions */
/* ----------------------- */
struct mu_timezone
{
int utc_offset; /* Seconds east of UTC. */
const char *tz_name;
/* Nickname for this timezone, if known. It is always considered
to be a pointer to static string, so will never be freed. */
};
int mu_parse_date (const char *p, time_t *rettime, const time_t *now);
time_t mu_utc_offset (void);
time_t mu_tm2time (struct tm *timeptr, struct mu_timezone *tz);
size_t mu_strftime (char *s, size_t max, const char *format, struct tm *tm);
int mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *tm,
struct mu_timezone *tz);
int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm,
struct mu_timezone *tz, char **endp);
/* Common datetime formats: */
#define MU_DATETIME_FROM "%a %b %e %H:%M:%S %Y"
/* Length of an envelope date in C locale,
not counting the terminating nul character */
#define MU_DATETIME_FROM_LENGTH 24
#define MU_DATETIME_IMAP "%d-%b-%Y %H:%M:%S %z"
#define MU_DATETIME_INTERNALDATE "%d-%b-%Y%$ %H:%M:%S %z"
/* RFC2822 date. Scan format contains considerable allowances which would
stun formatting functions, therefore two distinct formats are provided:
one for outputting and one for scanning: */
#define MU_DATETIME_FORM_RFC822 "%a, %e %b %Y %H:%M:%S %z"
#define MU_DATETIME_SCAN_RFC822 "%[%a, %]%e %b %Y %H:%M%[:%S%] %z"
#endif
......@@ -51,12 +51,6 @@ int mu_envelope_set_destroy (mu_envelope_t envelope,
int (*_destroy) (mu_envelope_t),
void *owner);
/* mu_strftime format for envelope dates */
#define MU_ENVELOPE_DATE_FORMAT "%a %b %d %H:%M:%S %Y"
/* Length of an envelope date in C locale,
not counting the terminating nul character */
#define MU_ENVELOPE_DATE_LENGTH 24
#ifdef __cplusplus
}
#endif
......
......@@ -21,7 +21,7 @@
#include <mailutils/iterator.h>
#include <mailutils/debug.h>
#include <mailutils/stream.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#include <mailutils/kwd.h>
#ifdef __cplusplus
......
......@@ -22,7 +22,7 @@ extern "C" {
#endif
# include <mailutils/types.h>
# include <mailutils/util.h>
# include <mailutils/datetime.h>
# include <time.h>
#define MU_IMAPIO_CLIENT 0
......
......@@ -25,6 +25,7 @@
#include <mailutils/attribute.h>
#include <mailutils/auth.h>
#include <mailutils/body.h>
#include <mailutils/datetime.h>
#include <mailutils/debug.h>
#include <mailutils/envelope.h>
#include <mailutils/errno.h>
......
......@@ -24,7 +24,7 @@
#define _MAILUTILS_PARSE822_H
#include <mailutils/types.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#ifdef __cplusplus
extern "C" {
......@@ -131,7 +131,7 @@ extern int mu_parse822_date (const char **p, const char *e, int *day,
extern int mu_parse822_time (const char **p, const char *e, int *h,
int *m, int *s, int *tz, const char **tz_name);
extern int mu_parse822_date_time (const char **p, const char *e,
struct tm *tm, mu_timezone *tz);
struct tm *tm, struct mu_timezone *tz);
#ifdef __cplusplus
......
......@@ -21,8 +21,6 @@
/* A collection of utility routines that don't belong somewhere else. */
#include <time.h>
#include <mailutils/list.h>
#include <mailutils/types.h>
......@@ -47,42 +45,6 @@ void mu_str_url_decode_inline (char *str);
int mu_str_url_decode (char **ptr, const char *s);
/* ----------------------- */
/* Date & time functions */
/* ----------------------- */
struct mu_timezone
{
int utc_offset; /* Seconds east of UTC. */
const char *tz_name;
/* Nickname for this timezone, if known. It is always considered
to be a pointer to static string, so will never be freed. */
};
typedef struct mu_timezone mu_timezone;
int mu_parse_date (const char *p, time_t *rettime, const time_t *now);
time_t mu_utc_offset (void);
time_t mu_tm2time (struct tm *timeptr, mu_timezone *tz);
size_t mu_strftime (char *s, size_t max, const char *format, struct tm *tm);
int mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *tm,
struct mu_timezone *tz);
int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm,
struct mu_timezone *tz, char **endp);
/* Common datetime formats: */
#define MU_DATETIME_FROM "%a %b %e %H:%M:%S %Y"
#define MU_DATETIME_IMAP "%d-%b-%Y %H:%M:%S %z"
#define MU_DATETIME_INTERNALDATE "%d-%b-%Y%$ %H:%M:%S %z"
/* RFC2822 date. Scan format contains considerable allowances which would
stun formatting functions, therefore two distinct formats are provided:
one for outputting and one for scanning: */
#define MU_DATETIME_FORM_RFC822 "%a, %e %b %Y %H:%M:%S %z"
#define MU_DATETIME_SCAN_RFC822 "%[%a, %]%e %b %Y %H:%M%[:%S%] %z"
/* ----------------------- */
/* File & path names. */
/* ----------------------- */
char *mu_get_homedir (void);
......
......@@ -63,6 +63,7 @@
#include <mailutils/locker.h>
#include <mailutils/message.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#include <mailutils/property.h>
#include <mailutils/stream.h>
#include <mailutils/url.h>
......
......@@ -23,6 +23,7 @@
#include <stdlib.h>
#include <string.h>
#include <mailutils/diag.h>
#include <mailutils/datetime.h>
#include <mailutils/util.h>
#include <mailutils/stream.h>
#include <mailutils/errno.h>
......@@ -123,7 +124,7 @@ ISO_8601_weekdays (int yday, int wday)
/* Convert struct tm into time_t, taking into account timezone offset. */
/* FIXME: It does not take DST into account */
time_t
mu_tm2time (struct tm *tm, mu_timezone *tz)
mu_tm2time (struct tm *tm, struct mu_timezone *tz)
{
time_t t;
int day;
......
......@@ -28,6 +28,7 @@
#include <mailutils/cctype.h>
#include <mailutils/message.h>
#include <mailutils/header.h>
#include <mailutils/datetime.h>
#include <mailutils/util.h>
#include <mailutils/io.h>
#include <mailutils/envelope.h>
......
......@@ -18,7 +18,7 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#include <mailutils/stream.h>
#include <mailutils/errno.h>
#include <mailutils/cstr.h>
......
......@@ -19,7 +19,7 @@
#include <mailutils/stream.h>
#include <mailutils/errno.h>
#include <mailutils/sys/imapio.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
int
mu_imapio_send_time (struct _mu_imapio *io, struct tm *tm,
......
......@@ -28,6 +28,7 @@
#include <mailutils/errno.h>
#include <mailutils/envelope.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#include <mailutils/header.h>
#include <mailutils/mu_auth.h>
#include <mailutils/address.h>
......@@ -49,14 +50,14 @@ message_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
if (buf == NULL || len == 0)
{
n = MU_ENVELOPE_DATE_LENGTH;
n = MU_DATETIME_FROM_LENGTH;
}
else
{
char tmpbuf[MU_ENVELOPE_DATE_LENGTH+1];
char tmpbuf[MU_DATETIME_FROM_LENGTH+1];
t = time (NULL);
n = mu_strftime (tmpbuf, sizeof tmpbuf,
MU_ENVELOPE_DATE_FORMAT, localtime (&t));
MU_DATETIME_FROM, localtime (&t));
n = mu_cpystr (buf, tmpbuf, len);
}
if (pnwrite)
......
......@@ -36,6 +36,7 @@
#include <mailutils/body.h>
#include <mailutils/stream.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/cctype.h>
......@@ -291,11 +292,11 @@ _message_open (mu_stream_t stream)
{
struct tm *tm;
time_t t;
char date[80]; /* FIXME: This size is way too big */
char date[MU_DATETIME_FROM_LENGTH+1];
time(&t);
tm = gmtime(&t);
mu_strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y", tm);
time (&t);
tm = gmtime (&t);
mu_strftime (date, sizeof (date), MU_DATETIME_FROM, tm);
env_date = strdup (date);
}
......
......@@ -23,7 +23,7 @@
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#include <mailutils/stream.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
......
......@@ -23,7 +23,7 @@
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#include <mailutils/stream.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
......
......@@ -79,7 +79,7 @@ mu_scm_message_print (SCM message_smob, SCM port, scm_print_state * pstate)
const char *p;
size_t m_size = 0, m_lines = 0;
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
char datebuf[sizeof ("Mon Jan 01 00:00")]; /* Warning: length must be > 9 */
mu_message_get_envelope (mum->msg, &env);
......@@ -404,7 +404,7 @@ SCM_DEFINE_PUBLIC (scm_mu_message_get_envelope_date, "mu-message-get-envelope-da
int status;
const char *sdate;
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
SCM_ASSERT (mu_scm_is_message (mesg), mesg, SCM_ARG1, FUNC_NAME);
msg = mu_scm_message_get (mesg);
......
......@@ -38,6 +38,7 @@
#include <mailutils/address.h>
#include <mailutils/registrar.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#include <mailutils/stream.h>
#include <mailutils/debug.h>
#include <mailutils/attribute.h>
......
......@@ -188,7 +188,7 @@ mime_create_ds (mu_mime_t mime, mu_message_t orig)
char datestr[80];
time_t t = time (NULL);
struct tm tm, *tmp;
mu_timezone tz;
struct mu_timezone tz;
mu_envelope_t env;
const char *p;
......
......@@ -29,6 +29,7 @@
#include <mailutils/io.h>
#include <mailutils/filter.h>
#include <mailutils/cctype.h>
#include <mailutils/datetime.h>
#define ATTRIBUTE_IS_DELETED(flag) (flag & MU_ATTRIBUTE_DELETED)
#define ATTRIBUTE_IS_EQUAL(flag1, flag2) (flag1 == flag2)
......@@ -859,22 +860,23 @@ restore_date (mu_message_t msg, char **pret)
mu_header_t hdr;
const char *s;
char *date = NULL;
time_t t;
struct tm tm;
struct mu_timezone tz;
if (mu_message_get_header (msg, &hdr) == 0)
mu_header_sget_value (hdr, MU_HEADER_DATE, &s);
if (s && mu_parse_date (s, &t, NULL))
if (s && mu_scan_datetime (s, MU_DATETIME_SCAN_RFC822, &tm, &tz, NULL) == 0)
{
char datebuf[MU_ENVELOPE_DATE_LENGTH+1];
char datebuf[MU_DATETIME_FROM_LENGTH+1];
/* FIXME: 1. Preserve TZ info */
mu_strftime (datebuf, sizeof datebuf, MU_ENVELOPE_DATE_FORMAT,
localtime (&t));
/* FIXME: Compensate for TZ differences. */
mu_strftime (datebuf, sizeof datebuf, MU_DATETIME_FROM, &tm);
date = strdup (datebuf);
}
else
{
time_t t;
time (&t);
date = strdup (ctime (&t));
}
......
......@@ -208,7 +208,7 @@ hdr_date (struct header_call_args *args, void *data)
{
const char *p;
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
mu_envelope_t env;
mu_message_get_envelope (args->msg, &env);
......
......@@ -71,6 +71,7 @@
#include <mailutils/mailer.h>
#include <mailutils/message.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
#include <mailutils/registrar.h>
#include <mailutils/stream.h>
#include <mailutils/url.h>
......
......@@ -59,6 +59,7 @@
#include <mailutils/prog.h>
#include <mailutils/mh.h>
#include <mailutils/stdstream.h>
#include <mailutils/datetime.h>
#include <mu_umaxtostr.h>
......
......@@ -1160,7 +1160,7 @@ builtin_putnumf (struct mh_machine *mach)
}
static int
_parse_date (struct mh_machine *mach, struct tm *tm, mu_timezone *tz)
_parse_date (struct mh_machine *mach, struct tm *tm, struct mu_timezone *tz)
{
char *date = strobj_ptr (&mach->arg_str);
const char *p = date;
......@@ -1183,7 +1183,7 @@ static void
builtin_sec (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1196,7 +1196,7 @@ static void
builtin_min (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1209,7 +1209,7 @@ static void
builtin_hour (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1222,7 +1222,7 @@ static void
builtin_wday (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1235,7 +1235,7 @@ static void
builtin_day (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
char buf[80];
if (_parse_date (mach, &tm, &tz))
......@@ -1251,7 +1251,7 @@ static void
builtin_weekday (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
char buf[80];
if (_parse_date (mach, &tm, &tz))
......@@ -1268,7 +1268,7 @@ static void
builtin_sday (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
/*FIXME: more elaborate check needed */
if (_parse_date (mach, &tm, &tz))
......@@ -1282,7 +1282,7 @@ static void
builtin_mday (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1295,7 +1295,7 @@ static void
builtin_yday (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1308,7 +1308,7 @@ static void
builtin_mon (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1321,7 +1321,7 @@ static void
builtin_month (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
char buf[80];
if (_parse_date (mach, &tm, &tz))
......@@ -1337,7 +1337,7 @@ static void
builtin_lmonth (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
char buf[80];
if (_parse_date (mach, &tm, &tz))
......@@ -1353,7 +1353,7 @@ static void
builtin_year (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1366,7 +1366,7 @@ static void
builtin_zone (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1379,7 +1379,7 @@ static void
builtin_tzone (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1410,7 +1410,7 @@ static void
builtin_szone (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
/*FIXME: more elaborate check needed */
if (_parse_date (mach, &tm, &tz))
......@@ -1438,7 +1438,7 @@ static void
builtin_dst (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1454,7 +1454,7 @@ static void
builtin_clock (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
if (_parse_date (mach, &tm, &tz))
return;
......@@ -1466,7 +1466,7 @@ void
builtin_rclock (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
time_t now = time (NULL);
if (_parse_date (mach, &tm, &tz))
......@@ -1493,7 +1493,7 @@ static void
date_cvt (struct mh_machine *mach, int pretty)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
char buf[80];
int i, len;
const char *tzname = NULL;
......@@ -1567,7 +1567,7 @@ static void
builtin_nodate (struct mh_machine *mach)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
mach->arg_num = _parse_date (mach, &tm, &tz);
}
......
......@@ -336,7 +336,7 @@ static int
_parse_822_date (char *date, time_t * timep)
{
struct tm tm;
mu_timezone tz;
struct mu_timezone tz;
const char *p = date;
if (mu_parse822_date_time (&p, date + strlen (date), &tm, &tz) == 0)
......
......@@ -179,13 +179,13 @@ print_unix_header (mu_message_t message)
if (mu_envelope_sget_date (envelope, &buf))
{
char datebuf[MU_ENVELOPE_DATE_LENGTH+1];
char datebuf[MU_DATETIME_FROM_LENGTH+1];
time_t t;
struct tm *tm;
t = time (NULL);
tm = localtime (&t);
mu_strftime (datebuf, sizeof datebuf, "%a %b %d %H:%M:%S %Y", tm);
mu_strftime (datebuf, sizeof datebuf, MU_DATETIME_FROM, tm);
buf = datebuf;
}
......
......@@ -50,7 +50,7 @@
#include <mailutils/error.h>
#include <mailutils/envelope.h>
#include <mailutils/wordsplit.h>
#include <mailutils/util.h>
#include <mailutils/datetime.h>
int msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int *n);
......