Tor  0.4.7.0-alpha-dev
Macros | Functions | Variables
time_fmt.c File Reference

Encode and decode time in various formats. More...

#include "lib/encoding/time_fmt.h"
#include "lib/log/log.h"
#include "lib/log/escape.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"
#include "lib/string/printf.h"
#include "lib/string/scanf.h"
#include "lib/wallclock/time_to_tm.h"
#include <string.h>
#include <time.h>

Go to the source code of this file.

Macros

#define IS_LEAPYEAR(y)   (!(y % 4) && ((y % 100) || !(y % 400)))
 

Functions

struct tm * tor_localtime_r (const time_t *timep, struct tm *result)
 
struct tm * tor_gmtime_r (const time_t *timep, struct tm *result)
 
static int n_leapdays (int year1, int year2)
 
int tor_timegm (const struct tm *tm, time_t *time_out)
 
void format_rfc1123_time (char *buf, time_t t)
 
int parse_rfc1123_time (const char *buf, time_t *t)
 
void format_local_iso_time (char *buf, time_t t)
 
void format_iso_time (char *buf, time_t t)
 
void format_local_iso_time_nospace (char *buf, time_t t)
 
void format_iso_time_nospace (char *buf, time_t t)
 
void format_iso_time_nospace_usec (char *buf, const struct timeval *tv)
 
int parse_iso_time_ (const char *cp, time_t *t, int strict, int nospace)
 
int parse_iso_time (const char *cp, time_t *t)
 
int parse_iso_time_nospace (const char *cp, time_t *t)
 
int parse_http_time (const char *date, struct tm *tm)
 
int format_time_interval (char *out, size_t out_len, long interval)
 

Variables

static const int days_per_month []
 
static const char * WEEKDAY_NAMES []
 
static const char * MONTH_NAMES []
 

Detailed Description

Encode and decode time in various formats.

This module is higher-level than the conversion functions in "wallclock", and handles a larger variety of types. It converts between different time formats, and encodes and decodes them from strings.

Definition in file time_fmt.c.

Macro Definition Documentation

◆ IS_LEAPYEAR

#define IS_LEAPYEAR (   y)    (!(y % 4) && ((y % 100) || !(y % 400)))

Yield true iff y is a leap-year.

Definition at line 77 of file time_fmt.c.

Function Documentation

◆ format_iso_time()

void format_iso_time ( char *  buf,
time_t  t 
)

Set buf to the ISO8601 encoding of the GMT value of t. The buffer must be at least ISO_TIME_LEN+1 bytes long.

Definition at line 295 of file time_fmt.c.

Referenced by format_iso_time_nospace().

◆ format_iso_time_nospace()

void format_iso_time_nospace ( char *  buf,
time_t  t 
)

As format_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid embedding an internal space.

Definition at line 313 of file time_fmt.c.

Referenced by format_iso_time_nospace_usec().

◆ format_iso_time_nospace_usec()

void format_iso_time_nospace_usec ( char *  buf,
const struct timeval tv 
)

As format_iso_time_nospace, but include microseconds in decimal fixed-point format. Requires that buf be at least ISO_TIME_USEC_LEN+1 bytes long.

Definition at line 323 of file time_fmt.c.

◆ format_local_iso_time()

void format_local_iso_time ( char *  buf,
time_t  t 
)

Set buf to the ISO8601 encoding of the local value of t. The buffer must be at least ISO_TIME_LEN+1 bytes long.

(ISO8601 format is 2006-10-29 10:57:20)

Definition at line 285 of file time_fmt.c.

Referenced by format_local_iso_time_nospace().

◆ format_local_iso_time_nospace()

void format_local_iso_time_nospace ( char *  buf,
time_t  t 
)

As format_local_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid embedding an internal space.

Definition at line 304 of file time_fmt.c.

◆ format_rfc1123_time()

void format_rfc1123_time ( char *  buf,
time_t  t 
)

Set buf to the RFC1123 encoding of the UTC value of t. The buffer must be at least RFC1123_TIME_LEN+1 bytes long.

(RFC1123 format is "Fri, 29 Sep 2006 15:54:20 GMT". Note the "GMT" rather than "UTC".)

Definition at line 182 of file time_fmt.c.

◆ format_time_interval()

int format_time_interval ( char *  out,
size_t  out_len,
long  interval 
)

Given an interval in seconds, try to write it to the out_len-byte buffer in out in a human-readable form. Returns a non-negative integer on success, -1 on failure.

Definition at line 481 of file time_fmt.c.

Referenced by clock_skew_warning().

◆ n_leapdays()

static int n_leapdays ( int  year1,
int  year2 
)
static

Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2.

Definition at line 80 of file time_fmt.c.

◆ parse_http_time()

int parse_http_time ( const char *  date,
struct tm *  tm 
)

Given a date in one of the three formats allowed by HTTP (ugh), parse it into tm. Return 0 on success, negative on failure.

Definition at line 409 of file time_fmt.c.

◆ parse_iso_time()

int parse_iso_time ( const char *  cp,
time_t *  t 
)

Given an ISO-formatted UTC time value (after the epoch) in cp, parse it and store its value in *t. Return 0 on success, -1 on failure. Reject the string if any characters are present after the time.

Definition at line 392 of file time_fmt.c.

◆ parse_iso_time_()

int parse_iso_time_ ( const char *  cp,
time_t *  t,
int  strict,
int  nospace 
)

Given an ISO-formatted UTC time value (after the epoch) in cp, parse it and store its value in *t. Return 0 on success, -1 on failure. Ignore extraneous stuff in cp after the end of the time string, unless strict is set. If nospace is set, expect the YYYY-MM-DDTHH:MM:SS format.

Definition at line 336 of file time_fmt.c.

Referenced by parse_iso_time(), and parse_iso_time_nospace().

◆ parse_iso_time_nospace()

int parse_iso_time_nospace ( const char *  cp,
time_t *  t 
)

As parse_iso_time, but parses a time encoded by format_iso_time_nospace().

Definition at line 401 of file time_fmt.c.

◆ parse_rfc1123_time()

int parse_rfc1123_time ( const char *  buf,
time_t *  t 
)

Parse the (a subset of) the RFC1123 encoding of some time (in UTC) from buf, and store the result in *t.

Note that we only accept the subset generated by format_rfc1123_time above, not the full range of formats suggested by RFC 1123.

Return 0 on success, -1 on failure.

Definition at line 206 of file time_fmt.c.

◆ tor_gmtime_r()

struct tm* tor_gmtime_r ( const time_t *  timep,
struct tm *  result 
)

As gmtime_r, but defined for platforms that don't have it:

Convert *timep to a struct tm in UTC, and store the value in *result. Return the result on success, or NULL on failure.

Treat malformatted inputs or gmtime outputs as a BUG.

Definition at line 65 of file time_fmt.c.

Referenced by format_rfc1123_time(), and voting_sched_get_start_of_interval_after().

◆ tor_localtime_r()

struct tm* tor_localtime_r ( const time_t *  timep,
struct tm *  result 
)

As localtime_r, but defined for platforms that don't have it:

Convert *timep to a struct tm in local time, and store the value in *result. Return the result on success, or NULL on failure.

Treat malformatted inputs localtime outputs as a BUG.

Definition at line 46 of file time_fmt.c.

Referenced by edge_of_accounting_period_containing().

◆ tor_timegm()

int tor_timegm ( const struct tm *  tm,
time_t *  time_out 
)

Compute a time_t given a struct tm. The result is given in UTC, and does not account for leap seconds. Return 0 on success, -1 on failure.

Definition at line 96 of file time_fmt.c.

Referenced by voting_sched_get_start_of_interval_after().

Variable Documentation

◆ days_per_month

const int days_per_month[]
static
Initial value:
=
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

Number of days per month in non-leap year; used by tor_timegm and parse_rfc1123_time.

Definition at line 89 of file time_fmt.c.

Referenced by tor_timegm().

◆ MONTH_NAMES

const char* MONTH_NAMES[]
static
Initial value:
=
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }

A c-locale array of 3-letter names of months, starting with Jan.

Definition at line 171 of file time_fmt.c.

Referenced by parse_http_time().

◆ WEEKDAY_NAMES

const char* WEEKDAY_NAMES[]
static
Initial value:
=
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

A c-locale array of 3-letter names of weekdays, starting with Sun.

Definition at line 168 of file time_fmt.c.