From d6b385b5e082e28f5ae297228eef07b39d896b6a Mon Sep 17 00:00:00 2001 From: dineshuprety Date: Tue, 21 Nov 2023 12:27:50 +0545 Subject: [PATCH 1/3] Working on helper function to convert date --- composer.json | 5 ++++- src/Helper/helper.php | 21 +++++++++++++++++++++ tests/ConvertToNepaliDateTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/Helper/helper.php diff --git a/composer.json b/composer.json index 520f1b3..073960b 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,10 @@ "psr-4": { "Anuzpandey\\LaravelNepaliDate\\Tests\\": "tests/", "Workbench\\App\\": "workbench/app/" - } + }, + "files": [ + "src/Helper/helper.php" + ] }, "scripts": { "post-autoload-dump": "@composer run prepare", diff --git a/src/Helper/helper.php b/src/Helper/helper.php new file mode 100644 index 0000000..c8708a9 --- /dev/null +++ b/src/Helper/helper.php @@ -0,0 +1,21 @@ +toNepaliDate($format ?? 'Y-m-d', $locale ?? 'en'); + } +} diff --git a/tests/ConvertToNepaliDateTest.php b/tests/ConvertToNepaliDateTest.php index cd517fb..837684b 100644 --- a/tests/ConvertToNepaliDateTest.php +++ b/tests/ConvertToNepaliDateTest.php @@ -29,3 +29,28 @@ ['d F Y', 'en', '10 Baisakh 2053'], ['Y/m/d', 'np', '२०५३/०१/१०'], ]); + + +it('can convert to basic nepali date with helper function toBS', function (string $date, string $expectedResult) { + expect(toBS($date)) + ->toBe($expectedResult); +})->with([ + ['1996-04-22', '2053-01-10'], + ['1972-07-19', '2029-04-04'], + ['1966-04-02', '2022-12-20'], +]); + +it('can convert to nepali formatted result with helper function toBS', function (string $format, string $locale, string $expectedResult) { + expect(toBS("1996-04-22", $format, $locale)) + ->toBe($expectedResult); +})->with([ + ['d F Y, l', 'np', '१० वैशाख २०५३, सोमबार'], + ['d F Y, l', 'en', '10 Baisakh 2053, Monday'], + ['Y-m-d', 'np', '२०५३-०१-१०'], + ['Y-m-d', 'en', '2053-01-10'], + ['l, d F Y', 'np', 'सोमबार, १० वैशाख २०५३'], + ['l, d F Y', 'en', 'Monday, 10 Baisakh 2053'], + ['d F Y', 'np', '१० वैशाख २०५३'], + ['d F Y', 'en', '10 Baisakh 2053'], + ['Y/m/d', 'np', '२०५३/०१/१०'], +]); From f615dfde35f5a7d7ba26d159346f575361fb0294 Mon Sep 17 00:00:00 2001 From: dineshuprety Date: Tue, 21 Nov 2023 17:10:11 +0545 Subject: [PATCH 2/3] Worked on helper function --- README.md | 7 ++++++- src/Helper/helper.php | 29 ++++++++++++++++++++++++----- tests/ConvertToEnglishDateTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9a14eb7..ef88534 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,12 @@ Carbon::parse($engDate)->toNepaliDate(format: 'D, j F Y'); // Result: सोम, १० वैशाख २०५३ ``` - +### Helper function +```php +// we do have a helper function to convert dates +toBS("1996-04-22") // to convert English date to Nepali date (BS). ie. Output: 2053-01-10 +toAD("2053-01-10") // to convert Nepali date to English date (AD). ie. Output: 1996-04-22 +``` ## Testing ```bash diff --git a/src/Helper/helper.php b/src/Helper/helper.php index c8708a9..c7c1a10 100644 --- a/src/Helper/helper.php +++ b/src/Helper/helper.php @@ -2,20 +2,39 @@ use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate; -if (! function_exists('toBS')) { +if (!function_exists('toBS')) { /** - * The function converts a given date string to the Nepali date format + * The function converts a given date to the Nepali date format * * @param string date The date parameter is a string that represents the date in the Gregorian * calendar format. It should be in the format "YYYY-MM-DD" or "YYYY/MM/DD". - * @param string format The format parameter is used to specify the desired format of the Nepali + * @param string|null format The format parameter is used to specify the desired format of the Nepali * date. It is an optional parameter and if not provided, the default format will be used. - * @param string locale The "locale" parameter is used to specify the language and region + * @param string|null locale The "locale" parameter is used to specify the language and region * @return string Nepali date converted from the given date. */ - function toBS(string $date, $format = null, $locale = null): string + function toBS($date, $format = null, $locale = null): string { return LaravelNepaliDate::from($date) ->toNepaliDate($format ?? 'Y-m-d', $locale ?? 'en'); } } + + +if (!function_exists('toAD')) { + /** + * The function converts a given date from Nepali (Bikram Sambat) to English format + * + * @param string date The date parameter is a string that represents the date in the Gregorian + * calendar format. It should be in the format "YYYY-MM-DD" or "YYYY/MM/DD". + * @param string|null format The format parameter is used to specify the desired format of the English + * date. It is an optional parameter and if not provided, the default format will be used. + * @param string|null locale The "locale" parameter is used to specify the language and region + * @return string English date converted from the given Nepali Date. + */ + function toAD($date, $format = null, $locale = null): string + { + return LaravelNepaliDate::from($date) + ->toEnglishDate($format ?? 'Y-m-d', $locale ?? 'en'); + } +} diff --git a/tests/ConvertToEnglishDateTest.php b/tests/ConvertToEnglishDateTest.php index d7694a1..d404aa1 100644 --- a/tests/ConvertToEnglishDateTest.php +++ b/tests/ConvertToEnglishDateTest.php @@ -30,3 +30,28 @@ ['d F Y', 'en', '22 April 1996'], ['Y/m/d', 'np', '१९९६/०४/२२'], ]); + +it('can convert to basic nepali date to english date with helper function', function (string $date, string $expectedResult) { + expect(toAD($date)) + ->toBe($expectedResult); +})->with([ + ['2053-01-10', '1996-04-22'], + ['2029-04-04', '1972-07-19'], + ['2022-12-20', '1966-04-02'], +]); + + +it('can convert to nepali formatted result to english date with helper function', function (string $format, string $locale, string $expectedResult) { + expect(toAD("2053-01-10", $format, $locale)) + ->toBe($expectedResult); +})->with([ + ['d F Y, l', 'np', '२२ अप्रिल १९९६, सोमबार'], + ['d F Y, l', 'en', '22 April 1996, Monday'], + ['Y-m-d', 'np', '१९९६-०४-२२'], + ['Y-m-d', 'en', '1996-04-22'], + ['l, d F Y', 'np', 'सोमबार, २२ अप्रिल १९९६'], + ['l, d F Y', 'en', 'Monday, 22 April 1996'], + ['d F Y', 'np', '२२ अप्रिल १९९६'], + ['d F Y', 'en', '22 April 1996'], + ['Y/m/d', 'np', '१९९६/०४/२२'], +]); From 836af51e1a80d8b3d41e83af4ffee9c59a419fee Mon Sep 17 00:00:00 2001 From: dineshuprety Date: Wed, 22 Nov 2023 10:29:26 +0545 Subject: [PATCH 3/3] done changes as mention --- README.md | 10 +++++++--- src/Helper/helper.php | 12 ++++++------ tests/ConvertToEnglishDateTest.php | 4 ++-- tests/ConvertToNepaliDateTest.php | 8 ++++---- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ef88534..99e16b6 100644 --- a/README.md +++ b/README.md @@ -114,9 +114,13 @@ Carbon::parse($engDate)->toNepaliDate(format: 'D, j F Y'); ### Helper function ```php -// we do have a helper function to convert dates -toBS("1996-04-22") // to convert English date to Nepali date (BS). ie. Output: 2053-01-10 -toAD("2053-01-10") // to convert Nepali date to English date (AD). ie. Output: 1996-04-22 +// Convert English date to Nepali date (B.S.). +toNepaliDate("1996-04-22") +// Result: 2053-01-10 + +// Convert Nepali date to English date (A.D.). +toEnglishDate("2053-01-10") +// Result: 1996-04-22 ``` ## Testing diff --git a/src/Helper/helper.php b/src/Helper/helper.php index c7c1a10..7e3727d 100644 --- a/src/Helper/helper.php +++ b/src/Helper/helper.php @@ -2,7 +2,7 @@ use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate; -if (!function_exists('toBS')) { +if (!function_exists('toNepaliDate')) { /** * The function converts a given date to the Nepali date format * @@ -13,15 +13,15 @@ * @param string|null locale The "locale" parameter is used to specify the language and region * @return string Nepali date converted from the given date. */ - function toBS($date, $format = null, $locale = null): string + function toNepaliDate($date, $format = 'Y-m-d', $locale = 'en'): string { return LaravelNepaliDate::from($date) - ->toNepaliDate($format ?? 'Y-m-d', $locale ?? 'en'); + ->toNepaliDate($format, $locale); } } -if (!function_exists('toAD')) { +if (!function_exists('toEnglishDate')) { /** * The function converts a given date from Nepali (Bikram Sambat) to English format * @@ -32,9 +32,9 @@ function toBS($date, $format = null, $locale = null): string * @param string|null locale The "locale" parameter is used to specify the language and region * @return string English date converted from the given Nepali Date. */ - function toAD($date, $format = null, $locale = null): string + function toEnglishDate($date, $format = 'Y-m-d', $locale = 'en'): string { return LaravelNepaliDate::from($date) - ->toEnglishDate($format ?? 'Y-m-d', $locale ?? 'en'); + ->toEnglishDate($format, $locale); } } diff --git a/tests/ConvertToEnglishDateTest.php b/tests/ConvertToEnglishDateTest.php index d404aa1..cecdfc9 100644 --- a/tests/ConvertToEnglishDateTest.php +++ b/tests/ConvertToEnglishDateTest.php @@ -32,7 +32,7 @@ ]); it('can convert to basic nepali date to english date with helper function', function (string $date, string $expectedResult) { - expect(toAD($date)) + expect(toEnglishDate($date)) ->toBe($expectedResult); })->with([ ['2053-01-10', '1996-04-22'], @@ -42,7 +42,7 @@ it('can convert to nepali formatted result to english date with helper function', function (string $format, string $locale, string $expectedResult) { - expect(toAD("2053-01-10", $format, $locale)) + expect(toEnglishDate("2053-01-10", $format, $locale)) ->toBe($expectedResult); })->with([ ['d F Y, l', 'np', '२२ अप्रिल १९९६, सोमबार'], diff --git a/tests/ConvertToNepaliDateTest.php b/tests/ConvertToNepaliDateTest.php index 837684b..8e197b5 100644 --- a/tests/ConvertToNepaliDateTest.php +++ b/tests/ConvertToNepaliDateTest.php @@ -31,8 +31,8 @@ ]); -it('can convert to basic nepali date with helper function toBS', function (string $date, string $expectedResult) { - expect(toBS($date)) +it('can convert to basic nepali date with helper function toNepaliDate', function (string $date, string $expectedResult) { + expect(toNepaliDate($date)) ->toBe($expectedResult); })->with([ ['1996-04-22', '2053-01-10'], @@ -40,8 +40,8 @@ ['1966-04-02', '2022-12-20'], ]); -it('can convert to nepali formatted result with helper function toBS', function (string $format, string $locale, string $expectedResult) { - expect(toBS("1996-04-22", $format, $locale)) +it('can convert to nepali formatted result with helper function toNepaliDate', function (string $format, string $locale, string $expectedResult) { + expect(toNepaliDate("1996-04-22", $format, $locale)) ->toBe($expectedResult); })->with([ ['d F Y, l', 'np', '१० वैशाख २०५३, सोमबार'],