NeoMutt  2024-04-25-89-g194907
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 (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)

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

891{
892 if (timeout < 0)
893 return now;
894
895 if ((TIME_T_MAX - now) < timeout)
896 return TIME_T_MAX;
897
898 return now + timeout;
899}
#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 432 of file date.c.

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

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

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

928{
929 struct tm tm = { 0 };
930
931 struct tm *ret = gmtime_r(&t, &tm);
932 if (!ret)
933 {
934 mutt_debug(LL_DEBUG1, "Could not convert time_t via gmtime_r() to struct tm: time_t = %jd\n",
935 (intmax_t) t);
936 struct tm default_tm = { 0 }; // 1970-01-01 00:00:00
937 mktime(&default_tm); // update derived fields making tm into a valid tm.
938 tm = default_tm;
939 }
940 return tm;
941}
#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 951 of file date.c.

952{
953 if (!buf || !format)
954 return 0;
955
956 struct tm tm = mutt_date_localtime(t);
957 return strftime(buf, buflen, format, &tm);
958}
struct tm mutt_date_localtime(time_t t)
Converts calendar time to a broken-down time structure expressed in user timezone.
Definition: date.c:906
+ 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 969 of file date.c.

971{
972 if (!buf || !format)
973 return 0;
974
975 struct tm tm = mutt_date_localtime(t);
976 return strftime_l(buf, buflen, format, &tm, loc);
977}
+ 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 906 of file date.c.

907{
908 struct tm tm = { 0 };
909
910 struct tm *ret = localtime_r(&t, &tm);
911 if (!ret)
912 {
913 mutt_debug(LL_DEBUG1, "Could not convert time_t via localtime_r() to struct tm: time_t = %jd\n",
914 (intmax_t) t);
915 struct tm default_tm = { 0 }; // 1970-01-01 00:00:00
916 mktime(&default_tm); // update derived fields making tm into a valid tm.
917 tm = default_tm;
918 }
919 return tm;
920}
+ 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 219 of file date.c.

220{
221 /* Check we haven't overflowed the time (on 32-bit arches) */
222 if ((t == TIME_T_MAX) || (t == TIME_T_MIN))
223 return 0;
224
225 if (t == 0)
226 t = mutt_date_now();
227
228 struct tm tm = mutt_date_gmtime(t);
229 return compute_tz(t, &tm);
230}
#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:140
struct tm mutt_date_gmtime(time_t t)
Converts calendar time to a broken-down time structure expressed in UTC timezone.
Definition: date.c:927
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:456
+ 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 397 of file date.c.

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

812{
813 if (!buf)
814 return -1;
815
816 struct tm tm = mutt_date_localtime(timestamp);
818
819 tz /= 60;
820
821 return buf_printf(buf, "%02d-%s-%d %02d:%02d:%02d %+03d%02d", tm.tm_mday,
822 Months[tm.tm_mon], tm.tm_year + 1900, tm.tm_hour, tm.tm_min,
823 tm.tm_sec, tz / 60, abs(tz) % 60);
824}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
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 242 of file date.c.

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

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

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

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

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

984{
985 const struct timespec sleep = {
986 .tv_sec = ms / 1000,
987 .tv_nsec = (ms % 1000) * 1000000UL,
988 };
989 nanosleep(&sleep, NULL);
990}
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 480 of file date.c.

481{
482#ifdef HAVE_CLOCK_GETTIME
483 if (clock_gettime(CLOCK_REALTIME, tp) != 0)
484 mutt_perror("clock_gettime");
485#else
486 struct timeval tv = { 0, 0 };
487 if (gettimeofday(&tv, NULL) != 0)
488 mutt_perror("gettimeofday");
489 tp->tv_sec = tv.tv_sec;
490 tp->tv_nsec = tv.tv_usec * 1000;
491#endif
492}
#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: