|
Tor 0.5.0.0-alpha-dev
|
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>#include <errno.h>Go to the source code of this file.
Macros | |
| #define | TIME_FMT_PRIVATE |
| #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) |
| ATTR_UNUSED STATIC int | tor_timegm_impl (const struct tm *tm, time_t *time_out) |
| 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 [] |
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.
| #define IS_LEAPYEAR | ( | y | ) | (!(y % 4) && ((y % 100) || !(y % 400))) |
Yield true iff y is a leap-year.
Definition at line 79 of file time_fmt.c.
| #define TIME_FMT_PRIVATE |
Definition at line 16 of file time_fmt.c.
| 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 326 of file time_fmt.c.
Referenced by accounting_set_wakeup_time(), addressmap_get_mappings(), bridgeauth_dump_bridge_status_to_file(), bwhist_get_one_bandwidth_line(), cert_is_valid(), conn_stats_format(), control_event_address_mapped(), dirserv_add_multiple_descriptors(), dirserv_generate_networkstatus_vote_obj(), dirvote_add_vote(), download_status_to_string(), dump_microdescriptor(), extrainfo_dump_to_string_header_helper(), format_iso_time_nospace(), format_networkstatus_vote(), geoip_format_bridge_stats(), geoip_format_dirreq_stats(), geoip_format_entry_stats(), get_state_valid_until_time(), getinfo_helper_accounting(), getinfo_helper_format_single_entry_guard(), getinfo_helper_networkstatus(), hibernate_end_time_elapsed(), keypin_open_journal(), load_downloaded_routers(), networkstatus_compute_consensus(), networkstatus_get_detached_signatures(), read_bandwidth_usage(), rep_hist_format_buffer_stats(), rep_hist_format_desc_stats(), rep_hist_format_exit_stats(), rep_hist_format_hs_stats(), rep_hist_get_overload_general_line(), rep_hist_get_overload_stats_lines(), rep_hist_record_mtbf_data(), router_dump_router_to_string(), routerstatus_format_entry(), sr_state_update(), update_consensus_router_descriptor_downloads(), and warn_early_consensus().
| 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 344 of file time_fmt.c.
Referenced by cdm_cache_lookup_consensus(), consensus_queue_compression_work(), entry_guard_encode_for_state(), format_iso_time_nospace_usec(), get_inner_encrypted_layer_plaintext(), and getinfo_helper_current_time().
| 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 354 of file time_fmt.c.
Referenced by circuit_describe_status_for_controller(), control_event_circ_bandwidth_used_for_circ(), control_event_circuit_status_minor(), control_event_stream_bandwidth(), and control_event_stream_bandwidth_used().
| 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 316 of file time_fmt.c.
Referenced by accounting_set_wakeup_time(), circuit_build_times_network_close(), circuit_log_ancient_one_hop_circuits(), consider_hibernation(), control_event_address_mapped(), disk_state_save_to_disk(), entry_guard_consider_retry(), format_local_iso_time_nospace(), log_ed_cert_expiration(), or_state_save(), rep_hist_note_router_reachable(), rep_hist_note_router_unreachable(), rotate_pow_seeds(), set_rotation_time(), update_consensus_networkstatus_fetch_time_impl(), upload_descriptor_to_all(), and warn_consensus_is_not_reasonably_live().
| 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 335 of file time_fmt.c.
Referenced by getinfo_helper_current_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 213 of file time_fmt.c.
Referenced by directory_send_command(), write_http_response_header_impl(), write_metrics_http_response(), and write_short_http_response().
| 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 512 of file time_fmt.c.
Referenced by clock_skew_warning(), and warn_early_consensus().
|
static |
Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2.
Definition at line 82 of file time_fmt.c.
Referenced by tor_timegm_impl().
| 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 440 of file time_fmt.c.
Referenced by directory_handle_command_get().
| 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 423 of file time_fmt.c.
Referenced by authority_cert_parse_from_string(), extrainfo_parse_entry_from_string(), load_stats_file(), microdesc_parse_fields(), networkstatus_parse_detached_signatures(), networkstatus_parse_vote_from_string(), parse_possibly_bad_iso_time(), rep_hist_load_mtbf_data(), router_parse_entry_from_string(), and tor_get_approx_release_date().
| 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 367 of file time_fmt.c.
Referenced by parse_iso_time(), and 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 432 of file time_fmt.c.
Referenced by consdiffmgr_cleanup(), consensus_cache_entry_get_fresh_until(), consensus_cache_entry_get_valid_after(), consensus_cache_entry_get_valid_until(), and decode_pow_params().
| 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 237 of file time_fmt.c.
Referenced by parse_http_response().
| 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 67 of file time_fmt.c.
Referenced by format_iso_time(), format_rfc1123_time(), log_cert_lifetime(), and voting_sched_get_start_of_interval_after().
| 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 48 of file time_fmt.c.
Referenced by edge_of_accounting_period_containing(), and format_local_iso_time().
| int tor_timegm | ( | const struct tm * | tm, |
| time_t * | time_out | ||
| ) |
Compute a time_t given a struct tm. The result here should be an inverse of the system's gmtime() function. Return 0 on success, -1 on failure.
Definition at line 171 of file time_fmt.c.
Referenced by directory_handle_command_get(), parse_iso_time_(), parse_rfc1123_time(), and voting_sched_get_start_of_interval_after().
| ATTR_UNUSED STATIC int tor_timegm_impl | ( | 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 98 of file time_fmt.c.
Referenced by tor_timegm().
|
static |
Number of days per month in non-leap year; used by tor_timegm and parse_rfc1123_time.
Definition at line 91 of file time_fmt.c.
Referenced by parse_rfc1123_time(), and tor_timegm_impl().
|
static |
A c-locale array of 3-letter names of months, starting with Jan.
Definition at line 202 of file time_fmt.c.
Referenced by format_rfc1123_time(), parse_http_time(), and parse_rfc1123_time().
|
static |
A c-locale array of 3-letter names of weekdays, starting with Sun.
Definition at line 199 of file time_fmt.c.
Referenced by format_rfc1123_time().