View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
    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.yaml.snakeyaml.types;
    18
    19  import java.io.IOException;
    20  import java.util.Calendar;
    21  import java.util.Date;
    22  import java.util.HashMap;
    23  import java.util.Map;
    24  import java.util.TimeZone;
    25
    26  /**
    27   * @see http://yaml.org/type/timestamp.html
    28   */
    29  public class TimestampTagTest extends AbstractTest {
    30
    31      public void testTimestamp() throws IOException {
    32          assertEquals("2001-12-15 at 2:59:43 (100)",
    33                  getText("canonical: 2001-12-15T02:59:43.1Z", "canonical"));
    34          // zero miliseconds
    35          assertEquals("2001-12-15 at 2:59:43 (0)",
    36                  getText("canonical: 2001-12-15T02:59:43.000Z", "canonical"));
    37          assertEquals("2001-12-15 at 2:59:43 (100)",
    38                  getText("valid iso8601:    2001-12-14t21:59:43.10-05:00", "valid iso8601"));
    39          // half hour time zone
    40          assertEquals("2001-12-14 at 22:29:43 (100)",
    41                  getText("valid iso8601:    2001-12-14t21:59:43.10-0:30", "valid iso8601"));
    42          // + time zone
    43          assertEquals("2001-12-14 at 19:59:43 (100)",
    44                  getText("valid iso8601:    2001-12-14t21:59:43.10+2:00", "valid iso8601"));
    45          assertEquals("2001-12-15 at 2:59:43 (100)",
    46                  getText("space separated:  2001-12-14 21:59:43.10 -5", "space separated"));
    47          assertEquals("2001-12-15 at 2:59:43 (100)",
    48                  getText("no time zone (Z): 2001-12-15 2:59:43.10", "no time zone (Z)"));
    49          assertEquals("2002-12-14 at 0:0:0 (0)",
    50                  getText("date (00:00:00Z): 2002-12-14", "date (00:00:00Z)"));
    51          assertEquals("2010-5-16 at 3:6:11 (3)",
    52                  getText("milliseconds: 2010-05-16 03:06:11.003", "milliseconds"));
    53          assertEquals("2010-5-16 at 3:6:11 (7)",
    54                  getText("milliseconds: 2010-05-16 03:06:11.0068", "milliseconds"));
    55          assertEquals("2010-5-16 at 3:6:11 (0)",
    56                  getText("milliseconds: 2010-05-16 03:06:11.0000", "milliseconds"));
    57          assertEquals("2010-5-16 at 3:6:11 (0)",
    58                  getText("milliseconds: 2010-05-16 03:06:11.0004", "milliseconds"));
    59          assertEquals("2010-5-16 at 3:6:11 (25)",
    60                  getText("milliseconds: 2010-05-16 03:06:11.0250", "milliseconds"));
    61      }
    62
    63      public void testTimestampShorthand() throws IOException {
    64          assertTrue(getMapValue("canonical: !!timestamp 2001-12-15T02:59:43.1Z", "canonical") instanceof Date);
    65      }
    66
    67      public void testTimestampTag() throws IOException {
    68          assertTrue(getMapValue("canonical: !<tag:yaml.org,2002:timestamp> 2001-12-15T02:59:43.1Z",
    69                  "canonical") instanceof Date);
    70      }
    71
    72      public void testTimestampOut() throws IOException {
    73          Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Moscow"));
    74          cal.clear();
    75          cal.set(2008, 8, 23, 14, 35, 4);
    76          Date date = cal.getTime();
    77          String output = dump(date);
    78          assertEquals("2008-09-23T10:35:04Z\n", output);
    79      }
    80
    81      public void testTimestampOutMap() throws IOException {
    82          Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Moscow"));
    83          cal.clear();
    84          cal.set(2008, 8, 23, 14, 35, 4);
    85          Date date = cal.getTime();
    86          Map<String, Date> map = new HashMap<String, Date>();
    87          map.put("canonical", date);
    88          String output = dump(map);
    89          assertEquals("{canonical: !!timestamp '2008-09-23T10:35:04Z'}\n", output);
    90      }
    91
    92      private String getText(String yaml, String key) {
    93          Date date = (Date) getMapValue(yaml, key);
    94          Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    95          cal.setTime(date);
    96          int years = cal.get(Calendar.YEAR);
    97          int months = cal.get(Calendar.MONTH) + 1; // 0..12
    98          int days = cal.get(Calendar.DAY_OF_MONTH); // 1..31
    99          int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..24
    100         int minutes = cal.get(Calendar.MINUTE); // 0..59
    101         int seconds = cal.get(Calendar.SECOND); // 0..59
    102         int millis = cal.get(Calendar.MILLISECOND);
    103         String result = String.valueOf(years) + "-" + String.valueOf(months) + "-"
    104                 + String.valueOf(days) + " at " + String.valueOf(hour24) + ":"
    105                 + String.valueOf(minutes) + ":" + String.valueOf(seconds) + " ("
    106                 + String.valueOf(millis) + ")";
    107         return result;
    108     }
    109
    110     public void testTimestampReadWrite() throws IOException {
    111         Date date = (Date) getMapValue("Time: 2001-11-23 15:01:42 -5", "Time");
    112         Map<String, Date> map = new HashMap<String, Date>();
    113         map.put("canonical", date);
    114         String output = dump(map);
    115         assertEquals("{canonical: !!timestamp '2001-11-23T20:01:42Z'}\n", output);
    116     }
    117
    118     public void testSqlDate() throws IOException {
    119         java.sql.Date date = new java.sql.Date(1000000000000L);
    120         Map<String, java.sql.Date> map = new HashMap<String, java.sql.Date>();
    121         map.put("canonical", date);
    122         String output = dump(map);
    123         assertEquals("{canonical: !!timestamp '2001-09-09T01:46:40Z'}\n", output);
    124     }
    125 }