Add variable for now_utc, format
This commit is contained in:
parent
ec226e0cab
commit
991677cd1e
23
src/utils.rs
23
src/utils.rs
@ -114,7 +114,7 @@ impl Poll {
|
|||||||
Some(Self {
|
Some(Self {
|
||||||
poll_options,
|
poll_options,
|
||||||
total_vote_count,
|
total_vote_count,
|
||||||
voting_end_timestamp
|
voting_end_timestamp,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,12 +126,13 @@ impl Poll {
|
|||||||
pub struct PollOption {
|
pub struct PollOption {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub text: String,
|
pub text: String,
|
||||||
pub vote_count: Option<u64>
|
pub vote_count: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PollOption {
|
impl PollOption {
|
||||||
pub fn parse(options: &Value) -> Option<Vec<Self>> {
|
pub fn parse(options: &Value) -> Option<Vec<Self>> {
|
||||||
Some(options
|
Some(
|
||||||
|
options
|
||||||
.as_array()?
|
.as_array()?
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|option| {
|
.filter_map(|option| {
|
||||||
@ -143,13 +144,10 @@ impl PollOption {
|
|||||||
let vote_count = option["vote_count"].as_u64();
|
let vote_count = option["vote_count"].as_u64();
|
||||||
|
|
||||||
// Construct PollOption items
|
// Construct PollOption items
|
||||||
Some(Self {
|
Some(Self { id, text, vote_count })
|
||||||
id,
|
|
||||||
text,
|
|
||||||
vote_count
|
|
||||||
})
|
})
|
||||||
})
|
.collect::<Vec<Self>>(),
|
||||||
.collect::<Vec<Self>>())
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -877,8 +875,9 @@ pub fn format_num(num: i64) -> (String, String) {
|
|||||||
// Parse a relative and absolute time from a UNIX timestamp
|
// Parse a relative and absolute time from a UNIX timestamp
|
||||||
pub fn time(created: f64) -> (String, String) {
|
pub fn time(created: f64) -> (String, String) {
|
||||||
let time = OffsetDateTime::from_unix_timestamp(created.round() as i64).unwrap_or(OffsetDateTime::UNIX_EPOCH);
|
let time = OffsetDateTime::from_unix_timestamp(created.round() as i64).unwrap_or(OffsetDateTime::UNIX_EPOCH);
|
||||||
let min = time.min(OffsetDateTime::now_utc());
|
let now = OffsetDateTime::now_utc();
|
||||||
let max = time.max(OffsetDateTime::now_utc());
|
let min = time.min(now);
|
||||||
|
let max = time.max(now);
|
||||||
let time_delta = max - min;
|
let time_delta = max - min;
|
||||||
|
|
||||||
// If the time difference is more than a month, show full date
|
// If the time difference is more than a month, show full date
|
||||||
@ -894,7 +893,7 @@ pub fn time(created: f64) -> (String, String) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if time_delta <= Duration::days(30) {
|
if time_delta <= Duration::days(30) {
|
||||||
if OffsetDateTime::now_utc() < time {
|
if now < time {
|
||||||
rel_time += " left";
|
rel_time += " left";
|
||||||
} else {
|
} else {
|
||||||
rel_time += " ago";
|
rel_time += " ago";
|
||||||
|
Loading…
Reference in New Issue
Block a user