Skip to content

Commit

Permalink
Fix date formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicoh committed Apr 9, 2024
1 parent c08f59f commit a00894e
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 200 deletions.
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

0 comments on commit a00894e

Please sign in to comment.