forked from mjameson-se/jebml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateElement.java
76 lines (69 loc) · 2.33 KB
/
DateElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* JEBML - Java library to read/write EBML/Matroska elements.
* Copyright (C) 2004 Jory Stone <[email protected]>
* Based on Javatroska (C) 2002 John Cannon <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.ebml;
import java.nio.ByteBuffer;
import java.util.Date;
public class DateElement extends SignedIntegerElement
{
// const uint64 EbmlDate::UnixEpochDelay = 978307200; // 2001/01/01 00:00:00 UTC
public static final long UnixEpochDelay = 978307200000L; // 2001/01/01 00:00:00 UTC
private static final int MIN_SIZE_LENGTH = 8;
public DateElement(final byte[] type)
{
super(type);
}
public DateElement()
{
super();
}
/**
* Set the Date of this element
*
* @param value Date to set
*/
public void setDate(final Date value)
{
final long val = (value.getTime() - UnixEpochDelay) * 1000000;
setData(ByteBuffer.wrap(packInt(val, MIN_SIZE_LENGTH)));
}
/**
* Get the Date value of this element
*
* @return Date of this element
*/
public Date getDate()
{
/*
* Date begin = new Date(0); Date start = new Date(1970, 1, 1, 0, 0, 0); Date end = new Date(2001, 1, 1, 0, 0, 0); long diff0 = begin.getTime();
* long diff1 = start.getTime(); long diff2 = end.getTime(); long diff3 = Date.UTC(2001, 1, 1, 0, 0, 0) - Date.UTC(1970, 1, 1, 0, 0, 0);
*/
long val = getValue();
val = val / 1000000 + UnixEpochDelay;
return new Date(val);
}
/**
* It's not recommended to use this method. Use the setDate(Date) method instead.
*/
@Override
public void setValue(final long value)
{
setData(ByteBuffer.wrap(packInt(value, MIN_SIZE_LENGTH)));
}
}