Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date formatting #10

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ all-features = true

[dependencies]
chrono = { version = "0.4", optional = true }
regex = { version = "1.7", optional = true }
once_cell = { version = "1.19", optional = true }
regex = { version = "1.10", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Expand All @@ -24,4 +25,4 @@ reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }

[features]
fmt = ["dep:chrono", "dep:regex"]
fmt = ["dep:chrono", "dep:once_cell", "dep:regex"]
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,37 @@ Enable `fmt` module and format messages in [this way](https://api.slack.com/refe

```rust
use chrono::prelude::*;
use slack_messaging::fmt::DateFormat;
use slack_messaging::fmt::DateFormatter;

// Formatter without optional link.
let f = DateFormatter::builder()
.token("{date_short} at {time}")
.build();

let dt = DateTime::parse_from_rfc3339("2023-02-27T12:34:56+09:00").unwrap();
let f = DateFormat::new(dt).token("{date_short} at {time}");

assert_eq!(
format!("{f}"),
f.format(&dt),
"<!date^1677468896^{date_short} at {time}|Feb 27, 2023 at 12:34 PM>"
);

// You can also set optional link when formatting.
assert_eq!(
f.format_with_link(&dt, "https://example.com"),
"<!date^1677468896^{date_short} at {time}^https://example.com|Feb 27, 2023 at 12:34 PM>"
);

// Formatter with optional link.
let f = DateFormatter::builder()
.token("{date_short} at {time}")
.link("https://example.com")
.build();

// This time, format method returns text with link set to the formatter.
assert_eq!(
f.format(&dt),
"<!date^1677468896^{date_short} at {time}^https://example.com|Feb 27, 2023 at 12:34 PM>"
);
```

## License
Expand Down
Loading
Loading