8
8
import org .joda .time .DateTime ;
9
9
import org .junit .Test ;
10
10
11
+ import java .text .SimpleDateFormat ;
12
+ import java .util .Locale ;
13
+
14
+ import static org .andstatus .todoagenda .prefs .dateformat .DateFormatter .toJavaDate ;
11
15
import static org .junit .Assert .assertEquals ;
12
16
13
17
/**
16
20
public class DateFormatterTest extends BaseWidgetTest {
17
21
18
22
@ Test
19
- public void timeTimeZones () {
23
+ public void testTimeZones () {
20
24
InstanceSettings settings = getSettings ();
21
25
settings .clock ().setSnapshotMode (SnapshotMode .SNAPSHOT_TIME );
22
26
assertNow ("2020-02-15T01:00:00.000+08:00" );
@@ -58,21 +62,41 @@ public void customPatterns() {
58
62
DateTime now = settings .clock ().now ().withTimeAtStartOfDay ().plusHours (1 );
59
63
provider .setExecutedAt (now );
60
64
61
- assertPattern (now , "yyyy-MM-dd b" , String .format ("%04d-%02d-%02d" , now .yearOfEra ().get (),
62
- now .monthOfYear ().get (), now .dayOfMonth ().get ()) + " 0" );
65
+ CharSequence todayText = provider .getContext ().getText (R .string .today );
66
+ String tomorrowText = provider .getContext ().getText (R .string .tomorrow ).toString ();
67
+ String inTwoDaysText = String .format (provider .getContext ().getText (R .string .in_N_days ).toString (), 2 );
68
+
69
+ String javaPattern = "yyyy-MM-dd" ;
70
+ String javaFormatted = String .format ("%04d-%02d-%02d" , now .yearOfEra ().get (),
71
+ now .monthOfYear ().get (), now .dayOfMonth ().get ());
72
+ assertEquals (javaFormatted , javaFormatted (javaPattern , now ));
73
+ assertPattern (now , javaPattern , javaFormatted );
74
+ assertPattern (now , javaPattern + " b" , javaFormatted + " 0" );
75
+ assertPattern (now , "BBB " + javaPattern + " b" , todayText + " " + javaFormatted );
76
+ assertPattern (now .plusDays (1 ), "BBB " + javaPattern + " b" , tomorrowText + " " +
77
+ javaFormatted (javaPattern , now .plusDays (1 )));
78
+ assertPattern (now .plusDays (2 ), "BBB " + javaPattern + " b" ,
79
+ javaFormatted (javaPattern , now .plusDays (2 )) + " 2" );
80
+
81
+ assertPattern (now , "BBB " + javaPattern + " BBBB" , todayText + " " + javaFormatted );
82
+ assertPattern (now .plusDays (1 ), "BBB " + javaPattern + " BBBB" , tomorrowText + " " +
83
+ javaFormatted (javaPattern , now .plusDays (1 )));
84
+ assertPattern (now .plusDays (2 ), "BBB " + javaPattern + " BBBB" ,
85
+ javaFormatted (javaPattern , now .plusDays (2 )) + " " + inTwoDaysText );
86
+
63
87
assertPattern (now , "b" , "0" );
64
88
assertPattern (now , "MM-dd bb" , String .format ("%02d-%02d" , now .monthOfYear ().get (), now .dayOfMonth ().get ()) + " 00" );
65
- assertPattern (now .plusDays (1 ), "" , "" );
89
+ assertPattern (now .plusDays (1 ), "" , "(not implemented: " +
90
+ DateFormatValue .of (DateFormatType .CUSTOM , "" ).getSummary (provider .getContext ()) + ")" );
66
91
assertPattern (now .plusDays (1 ), "b" , "1" );
67
92
assertPattern (now .plusDays (1 ), "bbb" , "001" );
68
- CharSequence tomorrowText = provider .getContext ().getText (R .string .tomorrow );
69
- assertPattern (now .plusDays (1 ), "bbbb" , tomorrowText .toString ());
70
- assertPattern (now .plusDays (1 ), "BBB" , tomorrowText .toString ());
71
- assertPattern (now .plusDays (1 ), "BBBB" , tomorrowText .toString ());
93
+ assertPattern (now .plusDays (1 ), "bbbb" , tomorrowText );
94
+ assertPattern (now .plusDays (1 ), "BBB" , tomorrowText );
95
+ assertPattern (now .plusDays (1 ), "BBBB" , tomorrowText );
72
96
assertPattern (now .plusDays (-2 ), "BBB" , "" );
73
97
assertPattern (now .plusDays (-2 ), "BBBB" , String .format (provider .getContext ().getText (R .string .N_days_ago ).toString (), 2 ));
74
98
assertPattern (now .plusDays (2 ), "BBB" , "" );
75
- assertPattern (now .plusDays (2 ), "BBBB" , String . format ( provider . getContext (). getText ( R . string . in_N_days ). toString (), 2 ) );
99
+ assertPattern (now .plusDays (2 ), "BBBB" , inTwoDaysText );
76
100
assertPattern (now .plusDays (5 ), "b" , "5" );
77
101
assertPattern (now .plusDays (5 ), "bbb" , "005" );
78
102
assertPattern (now .plusDays (5 ), "bbbb" , "5" );
@@ -81,6 +105,11 @@ public void customPatterns() {
81
105
assertPattern (now .minusDays (5 ), "bbbb" , "-5" );
82
106
}
83
107
108
+ private static String javaFormatted (String javaPattern , DateTime date ) {
109
+ Locale locale = Locale .getDefault ();
110
+ return new SimpleDateFormat (javaPattern , locale ).format (toJavaDate (date ));
111
+ }
112
+
84
113
private void assertPattern (DateTime date , String pattern , String expected ) {
85
114
DateFormatValue format = DateFormatValue .of (DateFormatType .CUSTOM , pattern );
86
115
DateTime now = getSettings ().clock ().now ();
0 commit comments