-
Notifications
You must be signed in to change notification settings - Fork 54
Round a fractional offset given as a string #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -413,12 +413,15 @@ shrink_space(char *d, const char *s, long l) | |||||
| } | ||||||
|
|
||||||
| VALUE | ||||||
| date_zone_to_diff(VALUE str) | ||||||
| date_zone_to_diff_frac(VALUE str, int *fracp, long *whole) | ||||||
| { | ||||||
| VALUE offset = Qnil; | ||||||
| long l = RSTRING_LEN(str); | ||||||
| const char *s = RSTRING_PTR(str); | ||||||
|
|
||||||
| *fracp = 0; | ||||||
| *whole = 0; | ||||||
|
|
||||||
| { | ||||||
| int dst = 0; | ||||||
| int w; | ||||||
|
|
@@ -533,6 +536,8 @@ date_zone_to_diff(VALUE str) | |||||
| offset = rb_rational_num(offset); | ||||||
| } | ||||||
| } | ||||||
| *fracp = sec != 0; | ||||||
| *whole = hour * 3600; | ||||||
| goto ok; | ||||||
| } | ||||||
| else if (l > 2) { | ||||||
|
|
@@ -558,6 +563,19 @@ date_zone_to_diff(VALUE str) | |||||
| return offset; | ||||||
| } | ||||||
|
|
||||||
| VALUE | ||||||
| date_zone_to_diff(VALUE str) | ||||||
| { | ||||||
| int frac; | ||||||
| long whole; | ||||||
| VALUE offset = date_zone_to_diff_frac(str, &frac, &whole); | ||||||
|
|
||||||
| if (frac) | ||||||
| rb_warning("fraction of hour in a zone offset is deprecated" | ||||||
| " and will be ignored"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Similar change for the other warning. |
||||||
| return offset; | ||||||
| } | ||||||
|
|
||||||
| static int | ||||||
| day_num(VALUE s) | ||||||
| { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| # frozen_string_literal: true | ||
| require 'test/unit' | ||
| require 'date' | ||
| require 'stringio' | ||
|
|
||
| class TestDateNew < Test::Unit::TestCase | ||
|
|
||
|
|
@@ -213,6 +214,45 @@ def test_civil__offset | |
| assert_equal(0, d.offset) | ||
| end | ||
|
|
||
| def test_civil__fractional_zone_offset_deprecated | ||
| # A fraction that makes the offset non-integral is already dropped whole, | ||
| # so ignoring the fraction will not change these; only the pre-existing | ||
| # "invalid offset" warning may appear. | ||
| ['+00.123', '-00.123'].each do |z| | ||
| out = capture_zone_warning { assert_equal(0, DateTime.civil(2001,2,3, 0,0,0, z).offset) } | ||
| assert_match(/invalid offset is ignored/, out) | ||
| refute_match(/fraction of hour/, out) | ||
| end | ||
|
|
||
| # Here the fraction reaches the result, or dropping it would give an | ||
| # offset different from the 0 returned today. | ||
| out = capture_zone_warning do | ||
| assert_equal(Rational(1800, 86400), DateTime.civil(2001,2,3, 0,0,0, '+00.5').offset) | ||
| end | ||
| assert_match(/fraction of hour/, out) | ||
|
|
||
| out = capture_zone_warning do | ||
| assert_equal(0, DateTime.civil(2001,2,3, 0,0,0, '+01.123').offset) | ||
| end | ||
| assert_match(/fraction of hour/, out) | ||
|
|
||
| # A zero fraction and the colon forms change nothing. | ||
| ['+01.0', '+00:30'].each do |z| | ||
| out = capture_zone_warning { DateTime.civil(2001,2,3, 0,0,0, z) } | ||
| refute_match(/fraction of hour/, out) | ||
| end | ||
| end | ||
|
|
||
| def capture_zone_warning | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for this approach instead of |
||
| verbose, $VERBOSE = $VERBOSE, true | ||
| err, $stderr = $stderr, StringIO.new | ||
| yield | ||
| $stderr.string | ||
| ensure | ||
| $stderr = err | ||
| $VERBOSE = verbose | ||
| end | ||
|
|
||
| def test_civil__reform | ||
| d = Date.jd(Date::ENGLAND, Date::ENGLAND) | ||
| dt = DateTime.jd(Date::ENGLAND, 0,0,0,0, Date::ENGLAND) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,26 @@ | |
|
|
||
| class TestDateParse < Test::Unit::TestCase | ||
|
|
||
| def test__parse_fractional_zone_offset_deprecated | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be tests added for |
||
| # The fractional hour is honoured today and will be ignored; warn only | ||
| # where dropping it changes the answer this returns now. | ||
| assert_warning(/fraction of hour/) do | ||
| assert_equal(Rational(2214, 5), Date._parse('2001-02-03T00:00:00+00.123')[:offset]) | ||
| end | ||
| assert_warning(/fraction of hour/) do | ||
| assert_equal(1800, Date._parse('2001-02-03T00:00:00+00.5')[:offset]) | ||
| end | ||
| # A zero fraction changes nothing, so it must stay quiet. | ||
| assert_warning('') do | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should warn, because while the |
||
| assert_equal(3600, Date._parse('2001-02-03T00:00:00+01.0')[:offset]) | ||
| end | ||
| # The colon forms have no fractional hour at all. | ||
| assert_warning('') do | ||
| assert_equal(1800, Date._parse('2001-02-03T00:00:00+00:30')[:offset]) | ||
| assert_equal(86399, Date._parse('2001-02-03T00:00:00+23:59:59')[:offset]) | ||
| end | ||
| end | ||
|
|
||
| def test__parse | ||
| [ | ||
| # ctime(3), asctime(3) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the following block up to near the top of the file (copied from
date_core.c):then switch to:
Similar change for the other warning, except there you would just move the block up in the same file.