Skip to content

Commit

Permalink
added tests for time zones
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Feb 27, 2015
1 parent abbfd08 commit 7cc1f71
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ Follow [@peterohler on Twitter](http://twitter.com/#!/peterohler) for announceme
zero. This fixes a problem with pre-2.2.0 Rubies that automatically convert
zero offset times to local times.

- TBD Added :unix_zone time_format option for formating numeric time. This
option is the same as the :unix time option but the UTC offset is included as
an exponent to the number time value. A value of 86400 is an indication of
UTC time.
- Added :unix_zone time_format option for formating numeric time. This option
is the same as the :unix time option but the UTC offset is included as an
exponent to the number time value. A value of 86400 is an indication of UTC
time.

## Current Release 2.11.5

Expand Down
3 changes: 2 additions & 1 deletion ext/oj/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ dump_time(VALUE obj, Out out, int withZone) {
char buf[64];
char *b = buf + sizeof(buf) - 1;
long size;
char *dot = b - 10;
char *dot;
int neg = 0;
long one = 1000000000;
#if HAS_RB_TIME_TIMESPEC
Expand Down Expand Up @@ -1046,6 +1046,7 @@ dump_time(VALUE obj, Out out, int withZone) {
}
*b-- = 'e';
}
dot = b - 9;
if (0 < out->opts->sec_prec) {
if (9 > out->opts->sec_prec) {
int i;
Expand Down
2 changes: 1 addition & 1 deletion ext/oj/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ hat_num(ParseInfo pi, Val parent, Val kval, NumInfo ni) {
args[2] = LONG2NUM(st->tm_mday);
args[3] = LONG2NUM(st->tm_hour);
args[4] = LONG2NUM(st->tm_min);
args[5] = DBL2NUM((double)st->tm_sec + (double)nsec / 1000000000.0);
args[5] = rb_float_new((double)st->tm_sec + (double)nsec / 1000000000.0);
args[6] = LONG2NUM(ni->exp);
parent->val = rb_funcall2(rb_cTime, oj_new_id, 7, args);
} else {
Expand Down
4 changes: 3 additions & 1 deletion ext/oj/sparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#else
#define NUM_MAX (FIXNUM_MAX >> 8)
#endif
#define EXP_MAX 1023
#define EXP_MAX 100000
#define DEC_MAX 15

static void
Expand Down Expand Up @@ -413,6 +413,7 @@ read_num(ParseInfo pi) {
ni.infinity = 0;
ni.nan = 0;
ni.neg = 0;
ni.hasExp = 0;
ni.no_big = (FloatDec == pi->options.bigdec_load);
c = reader_get(&pi->rd);
if ('-' == c) {
Expand Down Expand Up @@ -467,6 +468,7 @@ read_num(ParseInfo pi) {
if ('e' == c || 'E' == c) {
int eneg = 0;

ni.hasExp = 1;
c = reader_get(&pi->rd);
if ('-' == c) {
c = reader_get(&pi->rd);
Expand Down
8 changes: 4 additions & 4 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
^c^s show subtree

- time
- add :unix_zone time_format option
- time with zone as exponent
- have to increase EXP_MAX to 86400
- handle out of range in routine to create doubles
- test
- check number parsing for big exp
- times with different zones - check string also


- streaming parser for scp and saj

Expand Down
23 changes: 23 additions & 0 deletions test/test_compat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,29 @@ def test_bigdecimal_load
assert_equal(orig, bg)
end

# Time
def test_time_ruby
t = Time.parse('2015-01-05 21:37:07.123456 -0800')
expect = '"' + t.to_s + '"'
json = Oj.dump(t, :mode => :compat, :time_format => :ruby)
assert_equal(expect, json)
end
def test_time_xml
t = Time.parse('2015-01-05 21:37:07.123456 -0800')
json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 6)
assert_equal('"2015-01-05T21:37:07.123456-08:00"', json)
end
def test_time_unix
t = Time.parse('2015-01-05 21:37:07.123456 -0800')
json = Oj.dump(t, :mode => :compat, :time_format => :unix, :second_precision => 6)
assert_equal('1420522627.123456', json)
end
def test_time_unix_zone
t = Time.parse('2015-01-05 21:37:07.123456 -0800')
json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
assert_equal('1420522627.123456e-28800', json)
end

# Stream IO
def test_io_string
json = %{{
Expand Down
10 changes: 10 additions & 0 deletions test/test_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,16 @@ def test_time_early
dump_and_load(t, false)
end

def test_time_unix_zone
t = Time.parse('2015-01-05 21:37:07.123456789 -0800')
dump_and_load(t, false)
end

def test_time_unix_zone_utc
t = Time.parse('2015-01-05 21:37:07.123456789Z')
dump_and_load(t, false)
end

def test_json_object
obj = Jeez.new(true, 58)
dump_and_load(obj, false)
Expand Down

0 comments on commit 7cc1f71

Please sign in to comment.