Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #763: Convert.d

File Convert.d, 1.0 kB (added by schveiguy, 4 years ago)

Proposed module to handle time conversions between different time systems

Line 
1 module tango.time.Convert;
2
3 public import tango.time.Time;
4
5 public import tango.sys.win32.Types.di : FILETIME;
6
7 long toJavaTime(Time t)
8 in
9 {
10   // TODO: does java allow negative time?
11   assert(t >= Time.epoch1970);
12 }
13 body
14 {
15   return (t - Time.epoch1970).millis;
16 }
17
18 Time fromJavaTime(long millis)
19 in
20 {
21   // TODO: does java allow negative time?
22   assert(millis >= 0);
23 }
24 body
25 {
26   return Time.epoch1970 + TimeSpan.fromMillis(millis);
27 }
28
29 ulong toUnixTime(Time t)
30 {
31   return cast(ulong)toJavaTime(t) / 1000;
32 }
33
34 Time fromUnixTime(ulong secs)
35 {
36   return fromJavaTime(cast(long)secs * 1000);
37 }
38
39 FILETIME toFILETIME(Time t)
40 in
41 {
42   assert(t >= Time.epoch1601);
43 }
44 body
45 {
46   FILETIME result;
47   ulong *ul = cast(ulong *)&result;
48   *ul = (t - Time.epoch1601).ticks;
49   return result;
50 }
51
52 Time fromFILETIME(FILETIME ft)
53 in
54 {
55   long *l = cast(long *)&ft;
56   assert(*l >= 0);
57 }
58 body
59 {
60   ulong *ul = cast(ulong *)&ft;
61   return Time.epoch1601 + TimeSpan(*ul);
62 }