Skip to content

Commit

Permalink
Use rfc3339 instead of unix seconds on the socket
Browse files Browse the repository at this point in the history
  • Loading branch information
smrtrfszm committed Nov 3, 2024
1 parent 10bef7d commit b004914
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
20 changes: 14 additions & 6 deletions src/handlers/competition/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn set_time_patch<S: StateTrait>(
) -> Result<StatusCode> {
let txn = state.db().begin().await?;

if let Some(start_time) = req.start_time {
let start_time = if let Some(start_time) = req.start_time {
let Some(time) = DateTime::from_timestamp(start_time, 0) else {
error!("start_time seconds out of range!");
return Err(error::TIME_SECONDS_OUT_OF_RANGE);
Expand All @@ -35,9 +35,13 @@ pub async fn set_time_patch<S: StateTrait>(
};

times::Entity::update(model).exec(&txn).await?;
}

if let Some(end_time) = req.end_time {
Some(time)
} else {
None
};

let end_time = if let Some(end_time) = req.end_time {
let Some(time) = DateTime::from_timestamp(end_time, 0) else {
error!("end_time seconds out of range!");
return Err(error::TIME_SECONDS_OUT_OF_RANGE);
Expand All @@ -49,15 +53,19 @@ pub async fn set_time_patch<S: StateTrait>(
};

times::Entity::update(model).exec(&txn).await?;
}

Some(time)
} else {
None
};

state
.nats()
.publish(
topics::times(),
serde_json::to_vec(&Event::UpdateTime {
start_time: req.start_time,
end_time: req.end_time,
start_time,
end_time,
})
.unwrap()
.into(),
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ pub enum Event {
},
DisbandTeam,
UpdateTime {
start_time: Option<i64>,
end_time: Option<i64>,
start_time: Option<DateTime<Utc>>,
end_time: Option<DateTime<Utc>>,
},
SolutionSet {
problem: Uuid,
Expand Down Expand Up @@ -256,8 +256,8 @@ async fn send_times<S: StateTrait>(state: &S, socket: &mut WebSocket) -> Result<
socket
.send(Message::Text(
serde_json::to_string(&Event::UpdateTime {
start_time: Some(start_time.time.timestamp()),
end_time: Some(end_time.time.timestamp()),
start_time: Some(start_time.time),
end_time: Some(end_time.time),
})
.unwrap(),
))
Expand Down
4 changes: 2 additions & 2 deletions tests/competition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ mod time {
json!({
"event": "UPDATE_TIME",
"data": {
"start_time": 1234,
"end_time": 4321,
"start_time": "1970-01-01T00:20:34Z",
"end_time": "1970-01-01T01:12:01Z",
}
})
);
Expand Down

0 comments on commit b004914

Please sign in to comment.