diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 7fcd6dc..98a45b5 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -25,4 +25,4 @@ jobs: - name: Build run: dotnet build --no-restore - name: Test - run: dotnet test --no-build --verbosity normal + run: dotnet test --no-build --verbosity normal \ No newline at end of file diff --git a/Controllers/TJController.cs b/Controllers/TJController.cs index 3c121b9..1912bef 100644 --- a/Controllers/TJController.cs +++ b/Controllers/TJController.cs @@ -9,14 +9,13 @@ record TJRequest(int Period, int Year, int Duration); [ApiController] public class TJController : ControllerBase { - [HttpGet("{period:int}/{year:int}/{duration:int}")] - public IActionResult TJ(int period, int year, int duration) + [HttpGet("{period:int}/{year:int}/{duration:int?}")] + public IActionResult TJ(int period, int year, int duration = 0) { TJRequest req = new(period, year, duration); return GenerateResponse(req); } - private bool ValidateRequest(TJRequest req, out ObjectResult error) { error = BadRequest(new ErrorMessage("This shouldn't appear! Aamuja.")); @@ -52,7 +51,7 @@ private ObjectResult GenerateResponse(TJRequest req) // Assume it's the 21 century year += 2000; - TJ tj = TJGenerator.GenerateTJ(req.Period == 1, year, req.Duration); + TJ tj = TJGenerator.GenerateTJ(req.Period, year, req.Duration); return Ok(tj); } } \ No newline at end of file diff --git a/Models/TJ.cs b/Models/TJ.cs index 0f730bd..1b413c9 100644 --- a/Models/TJ.cs +++ b/Models/TJ.cs @@ -1,8 +1,10 @@ namespace TJ_API.Models; public record TJ( - int Days, + double Days, double Weeks, double Months, - double Seconds + double Seconds, + long StartDate, + long ReturnDate ); \ No newline at end of file diff --git a/Services/TJGenerator.cs b/Services/TJGenerator.cs index a1cb407..b546c17 100644 --- a/Services/TJGenerator.cs +++ b/Services/TJGenerator.cs @@ -1,30 +1,67 @@ using TJ_API.Models; +using TJ_API.Services; namespace TJ_API.Services; public static class TJGenerator { - public static TJ GenerateTJ(bool firstPeriod, int year, int duration) + /// + /// Arrival patches mapped with their starting dates. + /// + private static readonly Dictionary StartingDates = new() { - DateTime beginningDate; - if (firstPeriod) - { - // Assume first period starts January 6th - beginningDate = new(year, 1, 6); - } - else - { - // Assume second period starts July 7th - beginningDate = new(year, 7, 7); - } + {"1/2016", Utility.GenerateFinnishDate(2016, 1, 4)}, + {"2/2016", Utility.GenerateFinnishDate(2016, 7, 4)}, + {"1/2017", Utility.GenerateFinnishDate(2017, 1, 2)}, + {"2/2017", Utility.GenerateFinnishDate(2017, 7, 3)}, + {"1/2018", Utility.GenerateFinnishDate(2018, 1, 8)}, + {"2/2018", Utility.GenerateFinnishDate(2018, 7, 9)}, + {"1/2019", Utility.GenerateFinnishDate(2019, 1, 7)}, + {"2/2019", Utility.GenerateFinnishDate(2019, 7, 8)}, + {"1/2020", Utility.GenerateFinnishDate(2020, 1, 6)}, + {"2/2020", Utility.GenerateFinnishDate(2020, 7, 6)}, + {"1/2021", Utility.GenerateFinnishDate(2021, 1, 4)}, + {"2/2021", Utility.GenerateFinnishDate(2021, 7, 5)}, + {"1/2022", Utility.GenerateFinnishDate(2022, 1, 3)}, + {"2/2022", Utility.GenerateFinnishDate(2022, 7, 4)}, + {"1/2023", Utility.GenerateFinnishDate(2023, 1, 2)}, + {"2/2023", Utility.GenerateFinnishDate(2023, 7, 3)}, + {"1/2024", Utility.GenerateFinnishDate(2024, 1, 8)}, + {"2/2024", Utility.GenerateFinnishDate(2024, 7, 8)}, + {"1/2025", Utility.GenerateFinnishDate(2025, 1, 6)}, + {"2/2025", Utility.GenerateFinnishDate(2025, 7, 7)}, + {"1/2026", Utility.GenerateFinnishDate(2026, 1, 5)}, + {"2/2026", Utility.GenerateFinnishDate(2026, 7, 6)}, + }; + + private static string GenerateArrivalPatch(int period, int year) + { + return $"{period}/{year}"; + } + + public static TJ GenerateTJ(int period, int year, int duration) + { + if (!StartingDates.TryGetValue(GenerateArrivalPatch(period, year), out DateTime beginningDate)) + if (period == 1) + { + // Assume first period starts January 6th + beginningDate = new(year, 1, 6); + } + else + { + // Assume second period starts July 7th + beginningDate = new(year, 7, 7); + } DateTime endingDate = beginningDate.AddDays(duration); TimeSpan tjSpan = endingDate - DateTime.Now; return new TJ( - Days: tjSpan.Days, - Weeks: Math.Round((double)tjSpan.Days / 7f, 2), - Months: Math.Round((double)tjSpan.Days / 30f, 2), - Seconds: Math.Ceiling(tjSpan.TotalSeconds) + Days: Math.Round(tjSpan.TotalDays, 2), + Weeks: Math.Round(tjSpan.TotalDays / 7f, 2), + Months: Math.Round(tjSpan.TotalDays / 30f, 2), + Seconds: Math.Round(tjSpan.TotalSeconds, 1), + StartDate: Utility.GetUnixTimestamp(beginningDate), + ReturnDate: Utility.GetUnixTimestamp(endingDate) ); } } diff --git a/Services/Utility.cs b/Services/Utility.cs index bb81f96..758fb6a 100644 --- a/Services/Utility.cs +++ b/Services/Utility.cs @@ -2,6 +2,17 @@ namespace TJ_API.Services; public static class Utility { + private static readonly TimeZoneInfo FinnishTimeZone = TimeZoneInfo.FindSystemTimeZoneById("FLE Standard Time"); + + /// + /// Generates a DateTime with the Finnish timezone + /// + public static DateTime GenerateFinnishDate(int year, int month, int day) + { + DateTime date = new(year, month, day); + return TimeZoneInfo.ConvertTimeFromUtc(date, FinnishTimeZone); + } + /// /// Calculates the length of an integer. /// Ex. 12345 -> 5 @@ -11,4 +22,12 @@ public static int IntegerLength(int num) int abs = (num == int.MinValue) ? int.MaxValue : Math.Abs(num); return (num == 0) ? 1 : (int)Math.Floor(Math.Log10(abs)) + 1; } + + /// + /// Converts given DateTime into a Unix timestamp long integer + /// + public static long GetUnixTimestamp(DateTime date) + { + return ((DateTimeOffset)date).ToUnixTimeSeconds(); + } } \ No newline at end of file