NetSurf
|
Implementation of time operations. More...
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "utils/ascii.h"
#include "utils/errors.h"
#include "utils/time.h"
Go to the source code of this file.
Data Structures | |
struct | nsc_date_parse_ctx |
Context for date parsing. More... | |
Functions | |
const char * | rfc1123_date (time_t t) |
Create an RFC 1123 compliant date string from a Unix timestamp. More... | |
int | nsc_sntimet (char *str, size_t size, time_t *timep) |
Write the time in seconds since epoch to a buffer. More... | |
nserror | nsc_snptimet (const char *str, size_t size, time_t *timep) |
Parse time in seconds since epoc. More... | |
static bool | flags_chk (enum nsc_date_component_flags flags, enum nsc_date_component_flags mask) |
Helper for testing whether any of the flags in mask are set. More... | |
static bool | flags_chk_all (enum nsc_date_component_flags flags, enum nsc_date_component_flags mask) |
Helper for testing whether all of the flags in mask are set. More... | |
static bool | time__parse_weekday (const char *str, size_t len, enum nsc_date_component_flags *flags) |
Test for a weekday name in a string (case insensitive). More... | |
static bool | time__parse_month (const char *str, size_t len, enum nsc_date_component_flags *flags, struct nsc_date_parse_ctx *ctx) |
Attempt to parse a month name in a string (case insensitive). More... | |
static bool | time__parse_timezone (const char *str, size_t len, enum nsc_date_component_flags *flags, struct nsc_date_parse_ctx *ctx) |
Attempt to parse a timezone name in a string (case insensitive). More... | |
static bool | time__parse_hh_mm_ss (const char *str, size_t *len, enum nsc_date_component_flags *flags, struct nsc_date_parse_ctx *ctx) |
Attempt to parse an "hh:mm:ss" time from a string. More... | |
static bool | time__parse_number (const char *str, size_t len, enum nsc_date_component_flags *flags, struct nsc_date_parse_ctx *ctx) |
Attempt to parse a number from a date string. More... | |
static int | time__get_leap_days (int year) |
Get number of leap days up until end of given year. More... | |
static nserror | time__ctx_to_time_t (const struct nsc_date_parse_ctx *ctx, enum nsc_date_component_flags flags, time_t *time) |
Helper to convert a date string context to a time_t. More... | |
static nserror | time__get_date (const char *str, time_t *time) |
Parse a date string to a time_t . More... | |
nserror | nsc_strntimet (const char *str, size_t size, time_t *timep) |
Converts a date string to a number of seconds since epoch. More... | |
Variables | |
static const char *const | weekdays_short [NSC_TIME_WEEKDAY__COUNT] |
Array of short weekday names. More... | |
static const char *const | months [NSC_TIME_MONTH__COUNT] |
Array of month names. More... | |
static const char *const | weekdays_long [NSC_TIME_WEEKDAY__COUNT] |
Array of long weekday names. More... | |
static const int16_t | timezone_mins [NSC_TIME_ZONE__COUNT] |
Array of minute offsets for timezones. More... | |
static const char *const | timezones [NSC_TIME_ZONE__COUNT] |
Array of timezone names. More... | |
Implementation of time operations.
Definition in file time.c.
Flags for tracking the components of a date that have been parsed.
enum nsc_time_months |
enum nsc_time_weekdays |
Timezone offsets in mins.
Order doesn't matter.
enum nsc_time_zones |
List of timezones.
The order here is the order they appear in the timezone_mins
and timezones
arrays. So there is value in putting the most common timezones first.
|
static |
Helper for testing whether any of the flags in mask are set.
[in] | flags | Flags to to check. |
[in] | mask | The set of flags to check for in flags . |
mask
are set in flags
, else false. Definition at line 560 of file time.c.
Referenced by time__parse_hh_mm_ss(), time__parse_month(), time__parse_number(), time__parse_timezone(), and time__parse_weekday().
|
static |
Helper for testing whether all of the flags in mask are set.
[in] | flags | Flags to to check. |
[in] | mask | The set of flags to check for in flags . |
mask
are set in flags
, else false. Definition at line 574 of file time.c.
Referenced by time__get_date().
nserror nsc_snptimet | ( | const char * | str, |
size_t | size, | ||
time_t * | timep | ||
) |
Parse time in seconds since epoc.
This is provided as strptime is not generally portable.
str | The source buffer. |
size | The length of the source buffer. |
timep | Pointer to result. |
Definition at line 147 of file time.c.
References NSERROR_BAD_PARAMETER, NSERROR_OK, and strtoll().
Referenced by fetch_file_setup(), fetch_resource_setup(), llcache_process_metadata(), and urldb_load().
int nsc_sntimet | ( | char * | str, |
size_t | size, | ||
time_t * | timep | ||
) |
Write the time in seconds since epoch to a buffer.
This is provided as strftime is not generally portable.
str | The destination buffer. |
size | The length of the destination buffer. |
timep | The pointer to the time to write. |
Definition at line 126 of file time.c.
Referenced by llcache_serialise_metadata(), and urldb_write_timet().
nserror nsc_strntimet | ( | const char * | str, |
size_t | size, | ||
time_t * | timep | ||
) |
Converts a date string to a number of seconds since epoch.
returns the number of seconds since the Epoch, January 1st 1970 00:00:00 in the UTC time zone, for the date and time that the str parameter specifies.
datetime strings passed must be in one of the formats specified in:
[in] | str | The datetime string to parse |
[in] | size | The length of the source string |
[out] | timep | Pointer to result on success unmodified on error. |
Definition at line 980 of file time.c.
References time__get_date().
Referenced by llcache_fetch_header_cache_control(), and urldb_parse_avpair().
const char * rfc1123_date | ( | time_t | t | ) |
Create an RFC 1123 compliant date string from a Unix timestamp.
t | The timestamp to consider |
Definition at line 110 of file time.c.
References months, and weekdays_short.
Referenced by llcache_object_refetch().
|
static |
Helper to convert a date string context to a time_t.
[in] | ctx | Current date parsing context. |
[in] | flags | Flags indicating which date components have been set. |
[out] | time | Returns the number of seconds since 1 Jan 1970 00:00 UTC. |
< Leap years handled separatly
Definition at line 844 of file time.c.
References nsc_date_parse_ctx::day, nsc_date_parse_ctx::hours, nsc_date_parse_ctx::mins, nsc_date_parse_ctx::month, NSC_TIME_MONTH__COUNT, NSC_TIME_MONTH_APR, NSC_TIME_MONTH_AUG, NSC_TIME_MONTH_DEC, NSC_TIME_MONTH_FEB, NSC_TIME_MONTH_JAN, NSC_TIME_MONTH_JUL, NSC_TIME_MONTH_JUN, NSC_TIME_MONTH_MAR, NSC_TIME_MONTH_MAY, NSC_TIME_MONTH_NOV, NSC_TIME_MONTH_OCT, NSC_TIME_MONTH_SEP, NSERROR_OK, nsc_date_parse_ctx::secs, time__get_leap_days(), nsc_date_parse_ctx::timezone_offset_mins, and nsc_date_parse_ctx::years.
Referenced by time__get_date().
|
static |
Parse a date string to a time_t
.
[in] | str | String to parse. |
[out] | time | Returns the number of seconds since 1 Jan 1970 00:00 UTC. |
NSERROR_OK
on success, else NSERROR_INVALID
if the string parsing failed, appropriate error otherwise. Definition at line 917 of file time.c.
References ascii_count_alpha(), ascii_count_digit(), ascii_is_alpha(), ascii_is_digit(), nsc_date_parse_ctx::day, flags_chk_all(), nsc_date_parse_ctx::hours, nsc_date_parse_ctx::mins, nsc_date_parse_ctx::month, NSC_COMPONENT_FLAGS__HAVE_ALL, NSC_COMPONENT_FLAGS__HAVE_HHMMSS, NSC_COMPONENT_FLAGS_HAVE_TIMEZONE, NSC_COMPONENT_FLAGS_NONE, NSERROR_BAD_PARAMETER, NSERROR_INVALID, nsc_date_parse_ctx::prev, nsc_date_parse_ctx::secs, time__ctx_to_time_t(), time__parse_hh_mm_ss(), time__parse_month(), time__parse_number(), time__parse_timezone(), and time__parse_weekday().
Referenced by nsc_strntimet().
|
inlinestatic |
Get number of leap days up until end of given year.
[in] | year | Year to count leap years up to. |
year
. Definition at line 830 of file time.c.
Referenced by time__ctx_to_time_t().
|
inlinestatic |
Attempt to parse an "hh:mm:ss" time from a string.
[in] | str | String to parse a time in. |
[in,out] | len | The number of characters until the first non-digit. Iff a time component is found, updated to the number of comsumend characters. |
[in,out] | flags | Flags indicating which date components have been found in str already. If a time component is found, the hours, mins and secs flags are set. |
[in,out] | ctx | Current date parsing context. If time component is found, the context's time values are set. |
Definition at line 698 of file time.c.
References ascii_count_digit_or_colon(), count(), flags_chk(), nsc_date_parse_ctx::hours, nsc_date_parse_ctx::mins, NSC_COMPONENT_FLAGS__HAVE_HHMMSS, and nsc_date_parse_ctx::secs.
Referenced by time__get_date().
|
inlinestatic |
Attempt to parse a month name in a string (case insensitive).
[in] | str | String to parse a month name in. |
[in] | len | Number of consecutive alphabetical characters. |
[in,out] | flags | Flags indicating which date components have been found in str already. If a month component is found, the month flag is set. |
[in,out] | ctx | Current date parsing context. If month component is found, the context's month value is set. |
Definition at line 627 of file time.c.
References ascii_strings_count_equal_caseless(), flags_chk(), nsc_date_parse_ctx::month, months, NSC_COMPONENT_FLAGS_HAVE_MONTHS, and NSC_TIME_MONTH__COUNT.
Referenced by time__get_date().
|
inlinestatic |
Attempt to parse a number from a date string.
How the number is treated depends on various things:
[in] | str | String to parse a time in. |
[in] | len | The number of characters until the first non-digit. |
[in,out] | flags | Flags indicating which date components have been found in str already. If any component is found, their flags are set. |
[in,out] | ctx | Current date parsing context. If any component is found, the appropriate context values are set. |
Definition at line 757 of file time.c.
References ascii_is_sign(), ascii_string_to_int(), nsc_date_parse_ctx::day, flags_chk(), nsc_date_parse_ctx::month, NSC_COMPONENT_FLAGS__HAVE_YYYYMMDD, NSC_COMPONENT_FLAGS_HAVE_DAYS, NSC_COMPONENT_FLAGS_HAVE_TIMEZONE, NSC_COMPONENT_FLAGS_HAVE_YEARS, nsc_date_parse_ctx::prev, nsc_date_parse_ctx::timezone_offset_mins, and nsc_date_parse_ctx::years.
Referenced by time__get_date().
|
inlinestatic |
Attempt to parse a timezone name in a string (case insensitive).
[in] | str | String to parse a timezone name in. |
[in] | len | Number of consecutive alphabetical characters. |
[in,out] | flags | Flags indicating which date components have been found in str already. If a timezone component is found, the timezone flag is set. |
[in,out] | ctx | Current date parsing context. If timezone component is found, the context's timezone value is set. |
Definition at line 661 of file time.c.
References ascii_strings_count_equal_caseless(), flags_chk(), NSC_COMPONENT_FLAGS_HAVE_TIMEZONE, NSC_TIME_ZONE__COUNT, timezone_mins, nsc_date_parse_ctx::timezone_offset_mins, and timezones.
Referenced by time__get_date().
|
inlinestatic |
Test for a weekday name in a string (case insensitive).
[in] | str | String to parse a weekday name in. |
[in] | len | Number of consecutive alphabetical characters. |
[in,out] | flags | Flags indicating which date components have been found in str already. If a weekday component is found, the weekday flag is set. |
Definition at line 592 of file time.c.
References ascii_strings_count_equal_caseless(), flags_chk(), NSC_COMPONENT_FLAGS_HAVE_WEEKDAY, NSC_TIME_WEEKDAY__COUNT, weekdays_long, and weekdays_short.
Referenced by time__get_date().
|
static |
Array of month names.
Definition at line 93 of file time.c.
Referenced by rfc1123_date(), and time__parse_month().
|
static |
Array of minute offsets for timezones.
Definition at line 364 of file time.c.
Referenced by time__parse_timezone().
|
static |
Array of timezone names.
Order does not matter.
Definition at line 437 of file time.c.
Referenced by time__parse_timezone().
|
static |
Array of long weekday names.
Definition at line 192 of file time.c.
Referenced by time__parse_weekday().
|
static |
Array of short weekday names.
Definition at line 81 of file time.c.
Referenced by rfc1123_date(), and time__parse_weekday().