[PATCH 9/9] clock: binary: add sixteen bits mode

Felipe Contreras felipe.contreras at gmail.com
Fri Jun 14 02:40:44 CEST 2019


A true binary clock shouldn't based on hours (mod 12), but on bits (mod
2).

Take for example the following clock:

  1010010000100000

In order to make sense of it we would have to find out if it's
binary-coded decimal (BCD), or sexagesimal. And then if it's packed or
not, and where the divisions are.

But we could say this number is a binary... A true binary.

To see if this is the highest part (past 12) or the lowest part (before
12), we just look at the most significant digit '1'. So it's past noon.

Then of those later 12 hours are we in earlier or later? We look at the
second digit '0', so it's between 12-18; We still have a quarter of the
day left. We continue like that until the last digit, which is similar
to a second, but not quite.

This is a 16-bit binary clock; that is: a clock whose value is a real
binary, on which you can do binary operations. By dividing the day into
2 ^ 16 equal parts it's easy to represent the most and least significant
  parts of a day.

  000: day started
  100: past noon
  010: early morning (6am)
  110: afternoon (6pm)
  001: 3am
  011: 9am
  101: 3pm
  111: 9pm

We can't call this the true true binary clock, but it pretty much is.

In order to represent minutes (not seconds) we need the first 10 most
significant bits.

Signed-off-by: Felipe Contreras <felipe.contreras at gmail.com>
---
 plugins/clock/clock-binary.c     | 40 ++++++++++++++++++++++++++++++--
 plugins/clock/clock-dialog.glade |  3 +++
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/plugins/clock/clock-binary.c b/plugins/clock/clock-binary.c
index e2b05590..68c7d5b0 100644
--- a/plugins/clock/clock-binary.c
+++ b/plugins/clock/clock-binary.c
@@ -64,7 +64,8 @@ enum
 enum
 {
   MODE_DECIMAL,
-  MODE_SEXADECIMAL
+  MODE_SEXADECIMAL,
+  MODE_SIXTEEN_BITS
 };
 
 struct _XfceClockBinaryClass
@@ -131,7 +132,7 @@ xfce_clock_binary_class_init (XfceClockBinaryClass *klass)
   g_object_class_install_property (gobject_class,
                                    PROP_MODE,
                                    g_param_spec_uint ("binary-mode", NULL, NULL,
-                                                      MODE_DECIMAL, MODE_SEXADECIMAL, MODE_DECIMAL,
+                                                      MODE_DECIMAL, MODE_SIXTEEN_BITS, MODE_DECIMAL,
                                                       G_PARAM_READWRITE
                                                       | G_PARAM_STATIC_STRINGS));
 
@@ -243,6 +244,9 @@ xfce_clock_binary_get_property (GObject    *object,
       case MODE_SEXADECIMAL:
         ratio = binary->show_seconds ? 2.0 : 3.0;
         break;
+      case MODE_SIXTEEN_BITS:
+        ratio = binary->show_seconds ? 1.0 : 2.0;
+        break;
       default:
         return;
       }
@@ -338,6 +342,31 @@ binary_coded_decimal (gint *table, GDateTime *time, gint cols, gint rows)
 
 
 
+static void
+sixteen_bits_day (gint *table, GDateTime *time, gint cols, gint rows)
+{
+  gint col, row;
+  guint seconds, ticks, cur;
+
+  seconds = g_date_time_get_hour (time) * 60; // minutes
+  seconds = (seconds + g_date_time_get_minute (time)) * 60; // seconds
+  seconds = seconds + g_date_time_get_second (time);
+  ticks = (seconds * 512) / 675; // total_ticks / total_seconds
+
+  cur = 1 << 16;
+  for (row = 0; row < rows; row++)
+    {
+      for (col = 0; col < cols; col++)
+        {
+          cur = cur >> 1;
+          if (cur & ticks)
+              table[col] |= 1 << row;
+        }
+    }
+}
+
+
+
 static gboolean
 xfce_clock_binary_draw (GtkWidget *widget,
                         cairo_t   *cr)
@@ -380,6 +409,10 @@ xfce_clock_binary_draw (GtkWidget *widget,
     cols = 6;
     rows = binary->show_seconds ? 3 : 2;
     break;
+  case MODE_SIXTEEN_BITS:
+    cols = binary->show_seconds ? 4 : 5;
+    rows = binary->show_seconds ? 4 : 2;
+    break;
   default:
     return FALSE;
   }
@@ -437,6 +470,9 @@ xfce_clock_binary_draw (GtkWidget *widget,
   case MODE_SEXADECIMAL:
     binary_coded_sexadecimal (table, time, cols, rows);
     break;
+  case MODE_SIXTEEN_BITS:
+    sixteen_bits_day (table, time, cols, rows);
+    break;
   }
 
   inactive_rgba = active_rgba;
diff --git a/plugins/clock/clock-dialog.glade b/plugins/clock/clock-dialog.glade
index 83348c82..33b45328 100644
--- a/plugins/clock/clock-dialog.glade
+++ b/plugins/clock/clock-dialog.glade
@@ -59,6 +59,9 @@
       <row>
         <col id="0" translatable="yes">Sexadecimal</col>
       </row>
+      <row>
+        <col id="0" translatable="yes">Sixteen bits</col>
+      </row>
     </data>
   </object>
   <object class="XfceTitledDialog" id="dialog">
-- 
2.22.0.rc2.dirty



More information about the Xfce4-dev mailing list