Skip to content

Commit

Permalink
Create weekyear(date) function (#61)
Browse files Browse the repository at this point in the history
## 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
```
  • Loading branch information
niltonvasques authored Apr 25, 2023
1 parent f500d7a commit 26cef1c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ext/equations-parser
2 changes: 1 addition & 1 deletion ext/libnativemath/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion parsec.gemspec
Original file line number Diff line number Diff line change
@@ -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 = ['[email protected]', '[email protected]', '[email protected]']
Expand Down
34 changes: 34 additions & 0 deletions test/test_parsec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 26cef1c

Please sign in to comment.