From 26cef1c882c7caf78a03ca1d2e1daf46b946cbf5 Mon Sep 17 00:00:00 2001 From: Nilton Vasques Date: Tue, 25 Apr 2023 10:55:21 -0300 Subject: [PATCH] Create weekyear(date) function (#61) ## Description Create the `weekday(date)` function to return the week of year of a date, based on the ISO week date algorithm: ![image](https://user-images.githubusercontent.com/13650290/234122570-dd78ca39-722f-401f-96e8-8dbe47fc2cc4.png) > https://en.wikipedia.org/wiki/ISO_week_date ### Usage ```cpp weekyear("2022-04-20") // result => 16 ``` --- ext/equations-parser | 2 +- ext/libnativemath/extconf.rb | 2 +- parsec.gemspec | 2 +- test/test_parsec.rb | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/ext/equations-parser b/ext/equations-parser index 8769175..b81e50b 160000 --- a/ext/equations-parser +++ b/ext/equations-parser @@ -1 +1 @@ -Subproject commit 8769175390cafd9c40b52477f1c97184266c4a68 +Subproject commit b81e50bc4c43556f8d75566bf0f86a2e71a7ac4e diff --git a/ext/libnativemath/extconf.rb b/ext/libnativemath/extconf.rb index d5d11a5..b7dba8b 100644 --- a/ext/libnativemath/extconf.rb +++ b/ext/libnativemath/extconf.rb @@ -38,7 +38,7 @@ end GIT_REPOSITORY = 'https://github.com/oxeanbits/equations-parser.git'.freeze -COMMIT = '8769175390cafd9c40b52477f1c97184266c4a68'.freeze +COMMIT = 'b81e50bc4c43556f8d75566bf0f86a2e71a7ac4e'.freeze Dir.chdir(BASEDIR) do system('git init') diff --git a/parsec.gemspec b/parsec.gemspec index 7052f8e..4061f53 100644 --- a/parsec.gemspec +++ b/parsec.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'parsecs' - s.version = '0.12.0' + s.version = '0.13.0' s.platform = Gem::Platform::RUBY s.authors = ['Nilton Vasques', 'Victor Cordeiro', 'Beatriz Fagundes'] s.email = ['nilton.vasques@gmail.com', 'victorcorcos@gmail.com', 'beatrizsfslima@gmail.com'] diff --git a/test/test_parsec.rb b/test/test_parsec.rb index 5f4e97d..bf4c4a5 100644 --- a/test/test_parsec.rb +++ b/test/test_parsec.rb @@ -338,4 +338,38 @@ def test_regex assert_equal('apple', parsec.eval_equation('regex("apple123redbanana", "(?=apple)([a-z]+)(?=\\\\d+red)")')) assert_equal('apple', parsec.eval_equation('regex("apple123red456banana", "(?=apple)([a-z]+)(?=(\\\\d+red)\\\\d+banana)")')) end + + def test_weekyear + parsec = Parsec::Parsec + + # Week number of the year when 1st of January is a Sunday + assert_equal(1, parsec.eval_equation('weekyear("2023-01-01")')) + assert_equal(1, parsec.eval_equation('weekyear("2023-01-07")')) + assert_equal(2, parsec.eval_equation('weekyear("2023-01-08")')) + + assert_equal(17, parsec.eval_equation('weekyear("2023-04-25")')) + assert_equal(17, parsec.eval_equation('weekyear("2023-04-29")')) + assert_equal(18, parsec.eval_equation('weekyear("2023-04-30")')) + assert_equal(18, parsec.eval_equation('weekyear("2023-05-06")')) + + assert_equal(52, parsec.eval_equation('weekyear("2023-12-24")')) + assert_equal(53, parsec.eval_equation('weekyear("2023-12-31")')) + + # Week number of the year a leap year + assert_equal(1, parsec.eval_equation('weekyear("2024-01-01")')) + assert_equal(1, parsec.eval_equation('weekyear("2024-01-06")')) + assert_equal(2, parsec.eval_equation('weekyear("2024-01-07")')) + + assert_equal(52, parsec.eval_equation('weekyear("2024-12-22")')) + assert_equal(53, parsec.eval_equation('weekyear("2024-12-29")')) + + # Week number of the year when 1st of January is friday + assert_equal(53, parsec.eval_equation('weekyear("2027-01-01")')) + assert_equal(53, parsec.eval_equation('weekyear("2027-01-02")')) + assert_equal(1, parsec.eval_equation('weekyear("2027-01-03")')) + + assert_equal(52, parsec.eval_equation('weekyear("2027-12-27")')) + assert_equal(52, parsec.eval_equation('weekyear("2027-12-31")')) + assert_equal(53, parsec.eval_equation('weekyear("2028-01-01")')) + end end