gpnalin / module-date-time
N/A
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Type:magento2-module
Requires
- php: ~8.1.0||~8.2.0||~8.3.0
- magento/framework: *
README
Overview
The Aligent DateTime API is a Magento 2 module that provides functionality to calculate the difference between two dates. It supports various calculation types and can be accessed via both REST API and GraphQL.
Features
- Calculate the difference between two dates in:
days
: Calculate the total number of daysweekdays
: Calculate the number of weekdays (Monday to Friday)weeks
: Calculate the number of complete weekshours
: Calculate the total number of hoursminutes
: Calculate the total number of minutesseconds
: Calculate the total number of secondsyears
: Calculate the number of years
- Support for standard datetime formats with timezone
- REST API endpoint
- GraphQL query
- Unit test and API/GraphQL functional testing
Installation
- If you already have Magento instance setup, skip to #6.
- Create your project directory then go into it:
mkdir magento.test; cd $_;
- Download the Docker Compose template:
curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/template | bash
- Download the version of Magento you want to use with:
bin/download 2.4.7 community
- Run the setup installer for Magento:
bin/setup magento.test
- Install the module:
bin/composer require gpnalin/module-date-time # or clone the module to app/code/Aligent/DateTime mkdir -p app/code/Aligent; cd $_; git clone git@github.com:gpnalin/module-date-time.git DateTime;
- Enable the module by running:
bin/magento module:enable Aligent_DateTime
- Run the Magento setup upgrade:
bin/magento setup:upgrade
- Compile Dependency Injection:
bin/magento setup:di:compile
- Clean the cache:
bin/magento cache:clean
Usage
REST API
Endpoint
POST /V1/datetime/calculate
Parameters
startDate
(string): The start date in ISO 8601 format (e.g., "2023-01-01T00:00:00+00:00")endDate
(string): The end date in ISO 8601 format (e.g., "2023-01-10T00:00:00+00:00")calculationType
(string): The type of calculation to perform (days, weekdays, weeks, hours, minutes, seconds, years)
Example Request
POST /V1/datetime/calculate Content-Type: application/json { "startDate": "2023-01-01T00:00:00+00:00", "endDate": "2023-01-10T00:00:00+00:00", "calculationType": "days" }
Example Response
{ "result": 9 }
GraphQL
Query
query DiffCalculatorQuery( $startDate: String!, $endDate: String!, $calculationType: CalculationType! ) { DiffCalculatorQuery( startDate: $startDate, endDate: $endDate, calculationType: $calculationType ) { result } }
Variables
{ "startDate": "2023-01-01T00:00:00+00:00", "endDate": "2023-01-10T00:00:00+00:00", "calculationType": "days" }
Example Response
{ "data": { "DiffCalculatorQuery": { "result": 9 } } }
Error Handling
Both the REST API and GraphQL query will return appropriate error messages if the input is invalid or if an unexpected error occurs during calculation.
Notes
- All dates should be provided in standard datetime formats with timezone information.
- If no timezone is specified, UTC is assumed.
- The
endDate
must be greater than or equal to thestartDate
.
Test Coverage
- Unit Test
/usr/local/bin/php -dmemory_limit=-1 $(pwd)/vendor/bin/phpunit --bootstrap $(pwd)/dev/tests/unit/framework/bootstrap.php --configuration $(pwd)/dev/tests/unit/phpunit.xml.dist $(pwd)/vendor/gpnalin/module-date-time/Test/Unit/
- Web API Functional Test
/usr/local/bin/php -dmemory_limit=-1 $(pwd)/vendor/bin/phpunit -c $(pwd)/dev/tests/api-functional/phpunit_rest.xml.dist $(pwd)/vendor/gpnalin/module-date-time/Test/Api/
- GraphQL Functional Test
/usr/local/bin/php -dmemory_limit=-1 $(pwd)/vendor/bin/phpunit -c $(pwd)/dev/tests/api-functional/phpunit_graphql.xml.dist $(pwd)/vendor/gpnalin/module-date-time/Test/GraphQl/