Selaa lähdekoodia

LibCore+LibIPC: Add IPC coder for Core::DateTime

Since LibCore cannot depend on LibIPC, the coders are defined in LibIPC
just like they are for Core::AnonymousBuffer.
Timothy Flynn 4 vuotta sitten
vanhempi
commit
6e10c2cdb7

+ 8 - 0
Userland/Libraries/LibCore/DateTime.h

@@ -27,6 +27,7 @@
 #pragma once
 
 #include <AK/String.h>
+#include <LibIPC/Forward.h>
 #include <time.h>
 
 namespace Core {
@@ -70,3 +71,10 @@ private:
 };
 
 }
+
+namespace IPC {
+
+bool encode(IPC::Encoder&, const Core::DateTime&);
+bool decode(IPC::Decoder&, Core::DateTime&);
+
+}

+ 11 - 0
Userland/Libraries/LibIPC/Decoder.cpp

@@ -27,6 +27,7 @@
 #include <AK/MemoryStream.h>
 #include <AK/URL.h>
 #include <LibCore/AnonymousBuffer.h>
+#include <LibCore/DateTime.h>
 #include <LibIPC/Decoder.h>
 #include <LibIPC/Dictionary.h>
 #include <LibIPC/File.h>
@@ -205,4 +206,14 @@ bool decode(Decoder& decoder, Core::AnonymousBuffer& buffer)
     return buffer.is_valid();
 }
 
+bool decode(Decoder& decoder, Core::DateTime& datetime)
+{
+    i64 timestamp = -1;
+    if (!decoder.decode(timestamp))
+        return false;
+
+    datetime = Core::DateTime::from_timestamp(static_cast<time_t>(timestamp));
+    return true;
+}
+
 }

+ 8 - 0
Userland/Libraries/LibIPC/Encoder.cpp

@@ -28,6 +28,7 @@
 #include <AK/String.h>
 #include <AK/URL.h>
 #include <LibCore/AnonymousBuffer.h>
+#include <LibCore/DateTime.h>
 #include <LibIPC/Dictionary.h>
 #include <LibIPC/Encoder.h>
 #include <LibIPC/File.h>
@@ -180,4 +181,11 @@ bool encode(Encoder& encoder, const Core::AnonymousBuffer& buffer)
     }
     return true;
 }
+
+bool encode(Encoder& encoder, const Core::DateTime& datetime)
+{
+    encoder << static_cast<i64>(datetime.timestamp());
+    return true;
+}
+
 }