1 // Written in the D programming language.
2 
3 /**
4  * 
5  * Custom types for TOML's datetimes that add fractional time to D ones.
6  *
7  * License: $(HTTP https://github.com/Kripth/toml/blob/master/LICENSE, MIT)
8  * Authors: Kripth
9  * References: $(LINK https://github.com/toml-lang/toml/blob/master/README.md)
10  * Source: $(HTTP https://github.com/Kripth/toml/blob/master/src/toml/datetime.d, toml/_datetime.d)
11  * 
12  */
13 module toml.datetime;
14 
15 import std.conv : to;
16 import std.datetime : Duration, dur, DateTimeD = DateTime, Date,
17 	TimeOfDayD = TimeOfDay;
18 
19 struct DateTime
20 {
21 
22 	public Date date;
23 	public TimeOfDay timeOfDay;
24 
25 	public inout @property DateTimeD dateTime()
26 	{
27 		return DateTimeD(this.date, this.timeOfDay.timeOfDay);
28 	}
29 
30 	alias dateTime this;
31 
32 	public static pure DateTime fromISOExtString(string str)
33 	{
34 		Duration frac;
35 		if (str.length > 19 && str[19] == '.')
36 		{
37 			frac = dur!"msecs"(to!ulong(str[20 .. $]));
38 			str = str[0 .. 19];
39 		}
40 		auto dt = DateTimeD.fromISOExtString(str);
41 		return DateTime(dt.date, TimeOfDay(dt.timeOfDay, frac));
42 	}
43 
44 	public inout string toISOExtString()
45 	{
46 		return this.date.toISOExtString() ~ "T" ~ this.timeOfDay.toString();
47 	}
48 
49 }
50 
51 struct TimeOfDay
52 {
53 
54 	public TimeOfDayD timeOfDay;
55 	public Duration fracSecs;
56 
57 	alias timeOfDay this;
58 
59 	public static pure TimeOfDay fromISOExtString(string str)
60 	{
61 		Duration frac;
62 		if (str.length > 8 && str[8] == '.')
63 		{
64 			frac = dur!"msecs"(to!ulong(str[9 .. $]));
65 			str = str[0 .. 8];
66 		}
67 		return TimeOfDay(TimeOfDayD.fromISOExtString(str), frac);
68 	}
69 
70 	public inout string toISOExtString()
71 	{
72 		immutable msecs = this.fracSecs.total!"msecs";
73 		if (msecs != 0)
74 		{
75 			return this.timeOfDay.toISOExtString() ~ "." ~ to!string(msecs);
76 		}
77 		else
78 		{
79 			return this.timeOfDay.toISOExtString();
80 		}
81 	}
82 
83 }