Skip to content

Commit

Permalink
Local Time Zone (fixes #102) (#403)
Browse files Browse the repository at this point in the history
* Local Time Zone (fixes #102)

All times displayed in the UX for players, authors, and admins now appear in the browser's local time zone, with the exception of the event creation/properties because I don't know how to do the time zone transitions for editable fields.

Internet wisdom is that the only way to do this well is to do it in Javascript because anything done on the server will use the server timezone instead of the user timezone. I searched around for a while to locate the easiest-looking version of this, and it seems to be working properly.

* PR changes
  • Loading branch information
tabascq authored and jenetlan committed Apr 25, 2019
1 parent 5204194 commit 344af2f
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 10 deletions.
21 changes: 21 additions & 0 deletions ServerCore/Helpers/TimeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ServerCore.DataModel;

namespace ServerCore.Helpers
{
public static class TimeHelper
{
/// <summary>
/// returns a time with the formatting required for the jQuery code in site.js to convert it to browser local time
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static string LocalTime(DateTime? date)
{
return date == null ? "&nbsp;" : $"<time>{date.Value.ToString("yyyy-MM-ddTHH:mm:ssZ")}</time>";
}
}
}
5 changes: 5 additions & 0 deletions ServerCore/ModelBases/EventSpecificPageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public async Task<int> GetTeamId()
}
}

public string LocalTime(DateTime? date)
{
return TimeHelper.LocalTime(date);
}

public class EventBinder : IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
Expand Down
1 change: 1 addition & 0 deletions ServerCore/Pages/Events/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
}

<h2>Create a new event</h2>
<h3><span style="color: red">ALL TIMES UTC! Current time is @DateTime.UtcNow.ToShortTimeString()</span></h3>

<h4>Event</h4>
<hr />
Expand Down
1 change: 1 addition & 0 deletions ServerCore/Pages/Events/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
}

<h2>@Model.Event.Name: Details</h2>
<h3><span style="color: red">ALL TIMES UTC! Current time is @DateTime.UtcNow.ToShortTimeString()</span></h3>

<a asp-page="./Edit">Edit</a> | <a asp-page="./Import">Import</a> | <a asp-page="./Delete">Delete</a>

Expand Down
1 change: 1 addition & 0 deletions ServerCore/Pages/Events/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
}

<h2>@Model.Event.Name: Edit</h2>
<h3><span style="color: red">ALL TIMES UTC! Current time is @DateTime.UtcNow.ToShortTimeString()</span></h3>
<div>
<a asp-page="./Details">Cancel</a>
</div>
Expand Down
2 changes: 1 addition & 1 deletion ServerCore/Pages/Hints/AuthorIndex.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
@Html.DisplayFor(modelItem => item.Hint.Cost)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnlockTime)
@Html.Raw(Model.LocalTime(item.UnlockTime))
</td>
</tr>
}
Expand Down
2 changes: 1 addition & 1 deletion ServerCore/Pages/Puzzles/Feedback.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ else
@Html.DisplayFor(modelItem => item.Submitter.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Feedback.SubmissionTime)
@Html.Raw(Model.LocalTime(item.Feedback.SubmissionTime))
</td>
<td>
@Html.DisplayFor(modelItem => item.Feedback.WrittenFeedback)
Expand Down
4 changes: 2 additions & 2 deletions ServerCore/Pages/Puzzles/Status.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
}
else
{
@Html.DisplayFor(modelItem => item.UnlockedTime)
@Html.Raw(Model.LocalTime(item.UnlockedTime))
<a asp-page-handler="UnlockState" asp-route-sort="@Model.Sort" asp-route-teamId="@item.Team.ID" asp-route-puzzleid="@Model.Puzzle.ID" asp-route-value="false" onclick="return confirm('Are you sure you want to mark @(item.Team.Name) as locked?')"> X</a>
}
</td>
Expand All @@ -66,7 +66,7 @@
}
else
{
@Html.DisplayFor(modelItem => item.SolvedTime)
@Html.Raw(Model.LocalTime(item.SolvedTime))
<a asp-page-handler="SolveState" asp-route-sort="@Model.Sort" asp-route-teamId="@item.Team.ID" asp-route-puzzleid="@Model.Puzzle.ID" asp-route-value="false" onclick="return confirm('Are you sure you want to mark @(item.Team.Name) as unsolved?')"> X</a>
}
</td>
Expand Down
2 changes: 1 addition & 1 deletion ServerCore/Pages/Submissions/AuthorIndex.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
@Html.DisplayFor(modelItem => item.SubmissionText)
</td>
<td>
@Html.DisplayFor(modelItem => item.TimeSubmitted)
@Html.Raw(Model.LocalTime(item.TimeSubmitted))
</td>
</tr>
}
Expand Down
2 changes: 1 addition & 1 deletion ServerCore/Pages/Submissions/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TimeSubmitted)
@Html.Raw(Model.LocalTime(item.TimeSubmitted))
</td>
<td>
@Html.DisplayFor(modelItem => item.Submitter.Name)
Expand Down
2 changes: 1 addition & 1 deletion ServerCore/Pages/Teams/Answers.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{
<tr>
<td>
@item.TimeSubmitted
@Html.Raw(Model.LocalTime(item.TimeSubmitted))
</td>
<td>
@item.Group
Expand Down
2 changes: 1 addition & 1 deletion ServerCore/Pages/Teams/Play.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<a asp-page="/Submissions/Index" asp-route-puzzleId="@item.Puzzle.ID">
@if (item.State.SolvedTime != null)
{
<text>Solved at </text>@item.State.SolvedTime
<text>Solved on </text>@Html.Raw(Model.LocalTime(item.State.SolvedTime))
}
else if (@Model.Event.IsAnswerSubmissionActive)
{
Expand Down
4 changes: 2 additions & 2 deletions ServerCore/Pages/Teams/Status.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
}
else
{
@Html.DisplayFor(modelItem => item.UnlockedTime)
@Html.Raw(Model.LocalTime(item.UnlockedTime))
<a asp-page-handler="UnlockState" asp-route-sort="@Model.Sort" asp-route-puzzleId="@item.Puzzle.ID" asp-route-teamId="@Model.Team.ID" asp-route-value="false" onclick="return confirm('Are you sure you want to mark @(item.Puzzle.Name) as locked?')"> X</a>
}
</td>
Expand All @@ -65,7 +65,7 @@
}
else
{
@Html.DisplayFor(modelItem => item.SolvedTime)
@Html.Raw(Model.LocalTime(item.SolvedTime))
<a asp-page-handler="SolveState" asp-route-sort="@Model.Sort" asp-route-puzzleId="@item.Puzzle.ID" asp-route-teamId="@Model.Team.ID" asp-route-value="false" onclick="return confirm('Are you sure you want to mark @(item.Puzzle.Name) as unsolved?')"> X</a>
}
</td>
Expand Down
5 changes: 5 additions & 0 deletions ServerCore/wwwroot/js/site.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
// Write your Javascript code.
$("time").each(function (elem) {
var utctimeval = $(this).html();
var date = new Date(utctimeval);
$(this).html(date.toLocaleString());
});

0 comments on commit 344af2f

Please sign in to comment.