1 module plist.tests.date; 2 import plist; 3 import plist.types; 4 5 unittest { 6 /* Can we even parse the date? */ 7 { 8 string xml = `<?xml version="1.0" encoding="UTF-8"?> 9 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 10 <plist version="1.0"> 11 <date>1970-01-01T00:00:00Z</date> 12 </plist>`; 13 Plist parse = new Plist(); 14 parse.read(xml); 15 assert(parse.length == 1, "Expected length of 1"); 16 17 assert(parse.write() == xml, "Writing doesn't output the same what we put in"); 18 assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_DATE, "Expected boolean at index 0"); 19 20 PlistElementDate date = cast(PlistElementDate)parse[0]; 21 22 assert(date.value.toUnixTime() == 0, "Timestamp was incorrect"); 23 } 24 /* Test a random date */ 25 { 26 string xml = `<?xml version="1.0" encoding="UTF-8"?> 27 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 28 <plist version="1.0"> 29 <date>2020-04-20T13:37:00Z</date> 30 </plist>`; 31 Plist parse = new Plist(); 32 parse.read(xml); 33 assert(parse.length == 1, "Expected length of 1"); 34 35 assert(parse.write() == xml, "Writing doesn't output the same what we put in"); 36 assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_DATE, "Expected boolean at index 0"); 37 38 PlistElementDate date = cast(PlistElementDate)parse[0]; 39 40 assert(date.value.toUnixTime() == 1587389820, "Timestamp was incorrect"); 41 } 42 /* Try setting the date */ 43 { 44 string xml = `<?xml version="1.0" encoding="UTF-8"?> 45 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 46 <plist version="1.0"> 47 <date>1970-01-01T00:00:00Z</date> 48 </plist>`; 49 string expected = `<?xml version="1.0" encoding="UTF-8"?> 50 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 51 <plist version="1.0"> 52 <date>2020-04-20T13:37:00Z</date> 53 </plist>`; 54 55 Plist parse = new Plist(); 56 parse.read(xml); 57 assert(parse.length == 1, "Expected length of 1"); 58 59 assert(parse.write() == xml, "Writing doesn't output the same what we put in"); 60 assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_DATE, "Expected boolean at index 0"); 61 62 PlistElementDate date = cast(PlistElementDate)parse[0]; 63 64 assert(date.value.toUnixTime() == 0, "Timestamp was incorrect"); 65 import std.datetime; 66 auto time = SysTime.fromUnixTime(1587389820, UTC()); 67 date.value = time; 68 assert(parse.write() == expected, "Expected a different value"); 69 } 70 /* Test something invalid */ 71 { 72 string xml = `<?xml version="1.0" encoding="UTF-8"?> 73 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 74 <plist version="1.0"> 75 <date><string></string></date> 76 </plist>`; 77 bool error = false; 78 Plist parse = new Plist(); 79 try { 80 parse.read(xml); 81 } catch(PlistParsingException e) { 82 error = true; 83 } 84 85 assert(error, "Expected an exception to be thrown"); 86 } 87 /* An empty date */ 88 { 89 string xml = `<?xml version="1.0" encoding="UTF-8"?> 90 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 91 <plist version="1.0"> 92 <date></date> 93 </plist>`; 94 string expected = `<?xml version="1.0" encoding="UTF-8"?> 95 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 96 <plist version="1.0"> 97 <date>0001-01-01T00:00:00+00:00</date> 98 </plist>`; 99 Plist parse = new Plist(); 100 parse.read(xml); 101 assert(parse.length == 1, "Expected length of 1"); 102 103 assert(parse.write() == expected, "Writing doesn't output the same what we put in"); 104 assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_DATE, "Expected boolean at index 0"); 105 106 PlistElementDate date = cast(PlistElementDate)parse[0]; 107 108 } 109 110 }