Skip to content

Commit

Permalink
feat!: Add now(); remove chrono integration for now. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored Apr 5, 2024
1 parent 6aff845 commit 670ba05
Show file tree
Hide file tree
Showing 20 changed files with 329 additions and 574 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ on:
jobs:
unit-tests:
runs-on: ubuntu-latest
container: rust:1.70-alpine
steps:
- uses: actions/checkout@v2
- run: apk add alpine-sdk
- uses: actions/checkout@v4
- run: cargo test --all-features --workspace
lint:
runs-on: ubuntu-latest
container: rust:1.70-alpine
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- run: rustup toolchain install nightly
- run: rustup component add rustfmt --toolchain nightly
- run: cargo +nightly fmt -- --check
docs:
runs-on: ubuntu-latest
container: rust:1.70-alpine
steps:
- uses: actions/checkout@v2
- run: apk add alpine-sdk
- uses: actions/checkout@v4
- run: cargo doc
29 changes: 29 additions & 0 deletions .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: coverage
on:
pull_request:
branches:
- main
push:
branches:
- main
jobs:
test:
name: coverage
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install tarpaulin.
run: cargo install cargo-tarpaulin
- name: Generate code coverage
run: |
cargo tarpaulin --verbose --out xml --engine llvm --features serde,easter
env:
CARGO_NET_GIT_FETCH_WITH_CLI: true
- name: Upload to codecov.io
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: lukesneeringer/unix-ts
fail_ci_if_error: true
21 changes: 21 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: release
on:
release:
types:
- created
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Publish the `unix-ts-macros` crate.
run: cargo publish --package unix-ts-macros
env:
CARGO_NET_GIT_FETCH_WITH_CLI: true
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
- name: Publish the `unix-ts` crate.
run: cargo publish --package unix-ts
env:
CARGO_NET_GIT_FETCH_WITH_CLI: true
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
2 changes: 1 addition & 1 deletion .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
printWidth: 79
printWidth: 99
proseWrap: always
singleQuote: true
trailingComma: es5
11 changes: 3 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unix-ts"
version = "0.6.0"
version = "1.0.0"
edition = "2021"
authors = [
"Luke Sneeringer <[email protected]>"
Expand All @@ -20,15 +20,10 @@ exclude = [
]

[dependencies]
chrono = { version = "0.4", optional = true }
unix-ts-macros = { path = "macros", version = "0.4" }
unix-ts-macros = { path = "macros", version = "1" }

[dev-dependencies]
chrono-tz = "^0.8.3"
assert2 = "0.3"

[build-dependencies]
readme-rustdocifier = "0.1"

[workspace]
members = [".", "macros", "macros-test"]
members = [".", "macros"]
62 changes: 22 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
# unix-ts: Convertible Unix timestamps for Rust

unix-ts is a library for dealing with timestamps. It supports lightweight
creation of timestamps, and conversions into and out of other formats, from
integers to [chrono][] DateTime objects.
[![ci](https://github.com/lukesneeringer/unix-ts/actions/workflows/ci.yaml/badge.svg)](https://github.com/lukesneeringer/unix-ts/actions/workflows/ci.yaml)
[![codecov](https://codecov.io/gh/lukesneeringer/unix-ts/branch/main/graph/badge.svg?token=fDZ23KbbUo)](https://codecov.io/gh/lukesneeringer/unix-ts)
[![release](https://img.shields.io/crates/v/unix-ts.svg)](https://crates.io/crates/unix-ts)
[![docs](https://img.shields.io/badge/docs-release-blue)](https://docs.rs/unix-ts/)

The goal is to serve as a glue library that can take a timestamp and convert to
whatever other formats are needed.
unix-ts is a library for dealing with timestamps. It supports lightweight creation and manipulation
of timestamps.

The goal is to serve as a glue library that can take a timestamp and convert to whatever other
formats are needed.

## Usage

Add the crate to your `Cargo.toml` file like usual:

```toml
[dependencies]
unix-ts = "0.5"
unix-ts = "1"
```

You can create a timestamp with the `ts!` macro, which takes the Unix timestamp
as an argument:
You can create a timestamp with the `ts!` macro, which takes the Unix timestamp as an argument:

```rs
use unix_ts::ts;
Expand All @@ -37,51 +40,30 @@ use unix_ts::Timestamp;
let t = Timestamp::from(1335020400);
```

For milliseconds, microseconds, or nanoseconds, there are specific `from`
methods available:
For milliseconds, microseconds, or nanoseconds, there are specific `from` methods available:

```rs
use unix_ts::Timestamp;

let t = Timestamp::from_nanos(1335020400_000_000_000i64);
```

Finally, the `new` method accepts `seconds` and `nanos`. This is generally less
convenient than the macro, though, because you have to convert fractional
seconds to nanos by yourself.
Finally, the `new` method accepts `seconds` and `nanos`. This is generally less convenient than the
macro, though, because you have to convert fractional seconds to nanos by yourself.

### Reading timestamps

There are three methods available for reading timestamps:

- `seconds() -> i64`: Returns the whole seconds value of the timestamp.
- `at_precision(e) -> i128`: Returns the timestamp as an integer at greater
precision than the second. The `e` value represents the power of 10;
therefore, `at_precision(3)` would return the value in milliseconds.
- `subsec(e) -> u32`: Returns the subsecond value at the given precision. The
`e` value represents the power of 10; therefore, `subsec(3)` would return the
sub-second value in milliseconds.
- `at_precision(e) -> i128`: Returns the timestamp as an integer at greater precision than the
second. The `e` value represents the power of 10; therefore, `at_precision(3)` would return the
value in milliseconds.
- `subsec(e) -> u32`: Returns the subsecond value at the given precision. The `e` value represents
the power of 10; therefore, `subsec(3)` would return the sub-second value in milliseconds.

### Converting timestamps

Timestamps can currently be converted into integers (with the loss of the
subsecond), or `std::time::Duration`. This is done by implementing the Rust
`From` trait (so you can use the `from` or `into` methods).

If the `chrono` feature is enabled, unix-ts also supports converting to
`chrono::DateTime` and `chrono::NaiveDateTime`. This is done through the
`to_datetime` and `to_naive_datetime` methods. (A `to_utc_datetime` is also
offered to simplify time zone specification for this common case.)

## Features

All dependencies outside the standard library are optional, meaning that
unix-ts will not force you to also install, for example, [chrono][] (although
there is a good chance you should if you are dealing with time).

Optional features:

- `chrono`: Adds converstion functions to [chrono][] `DateTime` and
`NaiveDateTime`.

[chrono]: https://crates.io/crates/chrono
Timestamps can currently be converted into integers (with the loss of the subsecond), or
`std::time::Duration`. This is done by implementing the Rust `From` trait (so you can use the
`from` or `into` methods).
20 changes: 0 additions & 20 deletions build.rs

This file was deleted.

11 changes: 0 additions & 11 deletions macros-test/Cargo.toml

This file was deleted.

42 changes: 0 additions & 42 deletions macros-test/src/lib.rs

This file was deleted.

7 changes: 2 additions & 5 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unix-ts-macros"
version = "0.4.0"
version = "1.0.0"
edition = "2021"
keywords = ["date", "time", "unix-timestamp", "timestamp"]
categories = ["date-and-time"]
Expand All @@ -20,7 +20,4 @@ exclude = [
proc-macro = true

[dev-dependencies]
unix-ts = { path = "..", version = "0.6.0" }

[build-dependencies]
readme-rustdocifier = "0.1"
unix-ts = { path = "..", version = "1" }
28 changes: 3 additions & 25 deletions macros/README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
# unix-ts-macros: A macro to quickly generate unix-ts timestamps.

unix-ts-macros simplifies the creation of timestamps into a procedural macro:
`ts`. This is an implementation crate for `unix-ts`, which is what you should
actually add as a dependency.

## Usage

Add the `unix-ts` crate to your `Cargo.toml` file like usual:

```toml
[dependencies]
unix-ts = "0.2"
```

You can create a timestamp with the `ts!` macro, which takes the Unix timestamp
as an argument:

```
use unix_ts_macros::ts;
// The argument is the number of seconds since the Unix epoch.
let t = ts!(1335020400);
// Fractional seconds are also allowed.
let t2 = ts!(1335020400.25);
```
unix-ts-macros simplifies the creation of timestamps into a procedural macro: `ts`. This is an
implementation crate for `unix-ts`, which is what you should actually add as a dependency. (Don't
depend directly on this crate.)
20 changes: 0 additions & 20 deletions macros/build.rs

This file was deleted.

16 changes: 5 additions & 11 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![crate_name = "unix_ts_macros"]
#![doc = include_str!(concat!(env!("OUT_DIR"), "/README-rustdocified.md"))]

extern crate proc_macro;
//! The companion macro crate for `unix-ts`, to create Unix timestamps from integer and float
//! literals.
use proc_macro::TokenStream;

Expand Down Expand Up @@ -36,13 +34,9 @@ pub fn ts(input: TokenStream) -> TokenStream {
// If there is no decimal point, this is an integer;
// return a timestamp from it.
if !src.contains('.') {
return format!(
"::unix_ts::Timestamp::new({}{}, 0)",
if neg { '-' } else { ' ' },
src
)
.parse()
.unwrap();
return format!("::unix_ts::Timestamp::new({}{}, 0)", if neg { '-' } else { ' ' }, src)
.parse()
.unwrap();
}

// If we start with a decimal point, prepend a zero.
Expand Down
Loading

0 comments on commit 670ba05

Please sign in to comment.