1
+ /*
2
+ * Copyright 2017 dmfs GmbH
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package org .dmfs .tasks .utils ;
18
+
19
+ import android .support .test .runner .AndroidJUnit4 ;
20
+ import android .text .format .Time ;
21
+
22
+ import org .dmfs .rfc5545 .DateTime ;
23
+ import org .dmfs .rfc5545 .Duration ;
24
+ import org .junit .Test ;
25
+ import org .junit .runner .RunWith ;
26
+
27
+ import java .util .TimeZone ;
28
+
29
+
30
+ /**
31
+ * Test for {@link DateFormatter#toTime(DateTime)} method.
32
+ *
33
+ * @author Gabor Keszthelyi
34
+ */
35
+ @ RunWith (AndroidJUnit4 .class )
36
+ public class DateTimeToTimeConversionTest
37
+ {
38
+
39
+ @ Test
40
+ public void test_toTime_withVariousDateTimes ()
41
+ {
42
+ assertCorrectlyConverted (DateTime .now ());
43
+
44
+ assertCorrectlyConverted (DateTime .now (TimeZone .getTimeZone ("UTC+04:00" )));
45
+
46
+ assertCorrectlyConverted (DateTime .nowAndHere ());
47
+
48
+ assertCorrectlyConverted (new DateTime (1509473781000L ));
49
+
50
+ assertCorrectlyConverted (new DateTime (1509473781000L ).addDuration (new Duration (1 , 1 , 0 )));
51
+
52
+ assertCorrectlyConverted (DateTime .now (TimeZone .getTimeZone ("UTC+04:00" )).shiftTimeZone (TimeZone .getTimeZone ("UTC+05:00" )));
53
+
54
+ // Floating, all-day
55
+ assertCorrectlyConverted (DateTime .now ().toAllDay ());
56
+
57
+ // Not DST (March 2017 in Hungary):
58
+ assertCorrectlyConverted (new DateTime (TimeZone .getTimeZone ("Europe/Budapest" ), 2017 , 2 - 1 , 7 , 15 , 0 , 0 ));
59
+ assertCorrectlyConverted (new DateTime (2017 , 2 - 1 , 7 , 15 , 0 , 0 ).shiftTimeZone (TimeZone .getTimeZone ("Europe/Budapest" )));
60
+ assertCorrectlyConverted (new DateTime (2017 , 2 - 1 , 7 , 15 , 0 , 0 ).swapTimeZone (TimeZone .getTimeZone ("Europe/Budapest" )));
61
+
62
+ // DST (July 2017 in Hungary):
63
+ assertCorrectlyConverted (new DateTime (TimeZone .getTimeZone ("Europe/Budapest" ), 2017 , 7 - 1 , 7 , 15 , 0 , 0 ));
64
+ assertCorrectlyConverted (new DateTime (2017 , 7 - 1 , 7 , 15 , 0 , 0 ).shiftTimeZone (TimeZone .getTimeZone ("Europe/Budapest" )));
65
+ assertCorrectlyConverted (new DateTime (2017 , 7 - 1 , 7 , 15 , 0 , 0 ).swapTimeZone (TimeZone .getTimeZone ("Europe/Budapest" )));
66
+ }
67
+
68
+
69
+ @ Test (expected = IllegalArgumentException .class )
70
+ public void test_toTime_forFloatingButNotAllDayDateTime_throwsSinceItIsNotSupported ()
71
+ {
72
+ new DateFormatter (null ).toTime (new DateTime (2017 , 7 - 1 , 7 , 15 , 0 , 0 ));
73
+ }
74
+
75
+
76
+ private void assertCorrectlyConverted (DateTime dateTime )
77
+ {
78
+ Time time = new DateFormatter (null ).toTime (dateTime );
79
+ if (!isEquivalentDateTimeAndTime (dateTime , time ))
80
+ {
81
+ throw new AssertionError (String .format ("DateTime=%s and Time=%s are not equivalent" , dateTime , time ));
82
+ }
83
+ }
84
+
85
+
86
+ /**
87
+ * Contains the definition/requirement of when a {@link DateTime} and {@link Time} is considered equivalent in this project.
88
+ */
89
+ private boolean isEquivalentDateTimeAndTime (DateTime dateTime , Time time )
90
+ {
91
+ // Time doesn't seem to store in millis precision, there is a 1000 multiplier internally when calculating millis,
92
+ // so we can only compare to this precision:
93
+ boolean millisMatch =
94
+ dateTime .getTimestamp () / 1000
95
+ ==
96
+ time .toMillis (false ) / 1000 ;
97
+
98
+ boolean allDaysMatch = time .allDay == dateTime .isAllDay ();
99
+
100
+ boolean timeZoneMatch =
101
+ // If DateTime is floating, all-day then if the all-day flag is matched with Time (checked earlier)
102
+ // then we consider the Time's timezone matching, we ignore that basically,
103
+ // because Time always has a time zone, and there is no other way to represent all-day date-times with Time.
104
+ (dateTime .isFloating () && dateTime .isAllDay ())
105
+ ||
106
+ // This is the regular case with non-floating DateTime
107
+ (dateTime .getTimeZone () != null && time .timezone .equals (dateTime .getTimeZone ().getID ()));
108
+
109
+ return millisMatch && allDaysMatch && timeZoneMatch ;
110
+ }
111
+
112
+ }
0 commit comments