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, TimeOfDayD = TimeOfDay; 17 18 struct DateTime { 19 20 public Date date; 21 public TimeOfDay timeOfDay; 22 23 public inout @property DateTimeD dateTime() { 24 return DateTimeD(this.date, this.timeOfDay.timeOfDay); 25 } 26 27 alias dateTime this; 28 29 public static pure DateTime fromISOExtString(string str) { 30 Duration frac; 31 if(str.length > 19 && str[19] == '.') { 32 frac = dur!"msecs"(to!ulong(str[20..$])); 33 str = str[0..19]; 34 } 35 auto dt = DateTimeD.fromISOExtString(str); 36 return DateTime(dt.date, TimeOfDay(dt.timeOfDay, frac)); 37 } 38 39 public inout string toISOExtString() { 40 return this.date.toISOExtString() ~ "T" ~ this.timeOfDay.toString(); 41 } 42 43 } 44 45 struct TimeOfDay { 46 47 public TimeOfDayD timeOfDay; 48 public Duration fracSecs; 49 50 alias timeOfDay this; 51 52 public static pure TimeOfDay fromISOExtString(string str) { 53 Duration frac; 54 if(str.length > 8 && str[8] == '.') { 55 frac = dur!"msecs"(to!ulong(str[9..$])); 56 str = str[0..8]; 57 } 58 return TimeOfDay(TimeOfDayD.fromISOExtString(str), frac); 59 } 60 61 public inout string toISOExtString() { 62 immutable msecs = this.fracSecs.total!"msecs"; 63 if(msecs != 0) { 64 return this.timeOfDay.toISOExtString() ~ "." ~ to!string(msecs); 65 } else { 66 return this.timeOfDay.toISOExtString(); 67 } 68 } 69 70 }