1 /*
2  * hunt-proton: AMQP Protocol library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.proton.codec.impl.TimestampElement;
13 
14 import std.datetime.date;
15 import hunt.io.ByteBuffer;
16 import std.conv;
17 import hunt.proton.codec.impl.AtomicElement;
18 import hunt.proton.codec.impl.Element;
19 import hunt.proton.codec.impl.ArrayElement;
20 import hunt.proton.codec.impl.AbstractElement;
21 //import hunt.util.DateTime;
22 import hunt.time.LocalDateTime;
23 
24 
25 import hunt.proton.codec.Data;
26 
27 alias Date = LocalDateTime;
28 
29 class TimestampElement : AtomicElement!Date
30 {
31 
32    // private Date _value;
33     private Date _value;
34 
35     this(IElement parent, IElement prev, Date d)
36     {
37         super(parent, prev);
38         _value = d;
39     }
40 
41     public int size()
42     {
43         return isElementOfArray() ? 8 : 9;
44     }
45 
46     public Object getValue()
47     {
48         return _value;
49     }
50 
51     public Data.DataType getDataType()
52     {
53         return Data.DataType.TIMESTAMP;
54     }
55 
56     public int encode(ByteBuffer b)
57     {
58         int size = size();
59         if(size > b.remaining())
60         {
61             return 0;
62         }
63         if(size == 9)
64         {
65             b.put(cast(byte)0x83);
66         }
67         b.putLong(_value.toEpochMilli());
68         return size;
69     }
70 }