NeoMutt  2024-02-01-35-geee02f
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
date.h File Reference

Time and date handling routines. More...

#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
+ Include dependency graph for date.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Tz
 List of recognised Timezones. More...
 

Macros

#define TIME_T_MAX   ((((time_t) 1 << (sizeof(time_t) * 8 - 2)) - 1) * 2 + 1)
 
#define TIME_T_MIN   (-TIME_T_MAX - 1)
 
#define TM_YEAR_MAX    (1970 + (((((TIME_T_MAX - 59) / 60) - 59) / 60) - 23) / 24 / 366)
 
#define TM_YEAR_MIN   (1970 - (TM_YEAR_MAX - 1970) - 1)
 

Functions

time_t mutt_date_add_timeout (time_t now, time_t timeout)
 Safely add a timeout to a given time_t value.
 
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.
 
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.
 
struct tm mutt_date_localtime (time_t t)
 Converts calendar time to a broken-down time structure expressed in user timezone.
 
int mutt_date_local_tz (time_t t)
 Calculate the local timezone in seconds east of UTC.
 
void mutt_date_make_date (struct Buffer *buf, bool local)
 Write a date in RFC822 format to a buffer.
 
int mutt_date_make_imap (char *buf, size_t buflen, time_t timestamp)
 Format date in IMAP style: DD-MMM-YYYY HH:MM:SS +ZZzz.
 
time_t mutt_date_make_time (struct tm *t, bool local)
 Convert struct tm to time_t
 
int mutt_date_make_tls (char *buf, size_t buflen, time_t timestamp)
 Format date in TLS certificate verification style.
 
void mutt_date_normalize_time (struct tm *tm)
 Fix the contents of a struct tm.
 
time_t mutt_date_parse_date (const char *s, struct Tz *tz_out)
 Parse a date string in RFC822 format.
 
time_t mutt_date_parse_imap (const char *s)
 Parse date of the form: DD-MMM-YYYY HH:MM:SS +ZZzz.
 
void mutt_date_sleep_ms (size_t ms)
 Sleep for milliseconds.
 
void mutt_time_now (struct timespec *tp)
 Set the provided time field to the current time.
 

Detailed Description

Time and date handling routines.

Authors
  • Richard Russon
  • Victor Fernandes
  • Pietro Cerutti

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.h.

Macro Definition Documentation

◆ TIME_T_MAX

#define TIME_T_MAX   ((((time_t) 1 << (sizeof(time_t) * 8 - 2)) - 1) * 2 + 1)

Definition at line 40 of file date.h.

◆ TIME_T_MIN

#define TIME_T_MIN   (-TIME_T_MAX - 1)

Definition at line 41 of file date.h.

◆ TM_YEAR_MAX

#define TM_YEAR_MAX    (1970 + (((((TIME_T_MAX - 59) / 60) - 59) / 60) - 23) / 24 / 366)

Definition at line 42 of file date.h.

◆ TM_YEAR_MIN

#define TM_YEAR_MIN   (1970 - (TM_YEAR_MAX - 1970) - 1)

Definition at line 44 of file date.h.

Function Documentation

◆ mutt_date_add_timeout()

time_t mutt_date_add_timeout ( time_t  now,
time_t  timeout 
)

Safely add a timeout to a given time_t value.

Parameters
nowTime now
timeoutTimeout in seconds
Return values
numUnix time to timeout

This will truncate instead of overflowing.

Definition at line 892 of file date.c.

893{
894 if (timeout < 0)
895 return now;
896
897 if ((TIME_T_MAX - now) < timeout)
898 return TIME_T_MAX;
899
900 return now + timeout;
901}
#define TIME_T_MAX
Definition: date.h:40
+ Here is the caller graph for this function:

◆ mutt_date_check_month()

int mutt_date_check_month ( const char *  s)

Is the string a valid month name.

Parameters
sString to check (must be at least 3 bytes long)
Return values
numIndex into Months array (0-based)
-1Error
Note
Only the first three characters are checked
The comparison is case insensitive

Definition at line 431 of file date.c.

432{
433 if (!s)
434 return -1;
435
436 char buf[4] = { 0 };
437 memcpy(buf, s, 3);
438 uint32_t sv;
439 memcpy(&sv, buf, sizeof(sv));
440 for (int i = 0; i < mutt_array_size(Months); i++)
441 {
442 uint32_t mv;
443 memcpy(&mv, Months[i], sizeof(mv));
444 if (sv == mv)
445 return i;
446 }
447
448 return -1; /* error */
449}
#define mutt_array_size(x)
Definition: memory.h:38
static const char *const Months[]
Months of the year (abbreviated)
Definition: date.c:66
+ Here is the caller graph for this function:

◆ mutt_date_now()

time_t mutt_date_now ( void  )

Return the number of seconds since the Unix epoch.

Return values
numNumber of seconds since the Unix epoch, or 0 on failure

Definition at line 455 of file date.c.

456{
457 return mutt_date_now_ms() / 1000;
458}
uint64_t mutt_date_now_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:464
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_now_ms()

uint64_t mutt_date_now_ms ( void  )

Return the number of milliseconds since the Unix epoch.

Return values
numThe number of ms since the Unix epoch, or 0 on failure

Definition at line 464 of file date.c.

465{
466 struct timeval tv = { 0, 0 };
467 gettimeofday(&tv, NULL);
468 /* We assume that gettimeofday doesn't modify its first argument on failure.
469 * We also kind of assume that gettimeofday does not fail. */
470 return ((uint64_t) tv.tv_sec * 1000) + (tv.tv_usec / 1000);
471}
+ Here is the caller graph for this function:

◆ mutt_date_gmtime()

struct tm mutt_date_gmtime ( time_t  t)

Converts calendar time to a broken-down time structure expressed in UTC timezone.

Parameters
tTime
Return values
objBroken-down time representation

Definition at line 929 of file date.c.

930{
931 struct tm tm = { 0 };
932
933 struct tm *ret = gmtime_r(&t, &tm);
934 if (!ret)
935 {
936 mutt_debug(LL_DEBUG1, "Could not convert time_t via gmtime_r() to struct tm: time_t = %jd\n",
937 (intmax_t) t);
938 struct tm default_tm = { 0 }; // 1970-01-01 00:00:00
939 mktime(&default_tm); // update derived fields making tm into a valid tm.
940 tm = default_tm;
941 }
942 return tm;
943}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
+ Here is the caller graph for this function:

◆ mutt_date_localtime_format()

size_t mutt_date_localtime_format ( char *  buf,
size_t  buflen,
const char *  format,
time_t  t 
)

Format localtime.

Parameters
bufBuffer to store formatted time
buflenBuffer size
formatFormat to apply
tTime to format
Return values
numNumber of Bytes added to buffer, excluding NUL byte

Definition at line 953 of file date.c.

954{
955 if (!buf || !format)
956 return 0;
957
958 struct tm tm = mutt_date_localtime(t);
959 return strftime(buf, buflen, format, &tm);
960}
struct tm mutt_date_localtime(time_t t)
Converts calendar time to a broken-down time structure expressed in user timezone.
Definition: date.c:908
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_localtime_format_locale()

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.

Parameters
bufBuffer to store formatted time
buflenBuffer size
formatFormat to apply
tTime to format
locLocale to use
Return values
numNumber of Bytes added to buffer, excluding NUL byte

Definition at line 971 of file date.c.

973{
974 if (!buf || !format)
975 return 0;
976
977 struct tm tm = mutt_date_localtime(t);
978 return strftime_l(buf, buflen, format, &tm, loc);
979}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_localtime()

struct tm mutt_date_localtime ( time_t  t)

Converts calendar time to a broken-down time structure expressed in user timezone.

Parameters
tTime
Return values
objBroken-down time representation

Definition at line 908 of file date.c.

909{
910 struct tm tm = { 0 };
911
912 struct tm *ret = localtime_r(&t, &tm);
913 if (!ret)
914 {
915 mutt_debug(LL_DEBUG1, "Could not convert time_t via localtime_r() to struct tm: time_t = %jd\n",
916 (intmax_t) t);
917 struct tm default_tm = { 0 }; // 1970-01-01 00:00:00
918 mktime(&default_tm); // update derived fields making tm into a valid tm.
919 tm = default_tm;
920 }
921 return tm;
922}
+ Here is the caller graph for this function:

◆ mutt_date_local_tz()

int mutt_date_local_tz ( time_t  t)

Calculate the local timezone in seconds east of UTC.

Parameters
tTime to examine
Return values
numSeconds 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 218 of file date.c.

219{
220 /* Check we haven't overflowed the time (on 32-bit arches) */
221 if ((t == TIME_T_MAX) || (t == TIME_T_MIN))
222 return 0;
223
224 if (t == 0)
225 t = mutt_date_now();
226
227 struct tm tm = mutt_date_gmtime(t);
228 return compute_tz(t, &tm);
229}
#define TIME_T_MIN
Definition: date.h:41
static int compute_tz(time_t g, struct tm *utc)
Calculate the number of seconds east of UTC.
Definition: date.c:139
struct tm mutt_date_gmtime(time_t t)
Converts calendar time to a broken-down time structure expressed in UTC timezone.
Definition: date.c:929
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:455
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_make_date()

void mutt_date_make_date ( struct Buffer buf,
bool  local 
)

Write a date in RFC822 format to a buffer.

Parameters
bufBuffer for result
localIf 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 396 of file date.c.

397{
398 if (!buf)
399 return;
400
401 struct tm tm = { 0 };
402 int tz = 0;
403
404 time_t t = mutt_date_now();
405 if (local)
406 {
407 tm = mutt_date_localtime(t);
408 tz = mutt_date_local_tz(t);
409 }
410 else
411 {
412 tm = mutt_date_gmtime(t);
413 }
414
415 tz /= 60;
416
417 buf_add_printf(buf, "%s, %d %s %d %02d:%02d:%02d %+03d%02d", Weekdays[tm.tm_wday],
418 tm.tm_mday, Months[tm.tm_mon], tm.tm_year + 1900, tm.tm_hour,
419 tm.tm_min, tm.tm_sec, tz / 60, abs(tz) % 60);
420}
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:221
static const char *const Weekdays[]
Day of the week (abbreviated)
Definition: date.c:59
int mutt_date_local_tz(time_t t)
Calculate the local timezone in seconds east of UTC.
Definition: date.c:218
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_make_imap()

int mutt_date_make_imap ( char *  buf,
size_t  buflen,
time_t  timestamp 
)

Format date in IMAP style: DD-MMM-YYYY HH:MM:SS +ZZzz.

Parameters
bufBuffer to store the results
buflenLength of buffer
timestampTime to format
Return values
numCharacters written to buf

Caller should provide a buffer of at least 27 bytes.

Definition at line 813 of file date.c.

814{
815 if (!buf)
816 return -1;
817
818 struct tm tm = mutt_date_localtime(timestamp);
820
821 tz /= 60;
822
823 return snprintf(buf, buflen, "%02d-%s-%d %02d:%02d:%02d %+03d%02d",
824 tm.tm_mday, Months[tm.tm_mon], tm.tm_year + 1900, tm.tm_hour,
825 tm.tm_min, tm.tm_sec, tz / 60, abs(tz) % 60);
826}
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition: logging.c:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_make_time()

time_t mutt_date_make_time ( struct tm *  t,
bool  local 
)

Convert struct tm to time_t

Parameters
tTime to convert
localShould the local timezone be considered
Return values
numTime in Unix format
TIME_T_MINError

Convert a struct tm to time_t, but don't take the local timezone into account unless "local" is nonzero

Definition at line 241 of file date.c.

242{
243 if (!t)
244 return TIME_T_MIN;
245
246 static const int AccumDaysPerMonth[mutt_array_size(Months)] = {
247 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
248 };
249
250 /* Prevent an integer overflow, with some arbitrary limits. */
251 if (t->tm_year > 10000)
252 return TIME_T_MAX;
253 if (t->tm_year < -10000)
254 return TIME_T_MIN;
255
256 if ((t->tm_mday < 1) || (t->tm_mday > 31))
257 return TIME_T_MIN;
258 if ((t->tm_hour < 0) || (t->tm_hour > 23) || (t->tm_min < 0) ||
259 (t->tm_min > 59) || (t->tm_sec < 0) || (t->tm_sec > 60))
260 {
261 return TIME_T_MIN;
262 }
263 if (t->tm_year > 9999)
264 return TIME_T_MAX;
265
266 /* Compute the number of days since January 1 in the same year */
267 int yday = AccumDaysPerMonth[t->tm_mon % mutt_array_size(Months)];
268
269 /* The leap years are 1972 and every 4. year until 2096,
270 * but this algorithm will fail after year 2099 */
271 yday += t->tm_mday;
272 if ((t->tm_year % 4) || (t->tm_mon < 2))
273 yday--;
274 t->tm_yday = yday;
275
276 time_t g = yday;
277
278 /* Compute the number of days since January 1, 1970 */
279 g += (t->tm_year - 70) * (time_t) 365;
280 g += (t->tm_year - 69) / 4;
281
282 /* Compute the number of hours */
283 g *= 24;
284 g += t->tm_hour;
285
286 /* Compute the number of minutes */
287 g *= 60;
288 g += t->tm_min;
289
290 /* Compute the number of seconds */
291 g *= 60;
292 g += t->tm_sec;
293
294 if (local)
295 g -= compute_tz(g, t);
296
297 return g;
298}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_make_tls()

int mutt_date_make_tls ( char *  buf,
size_t  buflen,
time_t  timestamp 
)

Format date in TLS certificate verification style.

Parameters
bufBuffer to store the results
buflenLength of buffer
timestampTime to format
Return values
numCharacters 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 839 of file date.c.

840{
841 if (!buf)
842 return -1;
843
844 struct tm tm = mutt_date_gmtime(timestamp);
845 return snprintf(buf, buflen, "%s, %d %s %d %02d:%02d:%02d UTC",
846 Weekdays[tm.tm_wday], tm.tm_mday, Months[tm.tm_mon],
847 tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
848}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_normalize_time()

void mutt_date_normalize_time ( struct tm *  tm)

Fix the contents of a struct tm.

Parameters
tmTime 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 309 of file date.c.

310{
311 if (!tm)
312 return;
313
314 static const char DaysPerMonth[mutt_array_size(Months)] = {
315 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
316 };
317 int leap;
318
319 while (tm->tm_sec < 0)
320 {
321 tm->tm_sec += 60;
322 tm->tm_min--;
323 }
324 while (tm->tm_sec >= 60)
325 {
326 tm->tm_sec -= 60;
327 tm->tm_min++;
328 }
329 while (tm->tm_min < 0)
330 {
331 tm->tm_min += 60;
332 tm->tm_hour--;
333 }
334 while (tm->tm_min >= 60)
335 {
336 tm->tm_min -= 60;
337 tm->tm_hour++;
338 }
339 while (tm->tm_hour < 0)
340 {
341 tm->tm_hour += 24;
342 tm->tm_mday--;
343 }
344 while (tm->tm_hour >= 24)
345 {
346 tm->tm_hour -= 24;
347 tm->tm_mday++;
348 }
349 /* use loops on NNNdwmy user input values? */
350 while (tm->tm_mon < 0)
351 {
352 tm->tm_mon += 12;
353 tm->tm_year--;
354 }
355 while (tm->tm_mon >= 12)
356 {
357 tm->tm_mon -= 12;
358 tm->tm_year++;
359 }
360 while (tm->tm_mday <= 0)
361 {
362 if (tm->tm_mon)
363 {
364 tm->tm_mon--;
365 }
366 else
367 {
368 tm->tm_mon = 11;
369 tm->tm_year--;
370 }
371 tm->tm_mday += DaysPerMonth[tm->tm_mon] + is_leap_year_feb(tm);
372 }
373 while (tm->tm_mday > (DaysPerMonth[tm->tm_mon] + (leap = is_leap_year_feb(tm))))
374 {
375 tm->tm_mday -= DaysPerMonth[tm->tm_mon] + leap;
376 if (tm->tm_mon < 11)
377 {
378 tm->tm_mon++;
379 }
380 else
381 {
382 tm->tm_mon = 0;
383 tm->tm_year++;
384 }
385 }
386}
static int is_leap_year_feb(struct tm *tm)
Is a given February in a leap year.
Definition: date.c:201
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_parse_date()

time_t mutt_date_parse_date ( const char *  s,
struct Tz tz_out 
)

Parse a date string in RFC822 format.

Parameters
[in]sString to parse
[out]tz_outPointer to timezone (optional)
Return values
numUnix time in seconds

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 715 of file date.c.

716{
717 if (!s)
718 return -1;
719
720 const time_t strict_t = mutt_date_parse_rfc5322_strict(s, tz_out);
721 if (strict_t != -1)
722 return strict_t;
723
724 const regmatch_t *match = mutt_prex_capture(PREX_RFC5322_DATE_LAX, s);
725 if (!match)
726 {
727 mutt_debug(LL_DEBUG1, "Could not parse date: <%s>\n", s);
728 return -1;
729 }
730 mutt_debug(LL_DEBUG2, "Fallback regex for date: <%s>\n", s);
731
732 struct tm tm = { 0 };
733
734 // clang-format off
735 const regmatch_t *mday = &match[PREX_RFC5322_DATE_LAX_MATCH_DAY];
736 const regmatch_t *mmonth = &match[PREX_RFC5322_DATE_LAX_MATCH_MONTH];
737 const regmatch_t *myear = &match[PREX_RFC5322_DATE_LAX_MATCH_YEAR];
738 const regmatch_t *mhour = &match[PREX_RFC5322_DATE_LAX_MATCH_HOUR];
739 const regmatch_t *mminute = &match[PREX_RFC5322_DATE_LAX_MATCH_MINUTE];
740 const regmatch_t *msecond = &match[PREX_RFC5322_DATE_LAX_MATCH_SECOND];
741 const regmatch_t *mtz = &match[PREX_RFC5322_DATE_LAX_MATCH_TZ];
742 const regmatch_t *mtzobs = &match[PREX_RFC5322_DATE_LAX_MATCH_TZ_OBS];
743 // clang-format on
744
745 /* Day */
746 sscanf(s + mutt_regmatch_start(mday), "%d", &tm.tm_mday);
747 if (tm.tm_mday > 31)
748 return -1;
749
750 /* Month */
751 tm.tm_mon = mutt_date_check_month(s + mutt_regmatch_start(mmonth));
752
753 /* Year */
754 sscanf(s + mutt_regmatch_start(myear), "%d", &tm.tm_year);
755 if (tm.tm_year < 50)
756 tm.tm_year += 100;
757 else if (tm.tm_year >= 1900)
758 tm.tm_year -= 1900;
759
760 /* Time */
761 int hour, min, sec = 0;
762 sscanf(s + mutt_regmatch_start(mhour), "%d", &hour);
763 sscanf(s + mutt_regmatch_start(mminute), "%d", &min);
764 if (mutt_regmatch_start(msecond) != -1)
765 sscanf(s + mutt_regmatch_start(msecond), "%d", &sec);
766 if ((hour > 23) || (min > 59) || (sec > 60))
767 return -1;
768 tm.tm_hour = hour;
769 tm.tm_min = min;
770 tm.tm_sec = sec;
771
772 /* Time zone */
773 int zhours = 0;
774 int zminutes = 0;
775 bool zoccident = false;
776 if (mutt_regmatch_start(mtz) != -1)
777 {
778 char direction;
779 sscanf(s + mutt_regmatch_start(mtz), "%c%02d%02d", &direction, &zhours, &zminutes);
780 zoccident = (direction == '-');
781 }
782 else if (mutt_regmatch_start(mtzobs) != -1)
783 {
784 const struct Tz *tz = find_tz(s + mutt_regmatch_start(mtzobs),
785 mutt_regmatch_len(mtzobs));
786 if (tz)
787 {
788 zhours = tz->zhours;
789 zminutes = tz->zminutes;
790 zoccident = tz->zoccident;
791 }
792 }
793
794 if (tz_out)
795 {
796 tz_out->zhours = zhours;
797 tz_out->zminutes = zminutes;
798 tz_out->zoccident = zoccident;
799 }
800
802}
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
time_t mutt_date_make_time(struct tm *t, bool local)
Convert struct tm to time_t
Definition: date.c:241
int mutt_date_check_month(const char *s)
Is the string a valid month name.
Definition: date.c:431
static const struct Tz * find_tz(const char *s, size_t len)
Look up a timezone.
Definition: date.c:186
static time_t mutt_date_parse_rfc5322_strict(const char *s, struct Tz *tz_out)
Parse a date string in RFC822 format.
Definition: date.c:536
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.
Definition: date.c:171
regmatch_t * mutt_prex_capture(enum Prex which, const char *str)
Match a precompiled regex against a string.
Definition: prex.c:295
@ PREX_RFC5322_DATE_LAX
[Mon, (Comment) 16 Mar 2020 15:09:35 -0700]
Definition: prex.h:38
@ PREX_RFC5322_DATE_LAX_MATCH_SECOND
Tue, 3 Mar 2020 14:32:[55] +0200
Definition: prex.h:147
@ PREX_RFC5322_DATE_LAX_MATCH_TZ
Tue, 3 Mar 2020 14:32:55 [+0200]
Definition: prex.h:150
@ PREX_RFC5322_DATE_LAX_MATCH_YEAR
Tue, 3 Mar [2020] 14:32:55 +0200
Definition: prex.h:139
@ PREX_RFC5322_DATE_LAX_MATCH_HOUR
Tue, 3 Mar 2020 [14]:32:55 +0200
Definition: prex.h:141
@ PREX_RFC5322_DATE_LAX_MATCH_MINUTE
Tue, 3 Mar 2020 14:[32]:55 +0200
Definition: prex.h:143
@ PREX_RFC5322_DATE_LAX_MATCH_TZ_OBS
Tue, 3 Mar 2020 14:32:55[UT]
Definition: prex.h:151
@ PREX_RFC5322_DATE_LAX_MATCH_MONTH
Tue, 3 [Jan] 2020 14:32:55 +0200
Definition: prex.h:137
@ PREX_RFC5322_DATE_LAX_MATCH_DAY
Tue, [3] Mar 2020 14:32:55 +0200
Definition: prex.h:135
static size_t mutt_regmatch_len(const regmatch_t *match)
Return the length of a match.
Definition: regex3.h:76
static regoff_t mutt_regmatch_start(const regmatch_t *match)
Return the start of a match.
Definition: regex3.h:56
List of recognised Timezones.
Definition: date.h:50
unsigned char zminutes
Minutes away from UTC.
Definition: date.h:53
bool zoccident
True if west of UTC, False if East.
Definition: date.h:54
unsigned char zhours
Hours away from UTC.
Definition: date.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_parse_imap()

time_t mutt_date_parse_imap ( const char *  s)

Parse date of the form: DD-MMM-YYYY HH:MM:SS +ZZzz.

Parameters
sDate in string form
Return values
numUnix time
0Error

Definition at line 856 of file date.c.

857{
858 const regmatch_t *match = mutt_prex_capture(PREX_IMAP_DATE, s);
859 if (!match)
860 return 0;
861
862 const regmatch_t *mday = &match[PREX_IMAP_DATE_MATCH_DAY];
863 const regmatch_t *mmonth = &match[PREX_IMAP_DATE_MATCH_MONTH];
864 const regmatch_t *myear = &match[PREX_IMAP_DATE_MATCH_YEAR];
865 const regmatch_t *mtime = &match[PREX_IMAP_DATE_MATCH_TIME];
866 const regmatch_t *mtz = &match[PREX_IMAP_DATE_MATCH_TZ];
867
868 struct tm tm = { 0 };
869
870 sscanf(s + mutt_regmatch_start(mday), " %d", &tm.tm_mday);
871 tm.tm_mon = mutt_date_check_month(s + mutt_regmatch_start(mmonth));
872 sscanf(s + mutt_regmatch_start(myear), "%d", &tm.tm_year);
873 tm.tm_year -= 1900;
874 sscanf(s + mutt_regmatch_start(mtime), "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
875
876 char direction;
877 int zhours, zminutes;
878 sscanf(s + mutt_regmatch_start(mtz), "%c%02d%02d", &direction, &zhours, &zminutes);
879 bool zoccident = (direction == '-');
880
881 return add_tz_offset(mutt_date_make_time(&tm, false), zoccident, zhours, zminutes);
882}
@ PREX_IMAP_DATE_MATCH_TIME
15-MAR-2020 [15:09:35] -0700
Definition: prex.h:168
@ PREX_IMAP_DATE_MATCH_YEAR
15-MAR-[2020] 15:09:35 -0700
Definition: prex.h:167
@ PREX_IMAP_DATE_MATCH_DAY
[ 4]-MAR-2020 15:09:35 -0700
Definition: prex.h:163
@ PREX_IMAP_DATE_MATCH_TZ
15-MAR-2020 15:09:35 [-0700]
Definition: prex.h:169
@ PREX_IMAP_DATE_MATCH_MONTH
15-[MAR]-2020 15:09:35 -0700
Definition: prex.h:166
@ PREX_IMAP_DATE
[16-MAR-2020 15:09:35 -0700]
Definition: prex.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_date_sleep_ms()

void mutt_date_sleep_ms ( size_t  ms)

Sleep for milliseconds.

Parameters
msNumber of milliseconds to sleep

Definition at line 985 of file date.c.

986{
987 const struct timespec sleep = {
988 .tv_sec = ms / 1000,
989 .tv_nsec = (ms % 1000) * 1000000UL,
990 };
991 nanosleep(&sleep, NULL);
992}
Time value with nanosecond precision.
Definition: file.h:51
time_t tv_sec
Number of seconds since the epoch.
Definition: file.h:52
+ Here is the caller graph for this function:

◆ mutt_time_now()

void mutt_time_now ( struct timespec tp)

Set the provided time field to the current time.

Parameters
[out]tpField to set

Uses nanosecond precision if available, if not we fallback to microseconds.

Definition at line 479 of file date.c.

480{
481#ifdef HAVE_CLOCK_GETTIME
482 if (clock_gettime(CLOCK_REALTIME, tp) != 0)
483 mutt_perror("clock_gettime");
484#else
485 struct timeval tv = { 0, 0 };
486 if (gettimeofday(&tv, NULL) != 0)
487 mutt_perror("gettimeofday");
488 tp->tv_sec = tv.tv_sec;
489 tp->tv_nsec = tv.tv_usec * 1000;
490#endif
491}
#define mutt_perror(...)
Definition: logging2.h:93
long tv_nsec
Number of nanosecond, on top.
Definition: file.h:53
+ Here is the caller graph for this function: