Tovi Jaeschke-Rogers c55303af1f | 4 months ago | |
---|---|---|
app | 4 months ago | |
bootstrap | 4 months ago | |
config | 4 months ago | |
database | 4 months ago | |
public | 4 months ago | |
resources | 4 months ago | |
routes | 4 months ago | |
storage | 4 months ago | |
tests | 4 months ago | |
.editorconfig | 4 months ago | |
.env.example | 4 months ago | |
.gitattributes | 4 months ago | |
.gitignore | 4 months ago | |
README.md | 4 months ago | |
artisan | 4 months ago | |
composer.json | 4 months ago | |
composer.lock | 4 months ago | |
docker-compose.yml | 4 months ago | |
package.json | 4 months ago | |
phpunit.xml | 4 months ago | |
vite.config.js | 4 months ago |
This Laravel application calculates the duration between two provided dates, and outputs the number of days, week days, and weeks.
A conversion parameter can also be provided to the endpoint to convert the result to seconds, minutes, hours, or years.
I've used Laravel for this, as its PHP, and something I am familiar with.
The endpoint is a POST request to /calculate-duration
, this was done in
order to leverage Laravel's validation rules. This also allows the end user to
specify their data in JSON, which can be easier than managing query parameters.
Due to the specifications detailing that the duration should be initially calculated in days, week days, and weeks, and then convert it to the another value, the timezone requirement seemed redundant. This is because the most granular interval the specifications allows is a day. If, after more discussion, the requirement changed to needing the seconds between two datetimes, I would have changed the CalculateDuration service class to calculate the duration with the granularity of a second.
The weeks are calculated using $start->diff($end)
, as opposed to using the
PHP DateInterval
and DatePeriod
classes. This is primarily due to the
rounding issues I experienced during dev, i.e 2024-08-01 2024-08-17 equals 4
weeks, as the duration exists over 3 weeks, but does not span the whole of
the 3 weeks. The duration of the interval is then also added $this->end
property during the calculation (in order to include the final day in the
calculation when using P1D for the DateInterval
).
routes/web.php
app/Http/Controllers/CalculateDurationController.php
app/Http/Requests/CalculateDurationRequest.php
app/Http/Resources/CalculateDurationResource.php
app/Services/CalculateDuration.php
DurationModifier
if requiredapp/Enums/DurationModifier.php
convert_to
POST request parameterapp/DTO/DurationResult.php
tests/*
Unit and integration tests.
There are two things I would probably improve, if this was intended for production
I'd like to avoid the ternary on CalculateDuration.php:56, although I couldn't think of a quick way to simplify it. Its been a long day.
Tests relating to timezones are lacking, due to the aforementioned redundancy.
All is well, as all is dockerized.
composer install
./vendor/bin/sail up
./vendor/bin/sail test
You can the convert_to value to second
, minute
, hour
, year
, or omit the key entirely.
read -r -d '' VAR << EOM
{
"start": {
"date": "2024-08-01"
},
"end": {
"date": "2024-08-21"
},
"convert_to": null
}
EOM
curl -v \
--data "${VAR}" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
"http://localhost:80/calculate-duration"