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 #ifndef _MAILUTILS_DATETIME_H
19 #define _MAILUTILS_DATETIME_H
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #include <time.h>
26 #include <mailutils/types.h>
27 
28   /* ----------------------- */
29   /* Date & time functions   */
30   /* ----------------------- */
31 
32 /* Argument ranges:
33 
34    year != 0, AD if > 0, BC if < 0
35    1 <= month <= 12
36    1 <= day <= maxday(month)
37 */
38 /* Compute Julian Day for the given date */
39 int mu_datetime_julianday (int year, int month, int day);
40 /* Compute day of week (Sunday - 0) */
41 int mu_datetime_dayofweek (int year, int month, int day);
42 /* Compute ordinal date (1-based) */
43 int mu_datetime_dayofyear (int year, int month, int day);
44 /* Return number of days in the year */
45 int mu_datetime_year_days (int year);
46 
47 /* Day of week and month names in C locale */
48 extern const char *_mu_datetime_short_month[];
49 extern const char *_mu_datetime_full_month[];
50 extern const char *_mu_datetime_short_wday[];
51 extern const char *_mu_datetime_full_wday[];
52 
53 
54 struct mu_timezone
55 {
56   int utc_offset;  /* Seconds east of UTC. */
57 
58   const char *tz_name;
59     /* Nickname for this timezone, if known. It is always considered
60        to be a pointer to static string, so will never be freed. */
61 };
62 
63 #define MU_PD_MASK_SECOND   00001
64 #define MU_PD_MASK_MINUTE   00002
65 #define MU_PD_MASK_HOUR     00004
66 #define MU_PD_MASK_DAY      00010
67 #define MU_PD_MASK_MONTH    00020
68 #define MU_PD_MASK_YEAR     00040
69 #define MU_PD_MASK_TZ       00100
70 #define MU_PD_MASK_MERIDIAN 00200
71 #define MU_PD_MASK_ORDINAL  00400
72 #define MU_PD_MASK_NUMBER   01000
73 
74 #define MU_PD_MASK_TIME MU_PD_MASK_SECOND|MU_PD_MASK_MINUTE|MU_PD_MASK_HOUR
75 #define MU_PD_MASK_DATE MU_PD_MASK_DAY|MU_PD_MASK_MONTH|MU_PD_MASK_YEAR
76 #define MU_PD_MASK_DOW MU_PD_MASK_NUMBER
77 
78 int mu_parse_date_dtl (const char *p, const time_t *now,
79 		       time_t *rettime, struct tm *rettm,
80 		       struct mu_timezone *rettz,
81 		       int *retflags);
82 int mu_parse_date (const char *p, time_t *rettime, const time_t *now);
83 
84 int mu_utc_offset (void);
85 int mu_timezone_offset (const char *buf, int *off);
86 void mu_datetime_tz_local (struct mu_timezone *tz);
87 void mu_datetime_tz_utc (struct mu_timezone *tz);
88 time_t mu_datetime_to_utc (struct tm *timeptr, struct mu_timezone *tz);
89 size_t mu_strftime (char *s, size_t max, const char *format, struct tm *tm);
90 
91 int mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *tm,
92 		      struct mu_timezone *tz);
93 int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm,
94 		      struct mu_timezone *tz, char **endp);
95 
96   /* Common datetime formats: */
97 #define MU_DATETIME_FROM         "%a %b %e %H:%M:%S %Y"
98 /* Length of an envelope date in C locale,
99    not counting the terminating nul character */
100 #define MU_DATETIME_FROM_LENGTH 24
101 
102 #define MU_DATETIME_IMAP         "%d-%b-%Y %H:%M:%S %z"
103 #define MU_DATETIME_INTERNALDATE "%e-%b-%Y%$ %H:%M:%S %z"
104 
105   /* RFC2822 date.  Scan format contains considerable allowances which would
106      stun formatting functions, therefore two distinct formats are provided:
107      one for outputting and one for scanning: */
108 #define MU_DATETIME_FORM_RFC822  "%a, %e %b %Y %H:%M:%S %z"
109 #define MU_DATETIME_SCAN_RFC822  "%[%a, %]%e %b %Y %H:%M%[:%S%] %z"
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif
116