01046586dc5d615eb287662e302f15fbeba283a1
[idea/community.git] / platform / util / testSrc / com / intellij / util / text / DateFormatUtilTest.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
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 package com.intellij.util.text;
17
18 import com.intellij.openapi.application.PathManager;
19 import com.intellij.openapi.util.Clock;
20 import com.intellij.openapi.util.SystemInfo;
21 import com.intellij.openapi.util.text.StringUtil;
22 import com.intellij.util.containers.ContainerUtil;
23 import org.junit.Test;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.text.DateFormat;
30 import java.text.ParseException;
31 import java.text.SimpleDateFormat;
32 import java.util.*;
33
34 import static org.junit.Assert.assertEquals;
35
36 public class DateFormatUtilTest {
37   @SuppressWarnings("SpellCheckingInspection") private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy hh.mm.ss");
38
39   @Test
40   public void testBasics() throws ParseException {
41     Clock.setTime(2004, Calendar.DECEMBER, 10, 17, 10);
42
43     doTestPrettyDate("Today", "10.12.2004 17.00.00");
44     doTestPrettyDate("Today", "10.12.2004 00.00.00");
45     doTestPrettyDate("Yesterday", "09.12.2004 23.59.59");
46     doTestPrettyDate(DateFormatUtil.formatDate(DATE_FORMAT.parse("08.12.2004 23.59.59")), "08.12.2004 23.59.59");
47     doTestPrettyDate(DateFormatUtil.formatDate(DATE_FORMAT.parse("10.12.2003 17.00.00")), "10.12.2003 17.00.00");
48   }
49
50   @Test
51   public void testTime() throws Exception {
52     Clock.setTime(1980, Calendar.DECEMBER, 10, 17, 10, 15);
53
54     if (SystemInfo.isMac) {
55       assertEquals("17:10", DateFormatUtil.formatTime(Clock.getTime()));
56       assertEquals("17:10:15", DateFormatUtil.formatTimeWithSeconds(Clock.getTime()));
57     }
58     else if (SystemInfo.isUnix) {
59       assertEquals("5:10:15 PM", printTimeForLocale("en_US.UTF-8", Clock.getTime()));
60       assertEquals("17:10:15", printTimeForLocale("de_DE.UTF-8", Clock.getTime()));
61     }
62     else if (SystemInfo.isWinVistaOrNewer) {
63       long time = Clock.getTime();
64       assertEquals(printWindowsTime(time), DateFormatUtil.formatTimeWithSeconds(time));
65     }
66     else {
67       assertEquals(DateFormat.getTimeInstance(DateFormat.SHORT).format(Clock.getTime()),
68                    DateFormatUtil.formatTime(Clock.getTime()));
69       assertEquals(DateFormat.getTimeInstance(DateFormat.MEDIUM).format(Clock.getTime()),
70                    DateFormatUtil.formatTimeWithSeconds(new Date(Clock.getTime())));
71     }
72   }
73
74   @Test
75   public void testPrettyDateTime() throws ParseException {
76     Clock.setTime(2004, Calendar.DECEMBER, 10, 17, 0);
77     doTestDateTime("Moments ago", "10.12.2004 16.59.31");
78     doTestDateTime("A minute ago", "10.12.2004 16.59.29");
79     doTestDateTime("5 minutes ago", "10.12.2004 16.55.00");
80     doTestDateTime("1 hour ago", "10.12.2004 16.00.00");
81     doTestDateTime("Today " + DateFormatUtil.formatTime(DATE_FORMAT.parse("10.12.2004 15.55.00")), "10.12.2004 15.55.00");
82     doTestDateTime("Yesterday " + DateFormatUtil.formatTime(DATE_FORMAT.parse("09.12.2004 15.00.00")), "09.12.2004 15.00.00");
83     doTestDateTime("Today " + DateFormatUtil.formatTime(DATE_FORMAT.parse("10.12.2004 19.00.00")), "10.12.2004 19.00.00");
84
85     Clock.setTime(2004, Calendar.JANUARY, 1, 15, 53);
86     doTestDateTime(DateFormatUtil.formatDateTime(DATE_FORMAT.parse("01.01.2003 15.53.00")), "01.01.2003 15.53.00");
87     doTestDateTime("Yesterday " + DateFormatUtil.formatTime(DATE_FORMAT.parse("31.12.2003 15.00.00")), "31.12.2003 15.00.00");
88   }
89
90   @Test
91   public void testAboutDialogDataFormatter() {
92     assertEquals("December 12, 2012", DateFormatUtil.formatAboutDialogDate(date(2012, 12, 12, 15, 35, 12)));
93     assertEquals("January 1, 1999", DateFormatUtil.formatAboutDialogDate(date(1999, 1, 1, 0, 0, 0)));
94   }
95
96   @Test
97   public void testFormatFrequency() {
98     assertEquals("Once in 2 minutes", DateFormatUtil.formatFrequency(2 * 60 * 1000));
99     assertEquals("Once in a few moments", DateFormatUtil.formatFrequency(1000));
100   }
101
102   private static void doTestPrettyDate(String expected, String date) throws ParseException {
103     assertEquals(expected, DateFormatUtil.formatPrettyDate(DATE_FORMAT.parse(date)));
104   }
105
106   private static void doTestDateTime(String expected, String date) throws ParseException {
107     assertEquals(expected, DateFormatUtil.formatPrettyDateTime(DATE_FORMAT.parse(date)));
108   }
109
110   private static Date date(final int year, final int month, final int day, final int hour, final int minute, final int second) {
111     return new GregorianCalendar(year, month - 1, day, hour, minute, second).getTime();
112   }
113
114   private static String printTimeForLocale(String locale, long time) throws IOException {
115     List<String> classpath = ContainerUtil.newArrayList();
116     classpath.addAll(PathManager.getUtilClassPath());
117     classpath.add(PathManager.getJarPathForClass(PrintTime.class));
118     ProcessBuilder builder = new ProcessBuilder()
119       .command(System.getProperty("java.home") + "/bin/java",
120                "-classpath",
121                StringUtil.join(classpath, File.pathSeparator),
122                PrintTime.class.getName(),
123                String.valueOf(time));
124     builder.environment().put("LC_TIME", locale);
125     return execAndGetOutput(builder);
126   }
127
128   private static String printWindowsTime(long time) throws IOException {
129     String script = DateFormatUtil.class.getResource("PrintTime.js").getPath();
130     if (StringUtil.startsWithChar(script, '/')) script = script.substring(1);
131     ProcessBuilder builder = new ProcessBuilder().command("cscript", "//Nologo", script, String.valueOf(time));
132     return execAndGetOutput(builder);
133   }
134
135   private static String execAndGetOutput(ProcessBuilder builder) throws IOException {
136     Process process = builder.redirectErrorStream(true).start();
137     BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
138     try {
139       return reader.readLine();
140     }
141     finally {
142       reader.close();
143     }
144   }
145 }