Add variable for now_utc, format
This commit is contained in:
parent
ec226e0cab
commit
991677cd1e
45
src/utils.rs
45
src/utils.rs
@ -105,7 +105,7 @@ pub struct Poll {
|
|||||||
impl Poll {
|
impl Poll {
|
||||||
pub fn parse(poll_data: &Value) -> Option<Self> {
|
pub fn parse(poll_data: &Value) -> Option<Self> {
|
||||||
poll_data.as_object()?;
|
poll_data.as_object()?;
|
||||||
|
|
||||||
let total_vote_count = poll_data["total_vote_count"].as_u64()?;
|
let total_vote_count = poll_data["total_vote_count"].as_u64()?;
|
||||||
// voting_end_timestamp is in the format of milliseconds
|
// voting_end_timestamp is in the format of milliseconds
|
||||||
let voting_end_timestamp = time(poll_data["voting_end_timestamp"].as_f64()? / 1000.0);
|
let voting_end_timestamp = time(poll_data["voting_end_timestamp"].as_f64()? / 1000.0);
|
||||||
@ -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,30 +126,28 @@ 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(
|
||||||
.as_array()?
|
options
|
||||||
.iter()
|
.as_array()?
|
||||||
.filter_map(|option| {
|
.iter()
|
||||||
// For each poll option
|
.filter_map(|option| {
|
||||||
|
// For each poll option
|
||||||
|
|
||||||
// we can't just use as_u64() because "id": String("...") and serde would parse it as None
|
// we can't just use as_u64() because "id": String("...") and serde would parse it as None
|
||||||
let id = option["id"].as_str()?.parse::<u64>().ok()?;
|
let id = option["id"].as_str()?.parse::<u64>().ok()?;
|
||||||
let text = option["text"].as_str()?.to_owned();
|
let text = option["text"].as_str()?.to_owned();
|
||||||
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,
|
.collect::<Vec<Self>>(),
|
||||||
vote_count
|
)
|
||||||
})
|
|
||||||
})
|
|
||||||
.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