aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/yaml/snakeyaml/types/TimestampTagTest.java
blob: 5e889d16ae9cc0d9c8353c545b7e627f512564e3 (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
/**
 * Copyright (c) 2008, http://www.snakeyaml.org
 *
 * 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 org.yaml.snakeyaml.types;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

/**
 * @see <a href="http://yaml.org/type/timestamp.html"></a>
 */
public class TimestampTagTest extends AbstractTest {

    public void testTimestamp() {
        assertEquals("2001-12-15 at 2:59:43 (100)",
                getText("canonical: 2001-12-15T02:59:43.1Z", "canonical"));
        // zero miliseconds
        assertEquals("2001-12-15 at 2:59:43 (0)",
                getText("canonical: 2001-12-15T02:59:43.000Z", "canonical"));
        assertEquals("2001-12-15 at 2:59:43 (100)",
                getText("valid iso8601:    2001-12-14t21:59:43.10-05:00", "valid iso8601"));
        // half hour time zone
        assertEquals("2001-12-14 at 22:29:43 (100)",
                getText("valid iso8601:    2001-12-14t21:59:43.10-0:30", "valid iso8601"));
        // + time zone
        assertEquals("2001-12-14 at 19:59:43 (100)",
                getText("valid iso8601:    2001-12-14t21:59:43.10+2:00", "valid iso8601"));
        assertEquals("2001-12-15 at 2:59:43 (100)",
                getText("space separated:  2001-12-14 21:59:43.10 -5", "space separated"));
        assertEquals("2001-12-15 at 2:59:43 (100)",
                getText("no time zone (Z): 2001-12-15 2:59:43.10", "no time zone (Z)"));
        assertEquals("2002-12-14 at 0:0:0 (0)",
                getText("date (00:00:00Z): 2002-12-14", "date (00:00:00Z)"));
        assertEquals("2010-5-16 at 3:6:11 (3)",
                getText("milliseconds: 2010-05-16 03:06:11.003", "milliseconds"));
        assertEquals("2010-5-16 at 3:6:11 (7)",
                getText("milliseconds: 2010-05-16 03:06:11.0068", "milliseconds"));
        assertEquals("2010-5-16 at 3:6:11 (0)",
                getText("milliseconds: 2010-05-16 03:06:11.0000", "milliseconds"));
        assertEquals("2010-5-16 at 3:6:11 (0)",
                getText("milliseconds: 2010-05-16 03:06:11.0004", "milliseconds"));
        assertEquals("2010-5-16 at 3:6:11 (25)",
                getText("milliseconds: 2010-05-16 03:06:11.0250", "milliseconds"));
    }

    public void testTimestampShorthand() {
        assertTrue(getMapValue("canonical: !!timestamp 2001-12-15T02:59:43.1Z", "canonical") instanceof Date);
    }

    public void testTimestampTag() {
        assertTrue(getMapValue("canonical: !<tag:yaml.org,2002:timestamp> 2001-12-15T02:59:43.1Z",
                "canonical") instanceof Date);
    }

    public void testTimestampOut() {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Moscow"));
        cal.clear();
        cal.set(2008, 8, 23, 14, 35, 4);
        Date date = cal.getTime();
        String output = dump(date);
        assertEquals("2008-09-23T10:35:04Z\n", output);
    }

    public void testTimestampOutMap() {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Moscow"));
        cal.clear();
        cal.set(2008, 8, 23, 14, 35, 4);
        Date date = cal.getTime();
        Map<String, Date> map = new HashMap<String, Date>();
        map.put("canonical", date);
        String output = dump(map);
        assertEquals("{canonical: !!timestamp '2008-09-23T10:35:04Z'}\n", output);
    }

    private String getText(String yaml, String key) {
        Date date = (Date) getMapValue(yaml, key);
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        cal.setTime(date);
        int years = cal.get(Calendar.YEAR);
        int months = cal.get(Calendar.MONTH) + 1; // 0..12
        int days = cal.get(Calendar.DAY_OF_MONTH); // 1..31
        int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..24
        int minutes = cal.get(Calendar.MINUTE); // 0..59
        int seconds = cal.get(Calendar.SECOND); // 0..59
        int millis = cal.get(Calendar.MILLISECOND);
        String result = String.valueOf(years) + "-" + String.valueOf(months) + "-"
                + String.valueOf(days) + " at " + String.valueOf(hour24) + ":"
                + String.valueOf(minutes) + ":" + String.valueOf(seconds) + " ("
                + String.valueOf(millis) + ")";
        return result;
    }

    public void testTimestampReadWrite() {
        Date date = (Date) getMapValue("Time: 2001-11-23 15:01:42 -5", "Time");
        Map<String, Date> map = new HashMap<String, Date>();
        map.put("canonical", date);
        String output = dump(map);
        assertEquals("{canonical: !!timestamp '2001-11-23T20:01:42Z'}\n", output);
    }

    public void testSqlDate() {
        java.sql.Date date = new java.sql.Date(1000000000000L);
        Map<String, java.sql.Date> map = new HashMap<String, java.sql.Date>();
        map.put("canonical", date);
        String output = dump(map);
        assertEquals("{canonical: !!timestamp '2001-09-09T01:46:40Z'}\n", output);
    }
}