An open API service indexing awesome lists of open source software.

https://github.com/barcek/httpdt

worldwide when | easy-audit datetime library for HTTP | Rust (std only)
https://github.com/barcek/httpdt

backend datetime gmt http imf-fixdate library rust web-development

Last synced: 5 months ago
JSON representation

worldwide when | easy-audit datetime library for HTTP | Rust (std only)

Awesome Lists containing this project

README

          

# httpdt

A datetime library for HTTP clients and servers.

Generates timestamps for use in the HTTP Date header, the only format required for implementation of HTTP.

Calculates with a focus on clarity from `SystemTime`, with no external dependencies, and provides for updates to previously generated datetimes for speed.

Available via [crates.io](https://crates.io/crates/httpdt):

```shell
cargo add httpdt@0.1.0
```

## Why?

For a smaller surface area and better understanding of the underlying logic when implementing a client or server. No need to audit a more extensive datetime crate to generate a single relatively straightforward output.

## How?

Instantiate a `Datetime` struct with the `new` method, then get the current timestamp for the 'Date' header field with `for_header`:

```rust
use httpdt::Datetime;

let timestamp = Datetime::new()?
.for_header();
```

To reduce computation, an initial instance can be used as the basis for successive new timestamps via the `now` method:

```rust
use httpdt::Datetime;

let dt = Datetime::new()?;

let ts_initial = dt
.for_header();
// ...
let ts_updated = dt
.now()?
.for_header();
```

The `default` method provides a `Datetime` instance corresponding to the Unix epoch, the `raw` method the number of seconds since the epoch.

### Docs

The documentation is available at [docs.rs](https://docs.rs/httpdt/latest). It can be built and viewed locally in the browser with the following command:

```shell
cargo doc --open
```

## Making changes

Running the tests after making changes and adding tests to cover new behaviour is recommended.

### Tests

The unit tests and documentation example can be run with the following command:

```shell
cargo test
```

The unit test cases for each component are in the test module at the base of the corresponding source file.

## Development plan

The following are the expected next steps in the development of the code base. The general medium-term aim is a clear, robust and efficient datetime resource for fuller HTTP implementations. Pull requests are welcome for these and other potential improvements.

- implement a top-level error type
- revisit cross-component integer typing
- document and expose the individual components
- handle timezones, allowing for generation of server log entries in Common Log Format
- revise `SystemTime`-dependent testing
- extend test modules