NetSurf
Data Structures | Enumerations | Functions | Variables
time.c File Reference

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"
Include dependency graph for time.c:

Go to the source code of this file.

Data Structures

struct  nsc_date_parse_ctx
 Context for date parsing. More...
 

Enumerations

enum  nsc_time_weekdays {
  NSC_TIME_WEEKDAY_SUN , NSC_TIME_WEEKDAY_MON , NSC_TIME_WEEKDAY_TUE , NSC_TIME_WEEKDAY_WED ,
  NSC_TIME_WEEKDAY_THU , NSC_TIME_WEEKDAY_FRI , NSC_TIME_WEEKDAY_SAT , NSC_TIME_WEEKDAY__COUNT
}
 Weekdays. More...
 
enum  nsc_time_months {
  NSC_TIME_MONTH_JAN , NSC_TIME_MONTH_FEB , NSC_TIME_MONTH_MAR , NSC_TIME_MONTH_APR ,
  NSC_TIME_MONTH_MAY , NSC_TIME_MONTH_JUN , NSC_TIME_MONTH_JUL , NSC_TIME_MONTH_AUG ,
  NSC_TIME_MONTH_SEP , NSC_TIME_MONTH_OCT , NSC_TIME_MONTH_NOV , NSC_TIME_MONTH_DEC ,
  NSC_TIME_MONTH__COUNT
}
 Months. More...
 
enum  nsc_time_zone_offsets {
  NSC_TIME_ZONE_OFFSET_IDLE = -12 * 60 , NSC_TIME_ZONE_OFFSET_NZST = -12 * 60 , NSC_TIME_ZONE_OFFSET_NZT = -12 * 60 , NSC_TIME_ZONE_OFFSET_EAST = -10 * 60 ,
  NSC_TIME_ZONE_OFFSET_GST = -10 * 60 , NSC_TIME_ZONE_OFFSET_JST = - 9 * 60 , NSC_TIME_ZONE_OFFSET_CCT = - 8 * 60 , NSC_TIME_ZONE_OFFSET_WAST = - 7 * 60 ,
  NSC_TIME_ZONE_OFFSET_EET = - 2 * 60 , NSC_TIME_ZONE_OFFSET_CET = - 1 * 60 , NSC_TIME_ZONE_OFFSET_FWT = - 1 * 60 , NSC_TIME_ZONE_OFFSET_MET = - 1 * 60 ,
  NSC_TIME_ZONE_OFFSET_MEWT = - 1 * 60 , NSC_TIME_ZONE_OFFSET_GMT = 0 , NSC_TIME_ZONE_OFFSET_UTC = 0 , NSC_TIME_ZONE_OFFSET_WET = 0 ,
  NSC_TIME_ZONE_OFFSET_WAT = 1 * 60 , NSC_TIME_ZONE_OFFSET_AST = 4 * 60 , NSC_TIME_ZONE_OFFSET_EST = 5 * 60 , NSC_TIME_ZONE_OFFSET_CST = 6 * 60 ,
  NSC_TIME_ZONE_OFFSET_MST = 7 * 60 , NSC_TIME_ZONE_OFFSET_PST = 8 * 60 , NSC_TIME_ZONE_OFFSET_YST = 9 * 60 , NSC_TIME_ZONE_OFFSET_AHST = 10 * 60 ,
  NSC_TIME_ZONE_OFFSET_CAT = 10 * 60 , NSC_TIME_ZONE_OFFSET_HST = 10 * 60 , NSC_TIME_ZONE_OFFSET_IDLW = 12 * 60 , NSC_TIME_ZONE_OFFSET_NZDT = NSC_TIME_ZONE_OFFSET_NZT - 60 ,
  NSC_TIME_ZONE_OFFSET_EADT = NSC_TIME_ZONE_OFFSET_EAST - 60 , NSC_TIME_ZONE_OFFSET_WADT = NSC_TIME_ZONE_OFFSET_WAST - 60 , NSC_TIME_ZONE_OFFSET_CEST = NSC_TIME_ZONE_OFFSET_CET - 60 , NSC_TIME_ZONE_OFFSET_FST = NSC_TIME_ZONE_OFFSET_FWT - 60 ,
  NSC_TIME_ZONE_OFFSET_MEST = NSC_TIME_ZONE_OFFSET_MET - 60 , NSC_TIME_ZONE_OFFSET_MESZ = NSC_TIME_ZONE_OFFSET_MET - 60 , NSC_TIME_ZONE_OFFSET_BST = NSC_TIME_ZONE_OFFSET_GMT - 60 , NSC_TIME_ZONE_OFFSET_ADT = NSC_TIME_ZONE_OFFSET_AST - 60 ,
  NSC_TIME_ZONE_OFFSET_EDT = NSC_TIME_ZONE_OFFSET_EST - 60 , NSC_TIME_ZONE_OFFSET_CDT = NSC_TIME_ZONE_OFFSET_CST - 60 , NSC_TIME_ZONE_OFFSET_MDT = NSC_TIME_ZONE_OFFSET_MST - 60 , NSC_TIME_ZONE_OFFSET_PDT = NSC_TIME_ZONE_OFFSET_PST - 60 ,
  NSC_TIME_ZONE_OFFSET_YDT = NSC_TIME_ZONE_OFFSET_YST - 60 , NSC_TIME_ZONE_OFFSET_HDT = NSC_TIME_ZONE_OFFSET_HST - 60 , NSC_TIME_ZONE_OFFSET_Y = -12 * 60 , NSC_TIME_ZONE_OFFSET_X = -11 * 60 ,
  NSC_TIME_ZONE_OFFSET_W = -10 * 60 , NSC_TIME_ZONE_OFFSET_V = - 9 * 60 , NSC_TIME_ZONE_OFFSET_U = - 8 * 60 , NSC_TIME_ZONE_OFFSET_T = - 7 * 60 ,
  NSC_TIME_ZONE_OFFSET_S = - 6 * 60 , NSC_TIME_ZONE_OFFSET_R = - 5 * 60 , NSC_TIME_ZONE_OFFSET_Q = - 4 * 60 , NSC_TIME_ZONE_OFFSET_P = - 3 * 60 ,
  NSC_TIME_ZONE_OFFSET_O = - 2 * 60 , NSC_TIME_ZONE_OFFSET_N = - 1 * 60 , NSC_TIME_ZONE_OFFSET_Z = 0 * 60 , NSC_TIME_ZONE_OFFSET_A = 1 * 60 ,
  NSC_TIME_ZONE_OFFSET_B = 2 * 60 , NSC_TIME_ZONE_OFFSET_C = 3 * 60 , NSC_TIME_ZONE_OFFSET_D = 4 * 60 , NSC_TIME_ZONE_OFFSET_E = 5 * 60 ,
  NSC_TIME_ZONE_OFFSET_F = 6 * 60 , NSC_TIME_ZONE_OFFSET_G = 7 * 60 , NSC_TIME_ZONE_OFFSET_H = 8 * 60 , NSC_TIME_ZONE_OFFSET_I = 9 * 60 ,
  NSC_TIME_ZONE_OFFSET_K = 10 * 60 , NSC_TIME_ZONE_OFFSET_L = 11 * 60 , NSC_TIME_ZONE_OFFSET_M = 12 * 60
}
 Timezone offsets in mins. More...
 
enum  nsc_time_zones {
  NSC_TIME_ZONE_GMT , NSC_TIME_ZONE_IDLE , NSC_TIME_ZONE_NZST , NSC_TIME_ZONE_NZT ,
  NSC_TIME_ZONE_EAST , NSC_TIME_ZONE_GST , NSC_TIME_ZONE_JST , NSC_TIME_ZONE_CCT ,
  NSC_TIME_ZONE_WAST , NSC_TIME_ZONE_EET , NSC_TIME_ZONE_CET , NSC_TIME_ZONE_FWT ,
  NSC_TIME_ZONE_MET , NSC_TIME_ZONE_MEWT , NSC_TIME_ZONE_UTC , NSC_TIME_ZONE_WET ,
  NSC_TIME_ZONE_WAT , NSC_TIME_ZONE_AST , NSC_TIME_ZONE_EST , NSC_TIME_ZONE_CST ,
  NSC_TIME_ZONE_MST , NSC_TIME_ZONE_PST , NSC_TIME_ZONE_YST , NSC_TIME_ZONE_AHST ,
  NSC_TIME_ZONE_CAT , NSC_TIME_ZONE_HST , NSC_TIME_ZONE_IDLW , NSC_TIME_ZONE_NZDT ,
  NSC_TIME_ZONE_EADT , NSC_TIME_ZONE_WADT , NSC_TIME_ZONE_CEST , NSC_TIME_ZONE_FST ,
  NSC_TIME_ZONE_MEST , NSC_TIME_ZONE_MESZ , NSC_TIME_ZONE_BST , NSC_TIME_ZONE_ADT ,
  NSC_TIME_ZONE_EDT , NSC_TIME_ZONE_CDT , NSC_TIME_ZONE_MDT , NSC_TIME_ZONE_PDT ,
  NSC_TIME_ZONE_YDT , NSC_TIME_ZONE_HDT , NSC_TIME_ZONE_Y , NSC_TIME_ZONE_X ,
  NSC_TIME_ZONE_W , NSC_TIME_ZONE_V , NSC_TIME_ZONE_U , NSC_TIME_ZONE_T ,
  NSC_TIME_ZONE_S , NSC_TIME_ZONE_R , NSC_TIME_ZONE_Q , NSC_TIME_ZONE_P ,
  NSC_TIME_ZONE_O , NSC_TIME_ZONE_N , NSC_TIME_ZONE_Z , NSC_TIME_ZONE_A ,
  NSC_TIME_ZONE_B , NSC_TIME_ZONE_C , NSC_TIME_ZONE_D , NSC_TIME_ZONE_E ,
  NSC_TIME_ZONE_F , NSC_TIME_ZONE_G , NSC_TIME_ZONE_H , NSC_TIME_ZONE_I ,
  NSC_TIME_ZONE_K , NSC_TIME_ZONE_L , NSC_TIME_ZONE_M , NSC_TIME_ZONE__COUNT
}
 List of timezones. More...
 
enum  nsc_date_component_flags {
  NSC_COMPONENT_FLAGS_NONE = 0 , NSC_COMPONENT_FLAGS_HAVE_YEARS = (1 << 0) , NSC_COMPONENT_FLAGS_HAVE_MONTHS = (1 << 1) , NSC_COMPONENT_FLAGS_HAVE_DAYS = (1 << 2) ,
  NSC_COMPONENT_FLAGS_HAVE_HOURS = (1 << 3) , NSC_COMPONENT_FLAGS_HAVE_MINS = (1 << 4) , NSC_COMPONENT_FLAGS_HAVE_SECS = (1 << 5) , NSC_COMPONENT_FLAGS_HAVE_TIMEZONE = (1 << 6) ,
  NSC_COMPONENT_FLAGS_HAVE_WEEKDAY = (1 << 7) , NSC_COMPONENT_FLAGS__HAVE_YYYYMMDD , NSC_COMPONENT_FLAGS__HAVE_HHMMSS , NSC_COMPONENT_FLAGS__HAVE_ALL
}
 Flags for tracking the components of a date that have been parsed. 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...
 

Detailed Description

Implementation of time operations.

Definition in file time.c.

Enumeration Type Documentation

◆ nsc_date_component_flags

Flags for tracking the components of a date that have been parsed.

Enumerator
NSC_COMPONENT_FLAGS_NONE 
NSC_COMPONENT_FLAGS_HAVE_YEARS 
NSC_COMPONENT_FLAGS_HAVE_MONTHS 
NSC_COMPONENT_FLAGS_HAVE_DAYS 
NSC_COMPONENT_FLAGS_HAVE_HOURS 
NSC_COMPONENT_FLAGS_HAVE_MINS 
NSC_COMPONENT_FLAGS_HAVE_SECS 
NSC_COMPONENT_FLAGS_HAVE_TIMEZONE 
NSC_COMPONENT_FLAGS_HAVE_WEEKDAY 
NSC_COMPONENT_FLAGS__HAVE_YYYYMMDD 
NSC_COMPONENT_FLAGS__HAVE_HHMMSS 
NSC_COMPONENT_FLAGS__HAVE_ALL 

Definition at line 510 of file time.c.

◆ nsc_time_months

Months.

Must be calender order.

Enumerator
NSC_TIME_MONTH_JAN 
NSC_TIME_MONTH_FEB 
NSC_TIME_MONTH_MAR 
NSC_TIME_MONTH_APR 
NSC_TIME_MONTH_MAY 
NSC_TIME_MONTH_JUN 
NSC_TIME_MONTH_JUL 
NSC_TIME_MONTH_AUG 
NSC_TIME_MONTH_SEP 
NSC_TIME_MONTH_OCT 
NSC_TIME_MONTH_NOV 
NSC_TIME_MONTH_DEC 
NSC_TIME_MONTH__COUNT 

Definition at line 61 of file time.c.

◆ nsc_time_weekdays

Weekdays.

Must be calender order.

Enumerator
NSC_TIME_WEEKDAY_SUN 
NSC_TIME_WEEKDAY_MON 
NSC_TIME_WEEKDAY_TUE 
NSC_TIME_WEEKDAY_WED 
NSC_TIME_WEEKDAY_THU 
NSC_TIME_WEEKDAY_FRI 
NSC_TIME_WEEKDAY_SAT 
NSC_TIME_WEEKDAY__COUNT 

Definition at line 46 of file time.c.

◆ nsc_time_zone_offsets

Timezone offsets in mins.

Order doesn't matter.

Enumerator
NSC_TIME_ZONE_OFFSET_IDLE 
NSC_TIME_ZONE_OFFSET_NZST 
NSC_TIME_ZONE_OFFSET_NZT 
NSC_TIME_ZONE_OFFSET_EAST 
NSC_TIME_ZONE_OFFSET_GST 
NSC_TIME_ZONE_OFFSET_JST 
NSC_TIME_ZONE_OFFSET_CCT 
NSC_TIME_ZONE_OFFSET_WAST 
NSC_TIME_ZONE_OFFSET_EET 
NSC_TIME_ZONE_OFFSET_CET 
NSC_TIME_ZONE_OFFSET_FWT 
NSC_TIME_ZONE_OFFSET_MET 
NSC_TIME_ZONE_OFFSET_MEWT 
NSC_TIME_ZONE_OFFSET_GMT 
NSC_TIME_ZONE_OFFSET_UTC 
NSC_TIME_ZONE_OFFSET_WET 
NSC_TIME_ZONE_OFFSET_WAT 
NSC_TIME_ZONE_OFFSET_AST 
NSC_TIME_ZONE_OFFSET_EST 
NSC_TIME_ZONE_OFFSET_CST 
NSC_TIME_ZONE_OFFSET_MST 
NSC_TIME_ZONE_OFFSET_PST 
NSC_TIME_ZONE_OFFSET_YST 
NSC_TIME_ZONE_OFFSET_AHST 
NSC_TIME_ZONE_OFFSET_CAT 
NSC_TIME_ZONE_OFFSET_HST 
NSC_TIME_ZONE_OFFSET_IDLW 
NSC_TIME_ZONE_OFFSET_NZDT 
NSC_TIME_ZONE_OFFSET_EADT 
NSC_TIME_ZONE_OFFSET_WADT 
NSC_TIME_ZONE_OFFSET_CEST 
NSC_TIME_ZONE_OFFSET_FST 
NSC_TIME_ZONE_OFFSET_MEST 
NSC_TIME_ZONE_OFFSET_MESZ 
NSC_TIME_ZONE_OFFSET_BST 
NSC_TIME_ZONE_OFFSET_ADT 
NSC_TIME_ZONE_OFFSET_EDT 
NSC_TIME_ZONE_OFFSET_CDT 
NSC_TIME_ZONE_OFFSET_MDT 
NSC_TIME_ZONE_OFFSET_PDT 
NSC_TIME_ZONE_OFFSET_YDT 
NSC_TIME_ZONE_OFFSET_HDT 
NSC_TIME_ZONE_OFFSET_Y 
NSC_TIME_ZONE_OFFSET_X 
NSC_TIME_ZONE_OFFSET_W 
NSC_TIME_ZONE_OFFSET_V 
NSC_TIME_ZONE_OFFSET_U 
NSC_TIME_ZONE_OFFSET_T 
NSC_TIME_ZONE_OFFSET_S 
NSC_TIME_ZONE_OFFSET_R 
NSC_TIME_ZONE_OFFSET_Q 
NSC_TIME_ZONE_OFFSET_P 
NSC_TIME_ZONE_OFFSET_O 
NSC_TIME_ZONE_OFFSET_N 
NSC_TIME_ZONE_OFFSET_Z 
NSC_TIME_ZONE_OFFSET_A 
NSC_TIME_ZONE_OFFSET_B 
NSC_TIME_ZONE_OFFSET_C 
NSC_TIME_ZONE_OFFSET_D 
NSC_TIME_ZONE_OFFSET_E 
NSC_TIME_ZONE_OFFSET_F 
NSC_TIME_ZONE_OFFSET_G 
NSC_TIME_ZONE_OFFSET_H 
NSC_TIME_ZONE_OFFSET_I 
NSC_TIME_ZONE_OFFSET_K 
NSC_TIME_ZONE_OFFSET_L 
NSC_TIME_ZONE_OFFSET_M 

Definition at line 207 of file time.c.

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

Enumerator
NSC_TIME_ZONE_GMT 

"GMT" first since its the only one I've seen in the wild.

– tlsa

NSC_TIME_ZONE_IDLE 
NSC_TIME_ZONE_NZST 
NSC_TIME_ZONE_NZT 
NSC_TIME_ZONE_EAST 
NSC_TIME_ZONE_GST 
NSC_TIME_ZONE_JST 
NSC_TIME_ZONE_CCT 
NSC_TIME_ZONE_WAST 
NSC_TIME_ZONE_EET 
NSC_TIME_ZONE_CET 
NSC_TIME_ZONE_FWT 
NSC_TIME_ZONE_MET 
NSC_TIME_ZONE_MEWT 
NSC_TIME_ZONE_UTC 
NSC_TIME_ZONE_WET 
NSC_TIME_ZONE_WAT 
NSC_TIME_ZONE_AST 
NSC_TIME_ZONE_EST 
NSC_TIME_ZONE_CST 
NSC_TIME_ZONE_MST 
NSC_TIME_ZONE_PST 
NSC_TIME_ZONE_YST 
NSC_TIME_ZONE_AHST 
NSC_TIME_ZONE_CAT 
NSC_TIME_ZONE_HST 
NSC_TIME_ZONE_IDLW 
NSC_TIME_ZONE_NZDT 
NSC_TIME_ZONE_EADT 
NSC_TIME_ZONE_WADT 
NSC_TIME_ZONE_CEST 
NSC_TIME_ZONE_FST 
NSC_TIME_ZONE_MEST 
NSC_TIME_ZONE_MESZ 
NSC_TIME_ZONE_BST 
NSC_TIME_ZONE_ADT 
NSC_TIME_ZONE_EDT 
NSC_TIME_ZONE_CDT 
NSC_TIME_ZONE_MDT 
NSC_TIME_ZONE_PDT 
NSC_TIME_ZONE_YDT 
NSC_TIME_ZONE_HDT 
NSC_TIME_ZONE_Y 
NSC_TIME_ZONE_X 
NSC_TIME_ZONE_W 
NSC_TIME_ZONE_V 
NSC_TIME_ZONE_U 
NSC_TIME_ZONE_T 
NSC_TIME_ZONE_S 
NSC_TIME_ZONE_R 
NSC_TIME_ZONE_Q 
NSC_TIME_ZONE_P 
NSC_TIME_ZONE_O 
NSC_TIME_ZONE_N 
NSC_TIME_ZONE_Z 
NSC_TIME_ZONE_A 
NSC_TIME_ZONE_B 
NSC_TIME_ZONE_C 
NSC_TIME_ZONE_D 
NSC_TIME_ZONE_E 
NSC_TIME_ZONE_F 
NSC_TIME_ZONE_G 
NSC_TIME_ZONE_H 
NSC_TIME_ZONE_I 
NSC_TIME_ZONE_K 
NSC_TIME_ZONE_L 
NSC_TIME_ZONE_M 
NSC_TIME_ZONE__COUNT 

Definition at line 289 of file time.c.

Function Documentation

◆ flags_chk()

static bool flags_chk ( enum nsc_date_component_flags  flags,
enum nsc_date_component_flags  mask 
)
static

Helper for testing whether any of the flags in mask are set.

Parameters
[in]flagsFlags to to check.
[in]maskThe set of flags to check for in flags.
Returns
true iff any flags in 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().

Here is the caller graph for this function:

◆ flags_chk_all()

static bool flags_chk_all ( enum nsc_date_component_flags  flags,
enum nsc_date_component_flags  mask 
)
static

Helper for testing whether all of the flags in mask are set.

Parameters
[in]flagsFlags to to check.
[in]maskThe set of flags to check for in flags.
Returns
true iff all flags in mask are set in flags, else false.

Definition at line 574 of file time.c.

Referenced by time__get_date().

Here is the caller graph for this function:

◆ nsc_snptimet()

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.

Parameters
strThe source buffer.
sizeThe length of the source buffer.
timepPointer to result.
Returns
NSERROR_OK on success or error code on faliure.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nsc_sntimet()

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.

Parameters
strThe destination buffer.
sizeThe length of the destination buffer.
timepThe pointer to the time to write.
Returns
The length of the string written.

Definition at line 126 of file time.c.

Referenced by llcache_serialise_metadata(), and urldb_write_timet().

Here is the caller graph for this function:

◆ nsc_strntimet()

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:

  • RFC 822 (updated in RFC 1123) using time zone name or time zone delta
  • RFC 850 (obsoleted by RFC 1036)
  • ANSI C's asctime() format.
Parameters
[in]strThe datetime string to parse
[in]sizeThe length of the source string
[out]timepPointer to result on success unmodified on error.
Returns
NSERROR_OK on success and timep updated else NSERROR_INVALID if the string parsing failed otherwise a suitable error code

Definition at line 980 of file time.c.

References time__get_date().

Referenced by llcache_fetch_header_cache_control(), and urldb_parse_avpair().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rfc1123_date()

const char * rfc1123_date ( time_t  t)

Create an RFC 1123 compliant date string from a Unix timestamp.

Parameters
tThe timestamp to consider
Returns
Pointer to buffer containing string - invalidated by next call.

Definition at line 110 of file time.c.

References months, and weekdays_short.

Referenced by llcache_object_refetch().

Here is the caller graph for this function:

◆ time__ctx_to_time_t()

static nserror time__ctx_to_time_t ( const struct nsc_date_parse_ctx ctx,
enum nsc_date_component_flags  flags,
time_t *  time 
)
static

Helper to convert a date string context to a time_t.

Parameters
[in]ctxCurrent date parsing context.
[in]flagsFlags indicating which date components have been set.
[out]timeReturns the number of seconds since 1 Jan 1970 00:00 UTC.
Returns
NSERROR_OK on success, appropriate error otherwise.

< 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ time__get_date()

static nserror time__get_date ( const char *  str,
time_t *  time 
)
static

Parse a date string to a time_t.

Parameters
[in]strString to parse.
[out]timeReturns the number of seconds since 1 Jan 1970 00:00 UTC.
Returns
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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ time__get_leap_days()

static int time__get_leap_days ( int  year)
inlinestatic

Get number of leap days up until end of given year.

Parameters
[in]yearYear to count leap years up to.
Returns
Number of leap days up to end of year.

Definition at line 830 of file time.c.

Referenced by time__ctx_to_time_t().

Here is the caller graph for this function:

◆ time__parse_hh_mm_ss()

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 
)
inlinestatic

Attempt to parse an "hh:mm:ss" time from a string.

Parameters
[in]strString to parse a time in.
[in,out]lenThe number of characters until the first non-digit. Iff a time component is found, updated to the number of comsumend characters.
[in,out]flagsFlags 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]ctxCurrent date parsing context. If time component is found, the context's time values are set.
Returns
true iff time component is found, else false.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ time__parse_month()

static bool time__parse_month ( const char *  str,
size_t  len,
enum nsc_date_component_flags flags,
struct nsc_date_parse_ctx ctx 
)
inlinestatic

Attempt to parse a month name in a string (case insensitive).

Parameters
[in]strString to parse a month name in.
[in]lenNumber of consecutive alphabetical characters.
[in,out]flagsFlags indicating which date components have been found in str already. If a month component is found, the month flag is set.
[in,out]ctxCurrent date parsing context. If month component is found, the context's month value is set.
Returns
true iff month name component is found, else false.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ time__parse_number()

static bool time__parse_number ( const char *  str,
size_t  len,
enum nsc_date_component_flags flags,
struct nsc_date_parse_ctx ctx 
)
inlinestatic

Attempt to parse a number from a date string.

How the number is treated depends on various things:

  • its character length,
  • its value,
  • which date components have already been parsed
Parameters
[in]strString to parse a time in.
[in]lenThe number of characters until the first non-digit.
[in,out]flagsFlags indicating which date components have been found in str already. If any component is found, their flags are set.
[in,out]ctxCurrent date parsing context. If any component is found, the appropriate context values are set.
Returns
true iff a component is found, else false.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ time__parse_timezone()

static bool time__parse_timezone ( const char *  str,
size_t  len,
enum nsc_date_component_flags flags,
struct nsc_date_parse_ctx ctx 
)
inlinestatic

Attempt to parse a timezone name in a string (case insensitive).

Parameters
[in]strString to parse a timezone name in.
[in]lenNumber of consecutive alphabetical characters.
[in,out]flagsFlags indicating which date components have been found in str already. If a timezone component is found, the timezone flag is set.
[in,out]ctxCurrent date parsing context. If timezone component is found, the context's timezone value is set.
Returns
true iff timezone name component is found, else false.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ time__parse_weekday()

static bool time__parse_weekday ( const char *  str,
size_t  len,
enum nsc_date_component_flags flags 
)
inlinestatic

Test for a weekday name in a string (case insensitive).

Parameters
[in]strString to parse a weekday name in.
[in]lenNumber of consecutive alphabetical characters.
[in,out]flagsFlags indicating which date components have been found in str already. If a weekday component is found, the weekday flag is set.
Returns
true iff weekday component is found, else false.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ months

const char* const months[NSC_TIME_MONTH__COUNT]
static
Initial value:
= {
[NSC_TIME_MONTH_JAN] = "Jan",
[NSC_TIME_MONTH_FEB] = "Feb",
[NSC_TIME_MONTH_MAR] = "Mar",
[NSC_TIME_MONTH_APR] = "Apr",
[NSC_TIME_MONTH_MAY] = "May",
[NSC_TIME_MONTH_JUN] = "Jun",
[NSC_TIME_MONTH_JUL] = "Jul",
[NSC_TIME_MONTH_AUG] = "Aug",
[NSC_TIME_MONTH_SEP] = "Sep",
[NSC_TIME_MONTH_OCT] = "Oct",
[NSC_TIME_MONTH_NOV] = "Nov",
}
@ NSC_TIME_MONTH_AUG
Definition: time.c:69
@ NSC_TIME_MONTH_MAR
Definition: time.c:64
@ NSC_TIME_MONTH_JUL
Definition: time.c:68
@ NSC_TIME_MONTH_DEC
Definition: time.c:73
@ NSC_TIME_MONTH_MAY
Definition: time.c:66
@ NSC_TIME_MONTH_JUN
Definition: time.c:67
@ NSC_TIME_MONTH_OCT
Definition: time.c:71
@ NSC_TIME_MONTH_FEB
Definition: time.c:63
@ NSC_TIME_MONTH_JAN
Definition: time.c:62
@ NSC_TIME_MONTH_SEP
Definition: time.c:70
@ NSC_TIME_MONTH_APR
Definition: time.c:65
@ NSC_TIME_MONTH_NOV
Definition: time.c:72

Array of month names.

Definition at line 93 of file time.c.

Referenced by rfc1123_date(), and time__parse_month().

◆ timezone_mins

const int16_t timezone_mins[NSC_TIME_ZONE__COUNT]
static

Array of minute offsets for timezones.

Definition at line 364 of file time.c.

Referenced by time__parse_timezone().

◆ timezones

const char* const timezones[NSC_TIME_ZONE__COUNT]
static

Array of timezone names.

Order does not matter.

Definition at line 437 of file time.c.

Referenced by time__parse_timezone().

◆ weekdays_long

const char* const weekdays_long[NSC_TIME_WEEKDAY__COUNT]
static
Initial value:
= {
[NSC_TIME_WEEKDAY_SUN] = "Sunday",
[NSC_TIME_WEEKDAY_MON] = "Monday",
[NSC_TIME_WEEKDAY_TUE] = "Tuesday",
[NSC_TIME_WEEKDAY_WED] = "Wednesday",
[NSC_TIME_WEEKDAY_THU] = "Thursday",
[NSC_TIME_WEEKDAY_FRI] = "Friday",
[NSC_TIME_WEEKDAY_SAT] = "Saturday"
}
@ NSC_TIME_WEEKDAY_MON
Definition: time.c:48
@ NSC_TIME_WEEKDAY_FRI
Definition: time.c:52
@ NSC_TIME_WEEKDAY_THU
Definition: time.c:51
@ NSC_TIME_WEEKDAY_SAT
Definition: time.c:53
@ NSC_TIME_WEEKDAY_TUE
Definition: time.c:49
@ NSC_TIME_WEEKDAY_SUN
Definition: time.c:47
@ NSC_TIME_WEEKDAY_WED
Definition: time.c:50

Array of long weekday names.

Definition at line 192 of file time.c.

Referenced by time__parse_weekday().

◆ weekdays_short

const char* const weekdays_short[NSC_TIME_WEEKDAY__COUNT]
static
Initial value:

Array of short weekday names.

Definition at line 81 of file time.c.

Referenced by rfc1123_date(), and time__parse_weekday().