NeoMutt  2025-12-11-980-ge38c27
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)
 Maximum value for a time_t.
 
#define TIME_T_MIN   (-TIME_T_MAX - 1)
 Minimum value for a time_t.
 
#define TM_YEAR_MAX    (1970 + (((((TIME_T_MAX - 59) / 60) - 59) / 60) - 23) / 24 / 366)
 Maximum year value representable in a struct tm.
 
#define TM_YEAR_MIN   (1970 - (TM_YEAR_MAX - 1970) - 1)
 Minimum year value representable in a struct tm.
 

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 (struct Buffer *buf, 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)

Maximum value for a time_t.

Definition at line 40 of file date.h.

◆ TIME_T_MIN

#define TIME_T_MIN   (-TIME_T_MAX - 1)

Minimum value for a time_t.

Definition at line 42 of file date.h.

◆ TM_YEAR_MAX

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

Maximum year value representable in a struct tm.

Definition at line 44 of file date.h.

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

◆ TM_YEAR_MIN

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

Minimum year value representable in a struct tm.

Definition at line 47 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 896 of file date.c.

897{
898 if (timeout < 0)
899 return now;
900
901 if ((TIME_T_MAX - now) < timeout)
902 return TIME_T_MAX;
903
904 return now + timeout;
905}
#define TIME_T_MAX
Maximum value for a time_t.
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 435 of file date.c.

436{
437 if (!s)
438 return -1;
439
440 char buf[4] = { 0 };
441 memcpy(buf, s, 3);
442 uint32_t sv = 0;
443 memcpy(&sv, buf, sizeof(sv));
444 for (int i = 0; i < countof(Months); i++)
445 {
446 uint32_t mv = 0;
447 memcpy(&mv, Months[i], sizeof(mv));
448 if (sv == mv)
449 return i;
450 }
451
452 return -1; /* error */
453}
#define countof(x)
Definition memory.h:49
static const char *const Months[]
Months of the year (abbreviated)
Definition date.c:68
+ 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 459 of file date.c.

460{
461 return mutt_date_now_ms() / 1000;
462}
uint64_t mutt_date_now_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition date.c:468
+ 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 468 of file date.c.

469{
470 struct timeval tv = { 0, 0 };
471 gettimeofday(&tv, NULL);
472 /* We assume that gettimeofday doesn't modify its first argument on failure.
473 * We also kind of assume that gettimeofday does not fail. */
474 return ((uint64_t) tv.tv_sec * 1000) + (tv.tv_usec / 1000);
475}
+ 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 933 of file date.c.

934{
935 struct tm tm = { 0 };
936
937 struct tm *ret = gmtime_r(&t, &tm);
938 if (!ret)
939 {
940 mutt_debug(LL_DEBUG1, "Could not convert time_t via gmtime_r() to struct tm: time_t = %jd\n",
941 (intmax_t) t);
942 struct tm default_tm = { 0 }; // 1970-01-01 00:00:00
943 mktime(&default_tm); // update derived fields making tm into a valid tm.
944 tm = default_tm;
945 }
946 return tm;
947}
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
+ Here is the call graph for this function:
+ 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 957 of file date.c.

958{
959 if (!buf || !format)
960 return 0;
961
962 struct tm tm = mutt_date_localtime(t);
963 return strftime(buf, buflen, format, &tm);
964}
struct tm mutt_date_localtime(time_t t)
Converts calendar time to a broken-down time structure expressed in user timezone.
Definition date.c:912
+ 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 975 of file date.c.

977{
978 if (!buf || !format)
979 return 0;
980
981 struct tm tm = mutt_date_localtime(t);
982 return strftime_l(buf, buflen, format, &tm, loc);
983}
+ 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 912 of file date.c.

913{
914 struct tm tm = { 0 };
915
916 struct tm *ret = localtime_r(&t, &tm);
917 if (!ret)
918 {
919 mutt_debug(LL_DEBUG1, "Could not convert time_t via localtime_r() to struct tm: time_t = %jd\n",
920 (intmax_t) t);
921 struct tm default_tm = { 0 }; // 1970-01-01 00:00:00
922 mktime(&default_tm); // update derived fields making tm into a valid tm.
923 tm = default_tm;
924 }
925 return tm;
926}
+ Here is the call graph for this function:
+ 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 220 of file date.c.

221{
222 /* Check we haven't overflowed the time (on 32-bit arches) */
223 if ((t == TIME_T_MAX) || (t == TIME_T_MIN))
224 return 0;
225
226 if (t == 0)
227 t = mutt_date_now();
228
229 struct tm tm = mutt_date_gmtime(t);
230 return compute_tz(t, &tm);
231}
#define TIME_T_MIN
Minimum value for a time_t.
Definition date.h:42
static int compute_tz(time_t g, struct tm *utc)
Calculate the number of seconds east of UTC.
Definition date.c:141
struct tm mutt_date_gmtime(time_t t)
Converts calendar time to a broken-down time structure expressed in UTC timezone.
Definition date.c:933
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition date.c:459
+ 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 400 of file date.c.

401{
402 if (!buf)
403 return;
404
405 struct tm tm = { 0 };
406 int tz = 0;
407
408 time_t t = mutt_date_now();
409 if (local)
410 {
411 tm = mutt_date_localtime(t);
412 tz = mutt_date_local_tz(t);
413 }
414 else
415 {
416 tm = mutt_date_gmtime(t);
417 }
418
419 tz /= 60;
420
421 buf_add_printf(buf, "%s, %d %s %d %02d:%02d:%02d %+03d%02d", Weekdays[tm.tm_wday],
422 tm.tm_mday, Months[tm.tm_mon], tm.tm_year + 1900, tm.tm_hour,
423 tm.tm_min, tm.tm_sec, tz / 60, abs(tz) % 60);
424}
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:211
static const char *const Weekdays[]
Day of the week (abbreviated)
Definition date.c:61
int mutt_date_local_tz(time_t t)
Calculate the local timezone in seconds east of UTC.
Definition date.c:220
+ 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 ( struct Buffer * buf,
time_t timestamp )

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

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

Definition at line 816 of file date.c.

817{
818 if (!buf)
819 return -1;
820
821 struct tm tm = mutt_date_localtime(timestamp);
823
824 tz /= 60;
825
826 return buf_printf(buf, "%02d-%s-%d %02d:%02d:%02d %+03d%02d", tm.tm_mday,
827 Months[tm.tm_mon], tm.tm_year + 1900, tm.tm_hour, tm.tm_min,
828 tm.tm_sec, tz / 60, abs(tz) % 60);
829}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:168
static const char * timestamp(time_t stamp)
Create a YYYY-MM-DD HH:MM:SS timestamp.
Definition logging.c:79
+ 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 243 of file date.c.

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

843{
844 if (!buf)
845 return -1;
846
847 struct tm tm = mutt_date_gmtime(timestamp);
848 return snprintf(buf, buflen, "%s, %d %s %d %02d:%02d:%02d UTC",
849 Weekdays[tm.tm_wday], tm.tm_mday, Months[tm.tm_mon],
850 tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec);
851}
+ 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 313 of file date.c.

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

720{
721 if (!s)
722 return -1;
723
724 const time_t strict_t = mutt_date_parse_rfc5322_strict(s, tz_out);
725 if (strict_t != -1)
726 return strict_t;
727
728 const regmatch_t *match = mutt_prex_capture(PREX_RFC5322_DATE_LAX, s);
729 if (!match)
730 {
731 mutt_debug(LL_DEBUG1, "Could not parse date: <%s>\n", s);
732 return -1;
733 }
734 mutt_debug(LL_DEBUG2, "Fallback regex for date: <%s>\n", s);
735
736 struct tm tm = { 0 };
737
738 // clang-format off
739 const regmatch_t *mday = &match[PREX_RFC5322_DATE_LAX_MATCH_DAY];
740 const regmatch_t *mmonth = &match[PREX_RFC5322_DATE_LAX_MATCH_MONTH];
741 const regmatch_t *myear = &match[PREX_RFC5322_DATE_LAX_MATCH_YEAR];
742 const regmatch_t *mhour = &match[PREX_RFC5322_DATE_LAX_MATCH_HOUR];
743 const regmatch_t *mminute = &match[PREX_RFC5322_DATE_LAX_MATCH_MINUTE];
744 const regmatch_t *msecond = &match[PREX_RFC5322_DATE_LAX_MATCH_SECOND];
745 const regmatch_t *mtz = &match[PREX_RFC5322_DATE_LAX_MATCH_TZ];
746 const regmatch_t *mtzobs = &match[PREX_RFC5322_DATE_LAX_MATCH_TZ_OBS];
747 // clang-format on
748
749 /* Day */
750 sscanf(s + mutt_regmatch_start(mday), "%d", &tm.tm_mday);
751 if (tm.tm_mday > 31)
752 return -1;
753
754 /* Month */
755 tm.tm_mon = mutt_date_check_month(s + mutt_regmatch_start(mmonth));
756
757 /* Year */
758 sscanf(s + mutt_regmatch_start(myear), "%d", &tm.tm_year);
759 if (tm.tm_year < 50)
760 tm.tm_year += 100;
761 else if (tm.tm_year >= 1900)
762 tm.tm_year -= 1900;
763
764 /* Time */
765 int hour = 0;
766 int min = 0;
767 int sec = 0;
768 sscanf(s + mutt_regmatch_start(mhour), "%d", &hour);
769 sscanf(s + mutt_regmatch_start(mminute), "%d", &min);
770 if (mutt_regmatch_start(msecond) != -1)
771 sscanf(s + mutt_regmatch_start(msecond), "%d", &sec);
772 if ((hour > 23) || (min > 59) || (sec > 60))
773 return -1;
774 tm.tm_hour = hour;
775 tm.tm_min = min;
776 tm.tm_sec = sec;
777
778 /* Time zone */
779 int zhours = 0;
780 int zminutes = 0;
781 bool zoccident = false;
782 if (mutt_regmatch_start(mtz) != -1)
783 {
784 char direction = '\0';
785 sscanf(s + mutt_regmatch_start(mtz), "%c%02d%02d", &direction, &zhours, &zminutes);
786 zoccident = (direction == '-');
787 }
788 else if (mutt_regmatch_start(mtzobs) != -1)
789 {
790 const struct Tz *tz = find_tz(s + mutt_regmatch_start(mtzobs),
791 mutt_regmatch_len(mtzobs));
792 if (tz)
793 {
794 zhours = tz->zhours;
795 zminutes = tz->zminutes;
796 zoccident = tz->zoccident;
797 }
798 }
799
800 if (tz_out)
801 {
802 tz_out->zhours = zhours;
803 tz_out->zminutes = zminutes;
804 tz_out->zoccident = zoccident;
805 }
806
808}
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
time_t mutt_date_make_time(struct tm *t, bool local)
Convert struct tm to time_t
Definition date.c:243
int mutt_date_check_month(const char *s)
Is the string a valid month name.
Definition date.c:435
static const struct Tz * find_tz(const char *s, size_t len)
Look up a timezone.
Definition date.c:188
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:540
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:173
regmatch_t * mutt_prex_capture(enum Prex which, const char *str)
Match a precompiled regex against a string.
Definition prex.c:301
@ 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:53
unsigned char zminutes
Minutes away from UTC.
Definition date.h:56
bool zoccident
True if west of UTC, False if East.
Definition date.h:57
unsigned char zhours
Hours away from UTC.
Definition date.h:55
+ 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 859 of file date.c.

860{
861 const regmatch_t *match = mutt_prex_capture(PREX_IMAP_DATE, s);
862 if (!match)
863 return 0;
864
865 const regmatch_t *mday = &match[PREX_IMAP_DATE_MATCH_DAY];
866 const regmatch_t *mmonth = &match[PREX_IMAP_DATE_MATCH_MONTH];
867 const regmatch_t *myear = &match[PREX_IMAP_DATE_MATCH_YEAR];
868 const regmatch_t *mtime = &match[PREX_IMAP_DATE_MATCH_TIME];
869 const regmatch_t *mtz = &match[PREX_IMAP_DATE_MATCH_TZ];
870
871 struct tm tm = { 0 };
872
873 sscanf(s + mutt_regmatch_start(mday), " %d", &tm.tm_mday);
874 tm.tm_mon = mutt_date_check_month(s + mutt_regmatch_start(mmonth));
875 sscanf(s + mutt_regmatch_start(myear), "%d", &tm.tm_year);
876 tm.tm_year -= 1900;
877 sscanf(s + mutt_regmatch_start(mtime), "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
878
879 char direction = '\0';
880 int zhours = 0;
881 int zminutes = 0;
882 sscanf(s + mutt_regmatch_start(mtz), "%c%02d%02d", &direction, &zhours, &zminutes);
883 bool zoccident = (direction == '-');
884
885 return add_tz_offset(mutt_date_make_time(&tm, false), zoccident, zhours, zminutes);
886}
@ 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 989 of file date.c.

990{
991 const struct timespec sleep = {
992 .tv_sec = ms / 1000,
993 .tv_nsec = (ms % 1000) * 1000000UL,
994 };
995 nanosleep(&sleep, NULL);
996}
+ 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 483 of file date.c.

484{
485#ifdef HAVE_CLOCK_GETTIME
486 if (clock_gettime(CLOCK_REALTIME, tp) != 0)
487 mutt_perror("clock_gettime");
488#else
489 struct timeval tv = { 0, 0 };
490 if (gettimeofday(&tv, NULL) != 0)
491 mutt_perror("gettimeofday");
492 tp->tv_sec = tv.tv_sec;
493 tp->tv_nsec = tv.tv_usec * 1000;
494#endif
495}
#define mutt_perror(...)
Definition logging2.h:95
+ Here is the caller graph for this function: