title: Java - convert MessagePack timestamp to date
body: <p>I'm decoding a MessagePack message from an Apache Beam pipeline in a Java project. I'm using Maven to import the MessagePack library as dependency:</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.msgpack&lt;/groupId&gt;
    &lt;artifactId&gt;msgpack-core&lt;/artifactId&gt;
    &lt;version&gt;0.8.16&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>I can use this to parse the MessagePack message it into key/value pairs in a <code>Map</code>, like this:</p>
  
  <pre><code>    @ProcessElement
      public void processElement(ProcessContext c) 
      {
          try 
          {           
              Map&lt;Value, Value&gt; map = MessagePack.newDefaultUnpacker(c.element().getPayload()).unpackValue().asMapValue().map();
  </code></pre>
  
  <p>The map contains a key/value pair for a MessagePack 'Timestamp' 'extension' type that looks like this, and which represents a date/time (see the 'Note' at the bottom, for an explanation of MessagePack extension types):</p>
  
  <pre><code>UTC=(-1,0x5b-161d46)
  </code></pre>
  
  <p>I can get this a 'timestamp' value, by getting the value with the key <code>UTC</code>, from the map. I retrieve it as a MessagePack <code>ExtensionValue</code> like this:</p>
  
  <pre><code> Value date = map.get(ValueFactory.newString("UTC")).asExtensionValue();
  </code></pre>
  
  <p><code>date</code> is then an object which has 2 properties:</p>
  
  <pre><code>`type` = 1
  `data` = `0x5b-161d46`
  </code></pre>
  
  <p>How do I convert <code>data</code> to a meaningful representation of the date? The 'data' should translate to a 'current' date, sometime around 16th November 2018. It's not as simple as converting the hex value to decimal. Do I need to separately unpack this <code>data</code> somehow? I suspect that <code>5b-161d46</code> probably needs to be treated as a byte array and then converted somehow.</p>
  
  <p>I can do this to get the <code>data</code> part of the extension type as a byte array:</p>
  
  <pre><code>byte[] date = map.get(ValueFactory.newString("UTC")).asExtensionValue().getData();
  </code></pre>
  
  <p>which gives me <code>[91, -22, 29, 70]</code></p>
  
  <p>... and I can try to unpack it like this:</p>
  
  <pre><code>MessagePack.newDefaultUnpacker(date).unpackValue()
  </code></pre>
  
  <p>... however that just gives me the first byte (<code>5b</code>) converted to a <code>long</code> i.e. <code>91</code></p>
  
  <p>And if I try any of these I get <code>org.msgpack.core.MessageTypeCastException</code>, probably because <code>unpackValue</code> just gives me a single <code>long</code> number</p>
  
  <pre><code>MessagePack.newDefaultUnpacker(date).unpackValue().asIntegerValue();
  MessagePack.newDefaultUnpacker(date).unpackValue().asMapValue();
  MessagePack.newDefaultUnpacker(date).unpackValue().asRawValue();
  </code></pre>
  
  <p>I've also tried the following: </p>
  
  <pre><code>MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(date);
      while(unpacker.hasNext()) {
          MessageFormat f = unpacker.getNextFormat();
              switch(f) {
                  case POSFIXINT:
                  case NEGFIXINT: {
                      int v = unpacker.unpackInt();
                      break;
                  }
               }
      }
  </code></pre>
  
  <p>The values in the array are recognised as either <code>POSFIXINT</code> or <code>NEGFIXINT</code>, so I can use this to extract decimal integer values for each byte in the array, however that only allows me to extract the elements in the <code>date</code> array as integers, and I still don't know how to translate that to a date.</p>
  
  <p>How do I need to interpret/unpack these dates?</p>
  
  <hr>
  
  <blockquote>
    <p><strong>Note</strong> - An extension value is special type of MessagePack value, represented as a tuple where <code>-1</code> defines the extension type. <code>-1</code> is
    a reserved extension for a MessagePack <code>timestamp</code>, and the remainder
    gives a hex value (<code>0x5b-161d46</code>):</p>
    
    <p><a href="https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type" rel="nofollow noreferrer">https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type</a></p>
  </blockquote>