Repository: alexhsamuel/ora (language: python)

Issue:
remove trailing 0s from Time.__str__
```
>>> t
ora.Time(2022, 1, 27, 22, 30, 0.00000000, UTC)
>>> print(t)
2022-01-27T22:30:00.00000000+00:00
```

Print `2022-01-27T22:30:00+00:00` instead.  Likewise for daytime.



Discussion / hints:

Fix patch:
diff --git a/cxx/include/ora/format.hh b/cxx/include/ora/format.hh
index 2a4f4a3..1827744 100644
--- a/cxx/include/ora/format.hh
+++ b/cxx/include/ora/format.hh
@@ -63,7 +63,8 @@ format_second(
   Second const second,
   int const precision=-1,
   int const width=2,
-  char const pad='0')
+  char const pad='0',
+  bool const trim=false)
 {
   // FIXME: Improve this logic.  See fixfmt, or convert to a power-of-10
   // denominator.  
@@ -77,6 +78,10 @@ format_second(
     // Fractional part.
     if (precision > 0) 
       sb.format(digits.rem, prec, '0');
+    if (trim) {
+      sb.rstrip('0');
+      sb.rstrip('.');
+    }
   }
 }
 
@@ -258,7 +263,8 @@ format_iso_daytime(
   StringBuilder& sb,
   HmsDaytime const& daytime,
   int const precision,
-  bool const compact=false)
+  bool const compact=false,
+  bool const trim=false)
 {
   sb.format(daytime.hour, 2, '0');
   if (!compact)
@@ -266,7 +272,7 @@ format_iso_daytime(
   sb.format(daytime.minute, 2, '0');
   if (!compact)
     sb << ':';
-  format_second(sb, daytime.second, precision);
+  format_second(sb, daytime.second, precision, 2, '0', trim);
 }
 
 
@@ -349,7 +355,8 @@ format_iso_time(
   int const precision,
   bool const compact=false,
   bool const capital=true,
-  bool const military=false)
+  bool const military=false,
+  bool const trim=false)
 {
   sb.format(date.year, 4, '0');
   if (!compact)
@@ -359,7 +366,7 @@ format_iso_time(
     sb << '-';
   sb.format(date.day, 2, '0');
   sb << (capital ? 'T' : 't');
-  daytime::format_iso_daytime(sb, daytime, precision, compact);
+  daytime::format_iso_daytime(sb, daytime, precision, compact, trim);
   if (military)
     sb << get_time_zone_offset_letter(time_zone.offset);
   else
diff --git a/cxx/include/ora/lib/string_builder.hh b/cxx/include/ora/lib/string_builder.hh
index c146164..781c2d2 100644
--- a/cxx/include/ora/lib/string_builder.hh
+++ b/cxx/include/ora/lib/string_builder.hh
@@ -96,6 +96,15 @@ public:
     return *this;
   }
 
+  StringBuilder&
+  rstrip(
+    char const c)
+  {
+    while (length_ > 0 && buffer_[length_ - 1] == c)
+      --length_;
+    return *this;
+  }
+
 private:
 
   void
diff --git a/python/ora/ext/py_daytime.hh b/python/ora/ext/py_daytime.hh
index a344d58..fd27640 100644
--- a/python/ora/ext/py_daytime.hh
+++ b/python/ora/ext/py_daytime.hh
@@ -375,7 +375,7 @@ PyDaytime<DAYTIME>::tp_str(
   else {
     auto const hms = daytime::get_hms(self->daytime_);
     StringBuilder sb;
-    daytime::format_iso_daytime(sb, hms, precision_);
+    daytime::format_iso_daytime(sb, hms, precision_, false, true);
     return Unicode::FromStringAndSize((char const*) sb, sb.length());
   }
 }
diff --git a/python/ora/ext/py_time.hh b/python/ora/ext/py_time.hh
index 68a7458..9d6a7a5 100644
--- a/python/ora/ext/py_time.hh
+++ b/python/ora/ext/py_time.hh
@@ -405,7 +405,7 @@ PyTime<TIME>::tp_str(
     StringBuilder sb;
     time::format_iso_time(
       sb, datenum_to_ymd(ldd.datenum), daytick_to_hms(ldd.daytick),
-      ldd.time_zone, precision_);
+      ldd.time_zone, precision_, false, true, false, true);
     return Unicode::FromStringAndSize((char const*) sb, sb.length());
   }
 }


Test patch:
diff --git a/python/ora/test/test_daytime_basic.py b/python/ora/test/test_daytime_basic.py
index 2d3fa9d..1d193d8 100644
--- a/python/ora/test/test_daytime_basic.py
+++ b/python/ora/test/test_daytime_basic.py
@@ -390,8 +390,8 @@ def test_std():
 
 def test_str():
     # Don't test for number of trailing zeros.
-    assert str(Daytime(0, 0, 0)).rstrip("0") == "00:00:00."
-    assert str(Daytime(23, 59, 59.99999)).rstrip("0") == "23:59:59.99999"
+    assert str(Daytime(0, 0, 0)) == "00:00:00"
+    assert str(Daytime(23, 59, 59.99999)) == "23:59:59.99999"
 
 
 def test_format_basic():
diff --git a/python/ora/test/test_from_local.py b/python/ora/test/test_from_local.py
index 09ad15b..32111f3 100644
--- a/python/ora/test/test_from_local.py
+++ b/python/ora/test/test_from_local.py
@@ -11,8 +11,7 @@
 
 def test_utc0():
     t = from_local((1973/Dec/3, 7200.125), UTC)
-    # FIXME
-    assert(str(t) == "1973-12-03T02:00:00.12500000+00:00")
+    assert(str(t) == "1973-12-03T02:00:00.125+00:00")
 
 
 @pytest.mark.xfail