Time and date handling routines. More...
#include "config.h"
#include <ctype.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "date.h"
#include "buffer.h"
#include "eqi.h"
#include "logging2.h"
#include "memory.h"
#include "prex.h"
#include "regex3.h"
#include "string2.h"
Go to the source code of this file.
Functions | |
static int | compute_tz (time_t g, struct tm *utc) |
Calculate the number of seconds east of UTC. | |
static time_t | add_tz_offset (time_t t, bool w, time_t h, time_t m) |
Compute and add a timezone offset to an UTC time. | |
static const struct Tz * | find_tz (const char *s, size_t len) |
Look up a timezone. | |
static int | is_leap_year_feb (struct tm *tm) |
Is a given February in a leap year. | |
int | mutt_date_local_tz (time_t t) |
Calculate the local timezone in seconds east of UTC. | |
time_t | mutt_date_make_time (struct tm *t, bool local) |
Convert struct tm to time_t | |
void | mutt_date_normalize_time (struct tm *tm) |
Fix the contents of a struct tm. | |
void | mutt_date_make_date (struct Buffer *buf, bool local) |
Write a date in RFC822 format to a buffer. | |
int | mutt_date_check_month (const char *s) |
Is the string a valid month name. | |
time_t | mutt_date_now (void) |
Return the number of seconds since the Unix epoch. | |
uint64_t | mutt_date_now_ms (void) |
Return the number of milliseconds since the Unix epoch. | |
void | mutt_time_now (struct timespec *tp) |
Set the provided time field to the current time. | |
static int | parse_small_uint (const char *str, const char *end, int *val) |
Parse a positive integer of at most 5 digits. | |
static time_t | mutt_date_parse_rfc5322_strict (const char *s, struct Tz *tz_out) |
Parse a date string in RFC822 format. | |
time_t | mutt_date_parse_date (const char *s, struct Tz *tz_out) |
Parse a date string in RFC822 format. | |
int | mutt_date_make_imap (struct Buffer *buf, time_t timestamp) |
Format date in IMAP style: DD-MMM-YYYY HH:MM:SS +ZZzz. | |
int | mutt_date_make_tls (char *buf, size_t buflen, time_t timestamp) |
Format date in TLS certificate verification style. | |
time_t | mutt_date_parse_imap (const char *s) |
Parse date of the form: DD-MMM-YYYY HH:MM:SS +ZZzz. | |
time_t | mutt_date_add_timeout (time_t now, time_t timeout) |
Safely add a timeout to a given time_t value. | |
struct tm | mutt_date_localtime (time_t t) |
Converts calendar time to a broken-down time structure expressed in user timezone. | |
struct tm | mutt_date_gmtime (time_t t) |
Converts calendar time to a broken-down time structure expressed in UTC timezone. | |
size_t | mutt_date_localtime_format (char *buf, size_t buflen, const char *format, time_t t) |
Format localtime. | |
size_t | mutt_date_localtime_format_locale (char *buf, size_t buflen, const char *format, time_t t, locale_t loc) |
Format localtime using a given locale. | |
void | mutt_date_sleep_ms (size_t ms) |
Sleep for milliseconds. | |
Variables | |
static const char *const | Weekdays [] |
Day of the week (abbreviated) | |
static const char *const | Months [] |
Months of the year (abbreviated) | |
static const struct Tz | TimeZones [] |
Lookup table of Time Zones. | |
Time and date handling routines.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Definition in file date.c.
|
static |
Calculate the number of seconds east of UTC.
g | Local time |
utc | UTC time |
num | Seconds east of UTC |
returns the seconds east of UTC given 'g' and its corresponding gmtime() representation
Definition at line 140 of file date.c.
|
static |
Compute and add a timezone offset to an UTC time.
t | UTC time |
w | True if west of UTC, false if east |
h | Number of hours in the timezone |
m | Number of minutes in the timezone |
num | Timezone offset in seconds |
Definition at line 172 of file date.c.
|
static |
Look up a timezone.
s | Timezone to lookup |
len | Length of the s string |
ptr | Pointer to the Tz struct |
NULL | Not found |
Definition at line 187 of file date.c.
|
static |
Is a given February in a leap year.
tm | Date to be tested |
true | It's a leap year |
Definition at line 202 of file date.c.
int mutt_date_local_tz | ( | time_t | t | ) |
Calculate the local timezone in seconds east of UTC.
t | Time to examine |
num | Seconds east of UTC |
Returns the local timezone in seconds east of UTC for the time t, or for the current time if t is zero.
Definition at line 219 of file date.c.
time_t mutt_date_make_time | ( | struct tm * | t, |
bool | local | ||
) |
Convert struct tm
to time_t
t | Time to convert |
local | Should the local timezone be considered |
num | Time in Unix format |
TIME_T_MIN | Error |
Convert a struct tm to time_t, but don't take the local timezone into account unless "local" is nonzero
Definition at line 242 of file date.c.
void mutt_date_normalize_time | ( | struct tm * | tm | ) |
Fix the contents of a struct tm.
tm | Time to correct |
If values have been added/subtracted from a struct tm, it can lead to invalid dates, e.g. Adding 10 days to the 25th of a month.
This function will correct any over/under-flow.
Definition at line 310 of file date.c.
void mutt_date_make_date | ( | struct Buffer * | buf, |
bool | local | ||
) |
Write a date in RFC822 format to a buffer.
buf | Buffer for result |
local | If true, use the local timezone. Otherwise use UTC. |
Appends the date to the passed in buffer. The buffer is not cleared because some callers prepend quotes.
Definition at line 397 of file date.c.
int mutt_date_check_month | ( | const char * | s | ) |
Is the string a valid month name.
s | String to check (must be at least 3 bytes long) |
num | Index into Months array (0-based) |
-1 | Error |
Definition at line 432 of file date.c.
time_t mutt_date_now | ( | void | ) |
Return the number of seconds since the Unix epoch.
num | Number of seconds since the Unix epoch, or 0 on failure |
Definition at line 456 of file date.c.
uint64_t mutt_date_now_ms | ( | void | ) |
Return the number of milliseconds since the Unix epoch.
num | The number of ms since the Unix epoch, or 0 on failure |
Definition at line 465 of file date.c.
void mutt_time_now | ( | struct timespec * | tp | ) |
Set the provided time field to the current time.
[out] | tp | Field to set |
Uses nanosecond precision if available, if not we fallback to microseconds.
Definition at line 480 of file date.c.
|
static |
Parse a positive integer of at most 5 digits.
[in] | str | String to parse |
[in] | end | End of the string |
[out] | val | Value |
num | Number of chars parsed |
Leaves val untouched if the given string does not start with an integer; ignores junk after it or any digits beyond the first five (this is so that the function can never overflow, yet check if the integer is larger than the maximum 4 digits supported in a year). and does not support negative numbers. Empty strings are parsed as zero.
Definition at line 507 of file date.c.
|
static |
Parse a date string in RFC822 format.
[in] | s | String to parse |
[out] | tz_out | Timezone info (OPTIONAL) |
num | Unix time in seconds |
Parse a date string in RFC822 format, without any comments or extra whitespace (except a comment at the very end, since that is very common for time zones).
This is a fairly straightforward implementation in the hope of extracting the valid cases quickly, i.e., without having to resort to a regex. The hard cases are left to a regex implementation further down in mutt_date_parse_date() (which calls us).
Spec: https://tools.ietf.org/html/rfc5322#section-3.3
Definition at line 537 of file date.c.
time_t mutt_date_parse_date | ( | const char * | s, |
struct Tz * | tz_out | ||
) |
Parse a date string in RFC822 format.
[in] | s | String to parse |
[out] | tz_out | Pointer to timezone (optional) |
num | Unix time in seconds, or -1 on failure |
Parse a date of the form: [ weekday , ] day-of-month month year hour:minute:second [ timezone ]
The 'timezone' field is optional; it defaults to +0000 if missing.
Definition at line 716 of file date.c.
int mutt_date_make_imap | ( | struct Buffer * | buf, |
time_t | timestamp | ||
) |
Format date in IMAP style: DD-MMM-YYYY HH:MM:SS +ZZzz.
buf | Buffer to store the results |
timestamp | Time to format |
num | Characters written to buf |
Definition at line 811 of file date.c.
int mutt_date_make_tls | ( | char * | buf, |
size_t | buflen, | ||
time_t | timestamp | ||
) |
Format date in TLS certificate verification style.
buf | Buffer to store the results |
buflen | Length of buffer |
timestamp | Time to format |
num | Characters written to buf |
e.g., Mar 17 16:40:46 2016 UTC. The time is always in UTC.
Caller should provide a buffer of at least 27 bytes.
Definition at line 837 of file date.c.
time_t mutt_date_parse_imap | ( | const char * | s | ) |
Parse date of the form: DD-MMM-YYYY HH:MM:SS +ZZzz.
s | Date in string form |
num | Unix time |
0 | Error |
Definition at line 854 of file date.c.
time_t mutt_date_add_timeout | ( | time_t | now, |
time_t | timeout | ||
) |
Safely add a timeout to a given time_t value.
now | Time now |
timeout | Timeout in seconds |
num | Unix time to timeout |
This will truncate instead of overflowing.
Definition at line 890 of file date.c.
struct tm mutt_date_localtime | ( | time_t | t | ) |
Converts calendar time to a broken-down time structure expressed in user timezone.
t | Time |
obj | Broken-down time representation |
Definition at line 906 of file date.c.
struct tm mutt_date_gmtime | ( | time_t | t | ) |
Converts calendar time to a broken-down time structure expressed in UTC timezone.
t | Time |
obj | Broken-down time representation |
Definition at line 927 of file date.c.
size_t mutt_date_localtime_format | ( | char * | buf, |
size_t | buflen, | ||
const char * | format, | ||
time_t | t | ||
) |
Format localtime.
buf | Buffer to store formatted time |
buflen | Buffer size |
format | Format to apply |
t | Time to format |
num | Number of Bytes added to buffer, excluding NUL byte |
Definition at line 951 of file date.c.
size_t mutt_date_localtime_format_locale | ( | char * | buf, |
size_t | buflen, | ||
const char * | format, | ||
time_t | t, | ||
locale_t | loc | ||
) |
Format localtime using a given locale.
buf | Buffer to store formatted time |
buflen | Buffer size |
format | Format to apply |
t | Time to format |
loc | Locale to use |
num | Number of Bytes added to buffer, excluding NUL byte |
Definition at line 969 of file date.c.
void mutt_date_sleep_ms | ( | size_t | ms | ) |
|
static |
|
static |