Skip to content

Commit 7e612cb

Browse files
committed
Merge pull request OfficeDev#396 from cfraser/bug/TimeWindowUnscopedDatesOnlyUTC
Fix: OfficeDev#241 TimeWindow.writeToXmlUnscopedDatesOnly should have UTC set on formatter
2 parents 3c8c216 + 9beddf2 commit 7e612cb

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

src/main/java/microsoft/exchange/webservices/data/misc/availability/TimeWindow.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.text.DateFormat;
3636
import java.text.SimpleDateFormat;
3737
import java.util.Date;
38+
import java.util.TimeZone;
3839

3940
/**
4041
* Represents a time period.
@@ -160,6 +161,7 @@ protected void writeToXmlUnscopedDatesOnly(EwsServiceXmlWriter writer,
160161
final String DateOnlyFormat = "yyyy-MM-dd'T'00:00:00";
161162

162163
DateFormat formatter = new SimpleDateFormat(DateOnlyFormat);
164+
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
163165

164166
String start = formatter.format(this.startTime);
165167
String end = formatter.format(this.endTime);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* The MIT License
3+
* Copyright (c) 2012 Microsoft Corporation
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package microsoft.exchange.webservices.data.misc.availability;
25+
26+
import microsoft.exchange.webservices.base.BaseTest;
27+
import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
28+
import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
29+
import microsoft.exchange.webservices.data.core.XmlElementNames;
30+
import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
31+
import microsoft.exchange.webservices.data.security.XmlNodeType;
32+
import org.junit.Assert;
33+
import org.junit.Test;
34+
35+
import java.io.ByteArrayInputStream;
36+
import java.io.ByteArrayOutputStream;
37+
import java.io.InputStream;
38+
import java.util.Date;
39+
40+
public class TimeWindowTest extends BaseTest {
41+
42+
@Test
43+
public void testWriteToXmlUnscopedDatesOnlyUsesUTC() {
44+
// Thu, 01 Jan 2015 0:0:00 UTC
45+
final Date midnight = new Date(1420070400000l);
46+
// Thu, 01 Jan 2015 23:59:59 GMT
47+
final Date just_before_midnight = new Date(1420156799000l);
48+
49+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
50+
EwsServiceXmlWriter writer;
51+
52+
try {
53+
// build the test xml markup
54+
writer = new EwsServiceXmlWriter(exchangeServiceMock, outputStream);
55+
writer.writeStartDocument();
56+
writer.writeStartElement(XmlNamespace.NotSpecified, "test");
57+
writer.writeAttributeValue("xmlns:" + XmlNamespace.Types.getNameSpacePrefix(), XmlNamespace.Types.getNameSpaceUri());
58+
TimeWindow tw = new TimeWindow();
59+
tw.setStartTime(midnight);
60+
tw.setEndTime(just_before_midnight);
61+
tw.writeToXmlUnscopedDatesOnly(writer, XmlElementNames.Duration);
62+
writer.writeEndElement();
63+
64+
// read the test markup
65+
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
66+
EwsServiceXmlReader reader = new EwsServiceXmlReader(inputStream, exchangeServiceMock);
67+
reader.read(new XmlNodeType(XmlNodeType.START_DOCUMENT));
68+
reader.readStartElement(XmlNamespace.NotSpecified, "test");
69+
reader.readStartElement(XmlNamespace.Types, XmlElementNames.Duration);
70+
TimeWindow checkTw = new TimeWindow();
71+
72+
checkTw.loadFromXml(reader);
73+
74+
// Test that the dates have not shifted.
75+
Assert.assertEquals(midnight, checkTw.getStartTime());
76+
Assert.assertEquals(midnight, checkTw.getEndTime());
77+
} catch (Exception e) {
78+
Assert.fail(e.getMessage());
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)