]> www.infradead.org Git - users/dhowells/kafs-utils.git/commitdiff
Add a class to represent times
authorDavid Howells <dhowells@redhat.com>
Wed, 12 Aug 2020 12:14:15 +0000 (13:14 +0100)
committerDavid Howells <dhowells@redhat.com>
Fri, 5 May 2023 10:53:26 +0000 (11:53 +0100)
Signed-off-by: David Howells <dhowells@redhat.com>
kafs/Makefile
kafs/kafs.H
kafs/misc.C [new file with mode: 0644]

index aa1ff178eb0213ce873711a3916fc25742490a6a..3dca36271127ec827293603fa701140bf1514455 100644 (file)
@@ -7,7 +7,8 @@ CORE_SRCS := \
        kafs.C \
        arg_completion.C \
        arg_parse.C \
-       display_error.C
+       display_error.C \
+       misc.C
 
 BOS_SRCS := \
        bos.C \
index 0ae8b76e62d8f3a23f4c84b61d5905be27202ae5..3011c181a859fed5db7dd607439492d4edb0c56e 100644 (file)
@@ -23,6 +23,7 @@ extern "C" {
 namespace kafs {
 
 namespace afs {
+class opr_time;
 constexpr unsigned int FS_PORT         = 7000; /* AFS file server port */
 constexpr unsigned int FS_SERVICE      = 1;    /* AFS File Service ID */
 }
@@ -34,6 +35,24 @@ enum Service {
        service_yfs,
 };
 
+class Time {
+       struct timespec                 ts;
+       bool                            has_nsecs;
+public:
+       Time() {
+               ts.tv_sec = 0;
+               ts.tv_nsec = 0;
+               has_nsecs = false;
+       }
+       Time(time_t t) {
+               ts.tv_sec = t;
+               ts.tv_nsec = 0;
+               has_nsecs = false;
+       }
+       Time(const kafs::afs::opr_time &t);
+       inline time_t seconds() const { return ts.tv_sec; }
+};
+
 class Context : public rxrpc::refcount {
 public:
        rxrpc::ref<rxrpc::Endpoint> endpoint;
diff --git a/kafs/misc.C b/kafs/misc.C
new file mode 100644 (file)
index 0000000..fc3a184
--- /dev/null
@@ -0,0 +1,43 @@
+/* Miscellaneous routines.
+ *
+ * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public Licence
+ * as published by the Free Software Foundation; either version
+ * 2 of the Licence, or (at your option) any later version.
+ */
+
+#include "kafs.H"
+#include "afs_xg.H"
+
+/*
+ * Convert a signed 100ns-resolution 64-bit time into a kafs time.
+ */
+kafs::Time::Time(const kafs::afs::opr_time &yt)
+{
+       unsigned long long abs_t, div, rem, t = yt.time;
+
+       /*
+        * Unfortunately can not use normal 64 bit division on 32 bit arch, but
+        * the alternative, do_div, does not work with negative numbers so have
+        * to special case them
+        */
+       if (t < 0) {
+               abs_t = -t;
+               div = abs_t / 10000000;
+               rem = abs_t % 10000000;
+               ts.tv_sec = -(div * 100);
+               ts.tv_nsec = rem;
+       } else {
+               abs_t = t;
+               div = abs_t / 10000000;
+               rem = abs_t % 10000000;
+               ts.tv_sec = div * 100;
+               ts.tv_nsec = rem;
+       }
+
+       has_nsecs = true;
+}
+