tests: fix fractional timestamps
authorMichael Wallner <mike@php.net>
Mon, 10 Jan 2022 14:13:32 +0000 (15:13 +0100)
committerMichael Wallner <mike@php.net>
Mon, 10 Jan 2022 14:13:32 +0000 (15:13 +0100)
ion_private.h
tests/Timestamp.phpt
tests/serialize/timestamp.phpt [new file with mode: 0644]

index 205c9b81ae03621d4f22887bb79ea76fd225bdf9..600f18beddcb77aaf8fdbf255aa5144895cb162c 100644 (file)
@@ -929,10 +929,10 @@ LOCAL ION_TIMESTAMP *ion_timestamp_from_php(ION_TIMESTAMP *buf, php_ion_timestam
        zval tmp;
        int precision = Z_LVAL_P(zend_read_property(ts->std.ce, &ts->std, ZEND_STRL("precision"), 0, &tmp));
 
-       if (!precision || precision > ION_TS_FRAC) {
+       if (!precision || precision > (ION_TS_FRAC|0x80)) {
                zend_throw_exception_ex(spl_ce_InvalidArgumentException, IERR_INVALID_ARG,
                                "Invalid precision (%d) of ion\\Timestamp", precision);
-       } else switch ((buf->precision = precision)) {
+       } else switch ((buf->precision = precision) & 0x7f) {
        case ION_TS_FRAC:
                ion_ts_frac_from_usec(&buf->fraction, (int) ts->time->us, ctx);
                /* fallthrough */
index 1b4a222381d46187aa518cc9e4dcaf73fc7a502d..c0f0171ca3cb9d42634ebb6a49ad669e2457a5c6 100644 (file)
@@ -14,7 +14,7 @@ try {
 } catch (Throwable) {
        echo "caught empty\n";
 }
-$full = "2021-12-07T14:08:51+00:00";
+$full = "2021-12-07T14:08:51.123456+00:00";
 var_dump($t=new Timestamp(Timestamp\Precision::Day, datetime:$full),(string)$t);
 var_dump($t=new Timestamp(Timestamp\Precision::Day->value, datetime:$full),(string)$t);
 var_dump($t=new Timestamp(Timestamp\Precision::Min, datetime:"2020-10-01"),(string)$t);
@@ -32,7 +32,7 @@ object(ion\Timestamp)#%d (5) {
   ["format"]=>
   string(7) "Y-m-d\T"
   ["date"]=>
-  string(26) "2021-12-07 14:08:51.000000"
+  string(26) "2021-12-07 14:08:51.123456"
   ["timezone_type"]=>
   int(1)
   ["timezone"]=>
@@ -45,7 +45,7 @@ object(ion\Timestamp)#%d (5) {
   ["format"]=>
   string(7) "Y-m-d\T"
   ["date"]=>
-  string(26) "2021-12-07 14:08:51.000000"
+  string(26) "2021-12-07 14:08:51.123456"
   ["timezone_type"]=>
   int(1)
   ["timezone"]=>
diff --git a/tests/serialize/timestamp.phpt b/tests/serialize/timestamp.phpt
new file mode 100644 (file)
index 0000000..21941af
--- /dev/null
@@ -0,0 +1,35 @@
+--TEST--
+ion\serialize/timestamp
+--EXTENSIONS--
+ion
+--FILE--
+TEST
+<?php
+$dt = ion\unserialize("1971-02-03T04:05:06.789Z");
+var_dump($dt);
+$ts = ion\serialize($dt);
+var_dump($ts);
+
+var_dump(ion\unserialize($ts));
+?>
+DONE
+--EXPECTF--
+TEST
+object(ion\Timestamp)#5 (3) {
+  ["precision"]=>
+  int(247)
+  ["format"]=>
+  string(1) "c"
+  ["date"]=>
+  string(26) "1971-02-03 04:05:06.789000"
+}
+string(24) "1971-02-03T04:05:06.789Z"
+object(ion\Timestamp)#6 (3) {
+  ["precision"]=>
+  int(247)
+  ["format"]=>
+  string(1) "c"
+  ["date"]=>
+  string(26) "1971-02-03 04:05:06.789000"
+}
+DONE