summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/icu/RelativeDateTimeFormatterTest.java
blob: 101896f4c04c27f73747ded4587f7fb330c81893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.icu;

import android.icu.util.ULocale;

import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import static libcore.icu.DateUtilsBridge.FORMAT_ABBREV_ALL;
import static libcore.icu.DateUtilsBridge.FORMAT_ABBREV_RELATIVE;
import static libcore.icu.DateUtilsBridge.FORMAT_NO_YEAR;
import static libcore.icu.DateUtilsBridge.FORMAT_NUMERIC_DATE;
import static libcore.icu.DateUtilsBridge.FORMAT_SHOW_YEAR;
import static libcore.icu.RelativeDateTimeFormatter.DAY_IN_MILLIS;
import static libcore.icu.RelativeDateTimeFormatter.HOUR_IN_MILLIS;
import static libcore.icu.RelativeDateTimeFormatter.MINUTE_IN_MILLIS;
import static libcore.icu.RelativeDateTimeFormatter.SECOND_IN_MILLIS;
import static libcore.icu.RelativeDateTimeFormatter.WEEK_IN_MILLIS;
import static libcore.icu.RelativeDateTimeFormatter.YEAR_IN_MILLIS;
import static libcore.icu.RelativeDateTimeFormatter.getRelativeDateTimeString;
import static libcore.icu.RelativeDateTimeFormatter.getRelativeTimeSpanString;

public class RelativeDateTimeFormatterTest extends junit.framework.TestCase {

  // Tests adopted from CTS tests for DateUtils.getRelativeTimeSpanString.
  public void test_getRelativeTimeSpanStringCTS() throws Exception {
    Locale en_US = new Locale("en", "US");
    TimeZone tz = TimeZone.getTimeZone("GMT");
    Calendar cal = Calendar.getInstance(tz, en_US);
    // Feb 5, 2015 at 10:50 GMT
    cal.set(2015, Calendar.FEBRUARY, 5, 10, 50, 0);
    final long baseTime = cal.getTimeInMillis();

    assertEquals("0 minutes ago",
                 getRelativeTimeSpanString(en_US, tz, baseTime - SECOND_IN_MILLIS, baseTime,
                                           MINUTE_IN_MILLIS, 0));
    assertEquals("In 0 minutes",
                 getRelativeTimeSpanString(en_US, tz, baseTime + SECOND_IN_MILLIS, baseTime,
                                           MINUTE_IN_MILLIS, 0));

    assertEquals("1 minute ago",
                 getRelativeTimeSpanString(en_US, tz, 0, MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, 0));
    assertEquals("In 1 minute",
                 getRelativeTimeSpanString(en_US, tz, MINUTE_IN_MILLIS, 0, MINUTE_IN_MILLIS, 0));

    assertEquals("42 minutes ago",
      getRelativeTimeSpanString(en_US, tz, baseTime - 42 * MINUTE_IN_MILLIS, baseTime,
                                MINUTE_IN_MILLIS, 0));
    assertEquals("In 42 minutes",
      getRelativeTimeSpanString(en_US, tz, baseTime + 42 * MINUTE_IN_MILLIS, baseTime,
                                MINUTE_IN_MILLIS, 0));

    final long TWO_HOURS_IN_MS = 2 * HOUR_IN_MILLIS;
    assertEquals("2 hours ago",
                 getRelativeTimeSpanString(en_US, tz, baseTime - TWO_HOURS_IN_MS, baseTime,
                                           MINUTE_IN_MILLIS, FORMAT_NUMERIC_DATE));
    assertEquals("In 2 hours",
                 getRelativeTimeSpanString(en_US, tz, baseTime + TWO_HOURS_IN_MS, baseTime,
                                           MINUTE_IN_MILLIS, FORMAT_NUMERIC_DATE));

    assertEquals("In 42 min.",
                 getRelativeTimeSpanString(en_US, tz, baseTime + (42 * MINUTE_IN_MILLIS), baseTime,
                                           MINUTE_IN_MILLIS, FORMAT_ABBREV_RELATIVE));

    assertEquals("Tomorrow",
                 getRelativeTimeSpanString(en_US, tz, DAY_IN_MILLIS, 0, DAY_IN_MILLIS, 0));
    assertEquals("In 2 days",
                 getRelativeTimeSpanString(en_US, tz, 2 * DAY_IN_MILLIS, 0, DAY_IN_MILLIS, 0));
    assertEquals("Yesterday",
                 getRelativeTimeSpanString(en_US, tz, 0, DAY_IN_MILLIS, DAY_IN_MILLIS, 0));
    assertEquals("2 days ago",
                 getRelativeTimeSpanString(en_US, tz, 0, 2 * DAY_IN_MILLIS, DAY_IN_MILLIS, 0));

    final long DAY_DURATION = 5 * 24 * 60 * 60 * 1000;
    assertEquals("5 days ago",
                 getRelativeTimeSpanString(en_US, tz, baseTime - DAY_DURATION, baseTime,
                                           DAY_IN_MILLIS, 0));
  }

  private void test_getRelativeTimeSpanString_helper(long delta, long minResolution, int flags,
                                                     String expectedInPast,
                                                     String expectedInFuture) throws Exception {
    Locale en_US = new Locale("en", "US");
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = Calendar.getInstance(tz, en_US);
    // Feb 5, 2015 at 10:50 PST
    cal.set(2015, Calendar.FEBRUARY, 5, 10, 50, 0);
    final long base = cal.getTimeInMillis();

    assertEquals(expectedInPast,
                 getRelativeTimeSpanString(en_US, tz, base - delta, base, minResolution, flags));
    assertEquals(expectedInFuture,
                 getRelativeTimeSpanString(en_US, tz, base + delta, base, minResolution, flags));
  }

  private void test_getRelativeTimeSpanString_helper(long delta, long minResolution,
                                                     String expectedInPast,
                                                     String expectedInFuture) throws Exception {
    test_getRelativeTimeSpanString_helper(delta, minResolution, 0, expectedInPast, expectedInFuture);
  }

  public void test_getRelativeTimeSpanString() throws Exception {

    test_getRelativeTimeSpanString_helper(0 * SECOND_IN_MILLIS, 0, "0 seconds ago", "0 seconds ago");
    test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, 0, "1 minute ago", "In 1 minute");
    test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, 0, "1 minute ago", "In 1 minute");
    test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, 0, "5 days ago", "In 5 days");

    test_getRelativeTimeSpanString_helper(0 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "0 seconds ago",
                                          "0 seconds ago");
    test_getRelativeTimeSpanString_helper(1 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "1 second ago",
                                          "In 1 second");
    test_getRelativeTimeSpanString_helper(2 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "2 seconds ago",
                                          "In 2 seconds");
    test_getRelativeTimeSpanString_helper(25 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "25 seconds ago",
                                          "In 25 seconds");
    test_getRelativeTimeSpanString_helper(75 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "1 minute ago",
                                          "In 1 minute");
    test_getRelativeTimeSpanString_helper(5000 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, "1 hour ago",
                                          "In 1 hour");

    test_getRelativeTimeSpanString_helper(0 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "0 minutes ago",
                                          "0 minutes ago");
    test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "1 minute ago",
                                          "In 1 minute");
    test_getRelativeTimeSpanString_helper(2 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "2 minutes ago",
                                          "In 2 minutes");
    test_getRelativeTimeSpanString_helper(25 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "25 minutes ago",
                                          "In 25 minutes");
    test_getRelativeTimeSpanString_helper(75 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "1 hour ago",
                                          "In 1 hour");
    test_getRelativeTimeSpanString_helper(720 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, "12 hours ago",
                                          "In 12 hours");

    test_getRelativeTimeSpanString_helper(0 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "0 hours ago",
                                          "0 hours ago");
    test_getRelativeTimeSpanString_helper(1 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "1 hour ago",
                                          "In 1 hour");
    test_getRelativeTimeSpanString_helper(2 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "2 hours ago",
                                          "In 2 hours");
    test_getRelativeTimeSpanString_helper(5 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "5 hours ago",
                                          "In 5 hours");
    test_getRelativeTimeSpanString_helper(20 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, "20 hours ago",
                                          "In 20 hours");

    test_getRelativeTimeSpanString_helper(0 * DAY_IN_MILLIS, DAY_IN_MILLIS, "Today", "Today");
    test_getRelativeTimeSpanString_helper(20 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Yesterday",
                                          "Tomorrow");
    test_getRelativeTimeSpanString_helper(24 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Yesterday",
                                          "Tomorrow");
    test_getRelativeTimeSpanString_helper(2 * DAY_IN_MILLIS, DAY_IN_MILLIS, "2 days ago",
                                          "In 2 days");
    test_getRelativeTimeSpanString_helper(25 * DAY_IN_MILLIS, DAY_IN_MILLIS, "January 11",
                                          "March 2");

    test_getRelativeTimeSpanString_helper(0 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, "0 weeks ago",
                                          "0 weeks ago");
    test_getRelativeTimeSpanString_helper(1 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, "1 week ago",
                                          "In 1 week");
    test_getRelativeTimeSpanString_helper(2 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, "2 weeks ago",
                                          "In 2 weeks");
    test_getRelativeTimeSpanString_helper(25 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, "25 weeks ago",
                                          "In 25 weeks");

    // duration >= minResolution
    test_getRelativeTimeSpanString_helper(30 * SECOND_IN_MILLIS, 0, "30 seconds ago",
                                          "In 30 seconds");
    test_getRelativeTimeSpanString_helper(30 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS,
                                          "30 minutes ago", "In 30 minutes");
    test_getRelativeTimeSpanString_helper(30 * HOUR_IN_MILLIS, MINUTE_IN_MILLIS, "Yesterday",
                                          "Tomorrow");
    test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, MINUTE_IN_MILLIS, "5 days ago",
                                          "In 5 days");
    test_getRelativeTimeSpanString_helper(30 * WEEK_IN_MILLIS, MINUTE_IN_MILLIS, "July 10, 2014",
                                          "September 3");
    test_getRelativeTimeSpanString_helper(5 * 365 * DAY_IN_MILLIS, MINUTE_IN_MILLIS,
                                          "February 6, 2010", "February 4, 2020");

    test_getRelativeTimeSpanString_helper(60 * SECOND_IN_MILLIS, MINUTE_IN_MILLIS, "1 minute ago",
                                          "In 1 minute");
    test_getRelativeTimeSpanString_helper(120 * SECOND_IN_MILLIS - 1, MINUTE_IN_MILLIS,
                                          "1 minute ago", "In 1 minute");
    test_getRelativeTimeSpanString_helper(60 * MINUTE_IN_MILLIS, HOUR_IN_MILLIS, "1 hour ago",
                                          "In 1 hour");
    test_getRelativeTimeSpanString_helper(120 * MINUTE_IN_MILLIS - 1, HOUR_IN_MILLIS, "1 hour ago",
                                          "In 1 hour");
    test_getRelativeTimeSpanString_helper(2 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Today", "Today");
    test_getRelativeTimeSpanString_helper(12 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Yesterday",
                                          "Today");
    test_getRelativeTimeSpanString_helper(24 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "Yesterday",
                                          "Tomorrow");
    test_getRelativeTimeSpanString_helper(48 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "2 days ago",
                                          "In 2 days");
    test_getRelativeTimeSpanString_helper(45 * HOUR_IN_MILLIS, DAY_IN_MILLIS, "2 days ago",
                                          "In 2 days");
    test_getRelativeTimeSpanString_helper(7 * DAY_IN_MILLIS, WEEK_IN_MILLIS, "1 week ago",
                                          "In 1 week");
    test_getRelativeTimeSpanString_helper(14 * DAY_IN_MILLIS - 1, WEEK_IN_MILLIS, "1 week ago",
                                          "In 1 week");

    // duration < minResolution
    test_getRelativeTimeSpanString_helper(59 * SECOND_IN_MILLIS, MINUTE_IN_MILLIS, "0 minutes ago",
                                          "In 0 minutes");
    test_getRelativeTimeSpanString_helper(59 * MINUTE_IN_MILLIS, HOUR_IN_MILLIS, "0 hours ago",
                                          "In 0 hours");
    test_getRelativeTimeSpanString_helper(HOUR_IN_MILLIS - 1, HOUR_IN_MILLIS, "0 hours ago",
                                          "In 0 hours");
    test_getRelativeTimeSpanString_helper(DAY_IN_MILLIS - 1, DAY_IN_MILLIS, "Yesterday",
                                          "Tomorrow");
    test_getRelativeTimeSpanString_helper(20 * SECOND_IN_MILLIS, WEEK_IN_MILLIS, "0 weeks ago",
                                          "In 0 weeks");
    test_getRelativeTimeSpanString_helper(WEEK_IN_MILLIS - 1, WEEK_IN_MILLIS, "0 weeks ago",
                                          "In 0 weeks");
  }

  public void test_getRelativeTimeSpanStringAbbrev() throws Exception {
    int flags = FORMAT_ABBREV_RELATIVE;

    test_getRelativeTimeSpanString_helper(0 * SECOND_IN_MILLIS, 0, flags, "0 sec. ago",
                                          "0 sec. ago");
    test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, 0, flags, "1 min. ago",
                                          "In 1 min.");
    test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, 0, flags, "5 days ago", "In 5 days");

    test_getRelativeTimeSpanString_helper(0 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags,
                                          "0 sec. ago", "0 sec. ago");
    test_getRelativeTimeSpanString_helper(1 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags,
                                          "1 sec. ago", "In 1 sec.");
    test_getRelativeTimeSpanString_helper(2 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags,
                                          "2 sec. ago", "In 2 sec.");
    test_getRelativeTimeSpanString_helper(25 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags,
                                          "25 sec. ago", "In 25 sec.");
    test_getRelativeTimeSpanString_helper(75 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags,
                                          "1 min. ago", "In 1 min.");
    test_getRelativeTimeSpanString_helper(5000 * SECOND_IN_MILLIS, SECOND_IN_MILLIS, flags,
                                          "1 hr. ago", "In 1 hr.");

    test_getRelativeTimeSpanString_helper(0 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "0 min. ago", "0 min. ago");
    test_getRelativeTimeSpanString_helper(1 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "1 min. ago", "In 1 min.");
    test_getRelativeTimeSpanString_helper(2 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "2 min. ago", "In 2 min.");
    test_getRelativeTimeSpanString_helper(25 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "25 min. ago", "In 25 min.");
    test_getRelativeTimeSpanString_helper(75 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "1 hr. ago", "In 1 hr.");
    test_getRelativeTimeSpanString_helper(720 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "12 hr. ago", "In 12 hr.");

    test_getRelativeTimeSpanString_helper(0 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags,
                                          "0 hr. ago", "0 hr. ago");
    test_getRelativeTimeSpanString_helper(1 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags,
                                          "1 hr. ago", "In 1 hr.");
    test_getRelativeTimeSpanString_helper(2 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags,
                                          "2 hr. ago", "In 2 hr.");
    test_getRelativeTimeSpanString_helper(5 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags,
                                          "5 hr. ago", "In 5 hr.");
    test_getRelativeTimeSpanString_helper(20 * HOUR_IN_MILLIS, HOUR_IN_MILLIS, flags,
                                          "20 hr. ago", "In 20 hr.");

    test_getRelativeTimeSpanString_helper(0 * DAY_IN_MILLIS, DAY_IN_MILLIS, flags, "Today",
                                          "Today");
    test_getRelativeTimeSpanString_helper(20 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags,
                                          "Yesterday", "Tomorrow");
    test_getRelativeTimeSpanString_helper(24 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags,
                                          "Yesterday", "Tomorrow");
    test_getRelativeTimeSpanString_helper(2 * DAY_IN_MILLIS, DAY_IN_MILLIS, flags,
                                          "2 days ago", "In 2 days");
    test_getRelativeTimeSpanString_helper(25 * DAY_IN_MILLIS, DAY_IN_MILLIS, flags,
                                          "January 11", "March 2");

    test_getRelativeTimeSpanString_helper(0 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, flags,
                                          "0 wk. ago", "0 wk. ago");
    test_getRelativeTimeSpanString_helper(1 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, flags,
                                          "1 wk. ago", "In 1 wk.");
    test_getRelativeTimeSpanString_helper(2 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, flags,
                                          "2 wk. ago", "In 2 wk.");
    test_getRelativeTimeSpanString_helper(25 * WEEK_IN_MILLIS, WEEK_IN_MILLIS, flags,
                                          "25 wk. ago", "In 25 wk.");

    // duration >= minResolution
    test_getRelativeTimeSpanString_helper(30 * SECOND_IN_MILLIS, 0, flags, "30 sec. ago",
                                          "In 30 sec.");
    test_getRelativeTimeSpanString_helper(30 * MINUTE_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "30 min. ago", "In 30 min.");
    test_getRelativeTimeSpanString_helper(30 * HOUR_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "Yesterday", "Tomorrow");
    test_getRelativeTimeSpanString_helper(5 * DAY_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "5 days ago", "In 5 days");
    test_getRelativeTimeSpanString_helper(30 * WEEK_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "July 10, 2014", "September 3");
    test_getRelativeTimeSpanString_helper(5 * 365 * DAY_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "February 6, 2010", "February 4, 2020");

    test_getRelativeTimeSpanString_helper(60 * SECOND_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "1 min. ago", "In 1 min.");
    test_getRelativeTimeSpanString_helper(120 * SECOND_IN_MILLIS - 1, MINUTE_IN_MILLIS, flags,
                                          "1 min. ago", "In 1 min.");
    test_getRelativeTimeSpanString_helper(60 * MINUTE_IN_MILLIS, HOUR_IN_MILLIS, flags,
                                          "1 hr. ago", "In 1 hr.");
    test_getRelativeTimeSpanString_helper(120 * MINUTE_IN_MILLIS - 1, HOUR_IN_MILLIS, flags,
                                          "1 hr. ago", "In 1 hr.");
    test_getRelativeTimeSpanString_helper(2 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags, "Today",
                                          "Today");
    test_getRelativeTimeSpanString_helper(12 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags,
                                          "Yesterday", "Today");
    test_getRelativeTimeSpanString_helper(24 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags,
                                          "Yesterday", "Tomorrow");
    test_getRelativeTimeSpanString_helper(48 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags,
                                          "2 days ago", "In 2 days");
    test_getRelativeTimeSpanString_helper(45 * HOUR_IN_MILLIS, DAY_IN_MILLIS, flags,
                                          "2 days ago", "In 2 days");
    test_getRelativeTimeSpanString_helper(7 * DAY_IN_MILLIS, WEEK_IN_MILLIS, flags,
                                          "1 wk. ago", "In 1 wk.");
    test_getRelativeTimeSpanString_helper(14 * DAY_IN_MILLIS - 1, WEEK_IN_MILLIS, flags,
                                          "1 wk. ago", "In 1 wk.");

    // duration < minResolution
    test_getRelativeTimeSpanString_helper(59 * SECOND_IN_MILLIS, MINUTE_IN_MILLIS, flags,
                                          "0 min. ago", "In 0 min.");
    test_getRelativeTimeSpanString_helper(59 * MINUTE_IN_MILLIS, HOUR_IN_MILLIS, flags,
                                          "0 hr. ago", "In 0 hr.");
    test_getRelativeTimeSpanString_helper(HOUR_IN_MILLIS - 1, HOUR_IN_MILLIS, flags,
                                          "0 hr. ago", "In 0 hr.");
    test_getRelativeTimeSpanString_helper(DAY_IN_MILLIS - 1, DAY_IN_MILLIS, flags,
                                          "Yesterday", "Tomorrow");
    test_getRelativeTimeSpanString_helper(20 * SECOND_IN_MILLIS, WEEK_IN_MILLIS, flags,
                                          "0 wk. ago", "In 0 wk.");
    test_getRelativeTimeSpanString_helper(WEEK_IN_MILLIS - 1, WEEK_IN_MILLIS, flags,
                                          "0 wk. ago", "In 0 wk.");

  }

  public void test_getRelativeTimeSpanStringGerman() throws Exception {
    // Bug: 19744876
    // We need to specify the timezone and the time explicitly. Otherwise it
    // may not always give a correct answer of "tomorrow" by using
    // (now + DAY_IN_MILLIS).
    Locale de_DE = new Locale("de", "DE");
    TimeZone tz = TimeZone.getTimeZone("Europe/Berlin");
    Calendar cal = Calendar.getInstance(tz, de_DE);
    // Feb 5, 2015 at 10:50 CET
    cal.set(2015, Calendar.FEBRUARY, 5, 10, 50, 0);
    final long now = cal.getTimeInMillis();

    // 42 minutes ago
    assertEquals("Vor 42 Minuten", getRelativeTimeSpanString(de_DE, tz,
        now - 42 * MINUTE_IN_MILLIS, now, MINUTE_IN_MILLIS, 0));
    // In 42 minutes
    assertEquals("In 42 Minuten", getRelativeTimeSpanString(de_DE, tz,
        now + 42 * MINUTE_IN_MILLIS, now, MINUTE_IN_MILLIS, 0));
    // Yesterday
    assertEquals("Gestern", getRelativeTimeSpanString(de_DE, tz,
        now - DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0));
    // The day before yesterday
    assertEquals("Vorgestern", getRelativeTimeSpanString(de_DE, tz,
        now - 2 * DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0));
    // Tomorrow
    assertEquals("Morgen", getRelativeTimeSpanString(de_DE, tz,
        now + DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0));
    // The day after tomorrow
    assertEquals("Übermorgen", getRelativeTimeSpanString(de_DE, tz,
        now + 2 * DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0));
  }

  public void test_getRelativeTimeSpanStringFrench() throws Exception {
    Locale fr_FR = new Locale("fr", "FR");
    TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
    Calendar cal = Calendar.getInstance(tz, fr_FR);
    // Feb 5, 2015 at 10:50 CET
    cal.set(2015, Calendar.FEBRUARY, 5, 10, 50, 0);
    final long now = cal.getTimeInMillis();

    // 42 minutes ago
    assertEquals("Il y a 42 minutes", getRelativeTimeSpanString(fr_FR, tz,
        now - (42 * MINUTE_IN_MILLIS), now, MINUTE_IN_MILLIS, 0));
    // In 42 minutes
    assertEquals("Dans 42 minutes", getRelativeTimeSpanString(fr_FR, tz,
        now + (42 * MINUTE_IN_MILLIS), now, MINUTE_IN_MILLIS, 0));
    // Yesterday
    assertEquals("Hier", getRelativeTimeSpanString(fr_FR, tz,
        now - DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0));
    // The day before yesterday
    assertEquals("Avant-hier", getRelativeTimeSpanString(fr_FR, tz,
        now - 2 * DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0));
    // Tomorrow
    assertEquals("Demain", getRelativeTimeSpanString(fr_FR, tz,
        now + DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0));
    // The day after tomorrow
    assertEquals("Après-demain", getRelativeTimeSpanString(fr_FR, tz,
        now + 2 * DAY_IN_MILLIS, now, DAY_IN_MILLIS, 0));
  }

  // Tests adopted from CTS tests for DateUtils.getRelativeDateTimeString.
  public void test_getRelativeDateTimeStringCTS() throws Exception {
    Locale en_US = Locale.getDefault();
    TimeZone tz = TimeZone.getDefault();
    final long baseTime = System.currentTimeMillis();

    final long DAY_DURATION = 5 * 24 * 60 * 60 * 1000;
    assertNotNull(getRelativeDateTimeString(en_US, tz, baseTime - DAY_DURATION, baseTime,
                                            MINUTE_IN_MILLIS, DAY_IN_MILLIS,
                                            FORMAT_NUMERIC_DATE));
  }

  public void test_getRelativeDateTimeString() throws Exception {
    Locale en_US = new Locale("en", "US");
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = Calendar.getInstance(tz, en_US);
    // Feb 5, 2015 at 10:50 PST
    cal.set(2015, Calendar.FEBRUARY, 5, 10, 50, 0);
    final long base = cal.getTimeInMillis();

    assertEquals("5 seconds ago, 10:49 AM",
                 getRelativeDateTimeString(en_US, tz, base - 5 * SECOND_IN_MILLIS, base, 0,
                                           MINUTE_IN_MILLIS, 0));
    assertEquals("5 min. ago, 10:45 AM",
                 getRelativeDateTimeString(en_US, tz, base - 5 * MINUTE_IN_MILLIS, base, 0,
                                           HOUR_IN_MILLIS, FORMAT_ABBREV_RELATIVE));
    assertEquals("0 hr. ago, 10:45 AM",
                 getRelativeDateTimeString(en_US, tz, base - 5 * MINUTE_IN_MILLIS, base,
                                           HOUR_IN_MILLIS, DAY_IN_MILLIS, FORMAT_ABBREV_RELATIVE));
    assertEquals("5 hours ago, 5:50 AM",
                 getRelativeDateTimeString(en_US, tz, base - 5 * HOUR_IN_MILLIS, base,
                                           HOUR_IN_MILLIS, DAY_IN_MILLIS, 0));
    assertEquals("Yesterday, 7:50 PM",
                 getRelativeDateTimeString(en_US, tz, base - 15 * HOUR_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, FORMAT_ABBREV_RELATIVE));
    assertEquals("5 days ago, 10:50 AM",
                 getRelativeDateTimeString(en_US, tz, base - 5 * DAY_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, 0));
    assertEquals("Jan 29, 10:50 AM",
                 getRelativeDateTimeString(en_US, tz, base - 7 * DAY_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, 0));
    assertEquals("11/27/2014, 10:50 AM",
                 getRelativeDateTimeString(en_US, tz, base - 10 * WEEK_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, 0));
    assertEquals("11/27/2014, 10:50 AM",
                 getRelativeDateTimeString(en_US, tz, base - 10 * WEEK_IN_MILLIS, base, 0,
                                           YEAR_IN_MILLIS, 0));

    // User-supplied flags should be ignored when formatting the date clause.
    final int FORMAT_SHOW_WEEKDAY = 0x00002;
    assertEquals("11/27/2014, 10:50 AM",
                 getRelativeDateTimeString(en_US, tz, base - 10 * WEEK_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS,
                                           FORMAT_ABBREV_ALL | FORMAT_SHOW_WEEKDAY));
  }

  public void test_getRelativeDateTimeStringDST() throws Exception {
    Locale en_US = new Locale("en", "US");
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = Calendar.getInstance(tz, en_US);

    // DST starts on Mar 9, 2014 at 2:00 AM.
    // So 5 hours before 3:15 AM should be formatted as 'Yesterday, 9:15 PM'.
    cal.set(2014, Calendar.MARCH, 9, 3, 15, 0);
    long base = cal.getTimeInMillis();
    assertEquals("Yesterday, 9:15 PM",
                 getRelativeDateTimeString(en_US, tz, base - 5 * HOUR_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, 0));

    // 1 hour after 2:00 AM should be formatted as 'In 1 hour, 4:00 AM'.
    cal.set(2014, Calendar.MARCH, 9, 2, 0, 0);
    base = cal.getTimeInMillis();
    assertEquals("In 1 hour, 4:00 AM",
                 getRelativeDateTimeString(en_US, tz, base + 1 * HOUR_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, 0));

    // DST ends on Nov 2, 2014 at 2:00 AM. Clocks are turned backward 1 hour to
    // 1:00 AM. 8 hours before 5:20 AM should be 'Yesterday, 10:20 PM'.
    cal.set(2014, Calendar.NOVEMBER, 2, 5, 20, 0);
    base = cal.getTimeInMillis();
    assertEquals("Yesterday, 10:20 PM",
                 getRelativeDateTimeString(en_US, tz, base - 8 * HOUR_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, 0));

    cal.set(2014, Calendar.NOVEMBER, 2, 0, 45, 0);
    base = cal.getTimeInMillis();
    // 45 minutes after 0:45 AM should be 'In 45 minutes, 1:30 AM'.
    assertEquals("In 45 minutes, 1:30 AM",
                 getRelativeDateTimeString(en_US, tz, base + 45 * MINUTE_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, 0));
    // 45 minutes later, it should be 'In 45 minutes, 1:15 AM'.
    assertEquals("In 45 minutes, 1:15 AM",
                 getRelativeDateTimeString(en_US, tz, base + 90 * MINUTE_IN_MILLIS,
                                           base + 45 * MINUTE_IN_MILLIS, 0, WEEK_IN_MILLIS, 0));
    // Another 45 minutes later, it should be 'In 45 minutes, 2:00 AM'.
    assertEquals("In 45 minutes, 2:00 AM",
                 getRelativeDateTimeString(en_US, tz, base + 135 * MINUTE_IN_MILLIS,
                                           base + 90 * MINUTE_IN_MILLIS, 0, WEEK_IN_MILLIS, 0));
  }


  public void test_getRelativeDateTimeStringItalian() throws Exception {
    Locale it_IT = new Locale("it", "IT");
    TimeZone tz = TimeZone.getTimeZone("Europe/Rome");
    Calendar cal = Calendar.getInstance(tz, it_IT);
    // 05 febbraio 2015 20:15
    cal.set(2015, Calendar.FEBRUARY, 5, 20, 15, 0);
    final long base = cal.getTimeInMillis();

    assertEquals("5 secondi fa, 20:14",
                 getRelativeDateTimeString(it_IT, tz, base - 5 * SECOND_IN_MILLIS, base, 0,
                                           MINUTE_IN_MILLIS, 0));
    assertEquals("5 min. fa, 20:10",
                 getRelativeDateTimeString(it_IT, tz, base - 5 * MINUTE_IN_MILLIS, base, 0,
                                           HOUR_IN_MILLIS, FORMAT_ABBREV_RELATIVE));
    assertEquals("0 h. fa, 20:10",
                 getRelativeDateTimeString(it_IT, tz, base - 5 * MINUTE_IN_MILLIS, base,
                                           HOUR_IN_MILLIS, DAY_IN_MILLIS, FORMAT_ABBREV_RELATIVE));
    assertEquals("Ieri, 22:15",
                 getRelativeDateTimeString(it_IT, tz, base - 22 * HOUR_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, FORMAT_ABBREV_RELATIVE));
    assertEquals("5 giorni fa, 20:15",
                 getRelativeDateTimeString(it_IT, tz, base - 5 * DAY_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, 0));
    assertEquals("27/11/2014, 20:15",
                 getRelativeDateTimeString(it_IT, tz, base - 10 * WEEK_IN_MILLIS, base, 0,
                                           WEEK_IN_MILLIS, 0));
  }

  // http://b/5252772: detect the actual date difference
  public void test5252772() throws Exception {
    Locale en_US = new Locale("en", "US");
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");

    // Now is Sep 2, 2011, 10:23 AM PDT.
    Calendar nowCalendar = Calendar.getInstance(tz, en_US);
    nowCalendar.set(2011, Calendar.SEPTEMBER, 2, 10, 23, 0);
    final long now = nowCalendar.getTimeInMillis();

    // Sep 1, 2011, 10:24 AM
    Calendar yesterdayCalendar1 = Calendar.getInstance(tz, en_US);
    yesterdayCalendar1.set(2011, Calendar.SEPTEMBER, 1, 10, 24, 0);
    long yesterday1 = yesterdayCalendar1.getTimeInMillis();
    assertEquals("Yesterday, 10:24 AM",
                 getRelativeDateTimeString(en_US, tz, yesterday1, now, MINUTE_IN_MILLIS,
                                           WEEK_IN_MILLIS, 0));

    // Sep 1, 2011, 10:22 AM
    Calendar yesterdayCalendar2 = Calendar.getInstance(tz, en_US);
    yesterdayCalendar2.set(2011, Calendar.SEPTEMBER, 1, 10, 22, 0);
    long yesterday2 = yesterdayCalendar2.getTimeInMillis();
    assertEquals("Yesterday, 10:22 AM",
                 getRelativeDateTimeString(en_US, tz, yesterday2, now, MINUTE_IN_MILLIS,
                                           WEEK_IN_MILLIS, 0));

    // Aug 31, 2011, 10:24 AM
    Calendar twoDaysAgoCalendar1 = Calendar.getInstance(tz, en_US);
    twoDaysAgoCalendar1.set(2011, Calendar.AUGUST, 31, 10, 24, 0);
    long twoDaysAgo1 = twoDaysAgoCalendar1.getTimeInMillis();
    assertEquals("2 days ago, 10:24 AM",
                 getRelativeDateTimeString(en_US, tz, twoDaysAgo1, now, MINUTE_IN_MILLIS,
                                           WEEK_IN_MILLIS, 0));

    // Aug 31, 2011, 10:22 AM
    Calendar twoDaysAgoCalendar2 = Calendar.getInstance(tz, en_US);
    twoDaysAgoCalendar2.set(2011, Calendar.AUGUST, 31, 10, 22, 0);
    long twoDaysAgo2 = twoDaysAgoCalendar2.getTimeInMillis();
    assertEquals("2 days ago, 10:22 AM",
                 getRelativeDateTimeString(en_US, tz, twoDaysAgo2, now, MINUTE_IN_MILLIS,
                                           WEEK_IN_MILLIS, 0));

    // Sep 3, 2011, 10:22 AM
    Calendar tomorrowCalendar1 = Calendar.getInstance(tz, en_US);
    tomorrowCalendar1.set(2011, Calendar.SEPTEMBER, 3, 10, 22, 0);
    long tomorrow1 = tomorrowCalendar1.getTimeInMillis();
    assertEquals("Tomorrow, 10:22 AM",
                 getRelativeDateTimeString(en_US, tz, tomorrow1, now, MINUTE_IN_MILLIS,
                                           WEEK_IN_MILLIS, 0));

    // Sep 3, 2011, 10:24 AM
    Calendar tomorrowCalendar2 = Calendar.getInstance(tz, en_US);
    tomorrowCalendar2.set(2011, Calendar.SEPTEMBER, 3, 10, 24, 0);
    long tomorrow2 = tomorrowCalendar2.getTimeInMillis();
    assertEquals("Tomorrow, 10:24 AM",
                 getRelativeDateTimeString(en_US, tz, tomorrow2, now, MINUTE_IN_MILLIS,
                                           WEEK_IN_MILLIS, 0));

    // Sep 4, 2011, 10:22 AM
    Calendar twoDaysLaterCalendar1 = Calendar.getInstance(tz, en_US);
    twoDaysLaterCalendar1.set(2011, Calendar.SEPTEMBER, 4, 10, 22, 0);
    long twoDaysLater1 = twoDaysLaterCalendar1.getTimeInMillis();
    assertEquals("In 2 days, 10:22 AM",
                 getRelativeDateTimeString(en_US, tz, twoDaysLater1, now, MINUTE_IN_MILLIS,
                                           WEEK_IN_MILLIS, 0));

    // Sep 4, 2011, 10:24 AM
    Calendar twoDaysLaterCalendar2 = Calendar.getInstance(tz, en_US);
    twoDaysLaterCalendar2.set(2011, Calendar.SEPTEMBER, 4, 10, 24, 0);
    long twoDaysLater2 = twoDaysLaterCalendar2.getTimeInMillis();
    assertEquals("In 2 days, 10:24 AM",
                 getRelativeDateTimeString(en_US, tz, twoDaysLater2, now, MINUTE_IN_MILLIS,
                                           WEEK_IN_MILLIS, 0));
  }

  // b/19822016: show / hide the year based on the dates in the arguments.
  public void test_bug19822016() throws Exception {
    Locale en_US = new Locale("en", "US");
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = Calendar.getInstance(tz, en_US);
    // Feb 5, 2012 at 10:50 PST
    cal.set(2012, Calendar.FEBRUARY, 5, 10, 50, 0);
    long base = cal.getTimeInMillis();

    assertEquals("Feb 5, 5:50 AM", getRelativeDateTimeString(en_US, tz,
        base - 5 * HOUR_IN_MILLIS, base, 0, MINUTE_IN_MILLIS, 0));
    assertEquals("Jan 29, 10:50 AM", getRelativeDateTimeString(en_US, tz,
        base - 7 * DAY_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0));
    assertEquals("11/27/2011, 10:50 AM", getRelativeDateTimeString(en_US, tz,
        base - 10 * WEEK_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0));

    assertEquals("January 6", getRelativeTimeSpanString(en_US, tz,
        base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, 0));
    assertEquals("January 6", getRelativeTimeSpanString(en_US, tz,
        base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_NO_YEAR));
    assertEquals("January 6, 2012", getRelativeTimeSpanString(en_US, tz,
        base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_SHOW_YEAR));
    assertEquals("December 7, 2011", getRelativeTimeSpanString(en_US, tz,
        base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, 0));
    assertEquals("December 7, 2011", getRelativeTimeSpanString(en_US, tz,
        base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_SHOW_YEAR));
    assertEquals("December 7", getRelativeTimeSpanString(en_US, tz,
        base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_NO_YEAR));

    // Feb 5, 2018 at 10:50 PST
    cal.set(2018, Calendar.FEBRUARY, 5, 10, 50, 0);
    base = cal.getTimeInMillis();
    assertEquals("Feb 5, 5:50 AM", getRelativeDateTimeString(en_US, tz,
        base - 5 * HOUR_IN_MILLIS, base, 0, MINUTE_IN_MILLIS, 0));
    assertEquals("Jan 29, 10:50 AM", getRelativeDateTimeString(en_US, tz,
        base - 7 * DAY_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0));
    assertEquals("11/27/2017, 10:50 AM", getRelativeDateTimeString(en_US, tz,
        base - 10 * WEEK_IN_MILLIS, base, 0, WEEK_IN_MILLIS, 0));

    assertEquals("January 6", getRelativeTimeSpanString(en_US, tz,
        base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, 0));
    assertEquals("January 6", getRelativeTimeSpanString(en_US, tz,
        base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_NO_YEAR));
    assertEquals("January 6, 2018", getRelativeTimeSpanString(en_US, tz,
        base - 30 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_SHOW_YEAR));
    assertEquals("December 7, 2017", getRelativeTimeSpanString(en_US, tz,
        base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, 0));
    assertEquals("December 7, 2017", getRelativeTimeSpanString(en_US, tz,
        base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_SHOW_YEAR));
    assertEquals("December 7", getRelativeTimeSpanString(en_US, tz,
        base - 60 * DAY_IN_MILLIS, base, DAY_IN_MILLIS, FORMAT_NO_YEAR));
  }
}