js/src/tests/ecma_3/Date/15.9.5.4.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_3/Date/15.9.5.4.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/**
    1.10 +   File Name:    15.9.5.4.js
    1.11 +   ECMA Section: 15.9.5.4 Date.prototype.toTimeString()
    1.12 +   Description:
    1.13 +   This function returns a string value. The contents of the string are
    1.14 +   implementation dependent, but are intended to represent the "time"
    1.15 +   portion of the Date in the current time zone in a convenient,
    1.16 +   human-readable form.   We test the content of the string by checking
    1.17 +   that d.toDateString()  +  d.toTimeString()  ==  d.toString()
    1.18 +
    1.19 +   Author:  pschwartau@netscape.com                            
    1.20 +   Date:    14 november 2000
    1.21 +   Revised: 07 january 2002  because of a change in JS Date format:
    1.22 +
    1.23 +   See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey)
    1.24 +   See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino)
    1.25 +*/
    1.26 +//-----------------------------------------------------------------------------
    1.27 +var SECTION = "15.9.5.4";
    1.28 +var VERSION = "ECMA_3"; 
    1.29 +var TITLE   = "Date.prototype.toTimeString()";
    1.30 +  
    1.31 +var status = '';
    1.32 +var actual = ''; 
    1.33 +var expect = '';
    1.34 +var givenDate;
    1.35 +var year = '';
    1.36 +var regexp = '';
    1.37 +var reducedDateString = '';
    1.38 +var hopeThisIsTimeString = '';
    1.39 +var cnEmptyString = '';
    1.40 +var cnERR ='OOPS! FATAL ERROR: no regexp match in extractTimeString()';
    1.41 +
    1.42 +startTest();
    1.43 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.44 +
    1.45 +// first, a couple of generic tests -
    1.46 +
    1.47 +status = "typeof (now.toTimeString())"; 
    1.48 +actual =   typeof (now.toTimeString());
    1.49 +expect = "string";
    1.50 +addTestCase();
    1.51 +
    1.52 +status = "Date.prototype.toTimeString.length";  
    1.53 +actual =  Date.prototype.toTimeString.length;
    1.54 +expect =  0;  
    1.55 +addTestCase();
    1.56 +
    1.57 +// 1970
    1.58 +addDateTestCase(0);
    1.59 +addDateTestCase(TZ_ADJUST);
    1.60 +  
    1.61 +
    1.62 +// 1900
    1.63 +addDateTestCase(TIME_1900);
    1.64 +addDateTestCase(TIME_1900 - TZ_ADJUST);
    1.65 +
    1.66 +
    1.67 +// 2000
    1.68 +addDateTestCase(TIME_2000);
    1.69 +addDateTestCase(TIME_2000 - TZ_ADJUST);
    1.70 +
    1.71 +   
    1.72 +// 29 Feb 2000
    1.73 +addDateTestCase(UTC_29_FEB_2000);
    1.74 +addDateTestCase(UTC_29_FEB_2000 - 1000);
    1.75 +addDateTestCase(UTC_29_FEB_2000 - TZ_ADJUST);
    1.76 +
    1.77 +
    1.78 +// Now
    1.79 +addDateTestCase( TIME_NOW);
    1.80 +addDateTestCase( TIME_NOW - TZ_ADJUST);
    1.81 +
    1.82 +
    1.83 +// 2005
    1.84 +addDateTestCase(UTC_1_JAN_2005);
    1.85 +addDateTestCase(UTC_1_JAN_2005 - 1000);
    1.86 +addDateTestCase(UTC_1_JAN_2005 - TZ_ADJUST);
    1.87 +
    1.88 +//-----------------------------------------------------------------------------------------------------
    1.89 +test();
    1.90 +//-----------------------------------------------------------------------------------------------------
    1.91 +
    1.92 +function addTestCase()
    1.93 +{
    1.94 +  new TestCase(
    1.95 +    SECTION,
    1.96 +    status,
    1.97 +    expect,
    1.98 +    actual);
    1.99 +}
   1.100 +
   1.101 +function addDateTestCase(date_given_in_milliseconds)
   1.102 +{
   1.103 +  givenDate = new Date(date_given_in_milliseconds);
   1.104 +  
   1.105 +  status = '('  +  givenDate  +  ').toTimeString()';  
   1.106 +  actual = givenDate.toTimeString();
   1.107 +  expect = extractTimeString(givenDate);
   1.108 +  addTestCase();
   1.109 +}
   1.110 +
   1.111 +
   1.112 +/*
   1.113 + * As of 2002-01-07, the format for JavaScript dates changed.
   1.114 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey)
   1.115 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino)
   1.116 + *
   1.117 + * WAS: Mon Jan 07 13:40:34 GMT-0800 (Pacific Standard Time) 2002
   1.118 + * NOW: Mon Jan 07 2002 13:40:34 GMT-0800 (Pacific Standard Time)
   1.119 + *
   1.120 + * Thus, use a regexp of the form /date.toDateString()(.*)$/
   1.121 + * to capture the TimeString into the first backreference -
   1.122 + */
   1.123 +function extractTimeString(date)
   1.124 +{
   1.125 +  regexp = new RegExp(date.toDateString() + '(.*)' + '$');
   1.126 +
   1.127 +  try
   1.128 +  {
   1.129 +    hopeThisIsTimeString = date.toString().match(regexp)[1];
   1.130 +  }
   1.131 +  catch(e)
   1.132 +  {
   1.133 +    return cnERR;
   1.134 +  }
   1.135 +
   1.136 +  // trim any leading or trailing spaces -
   1.137 +  return trimL(trimR(hopeThisIsTimeString));
   1.138 +}
   1.139 +
   1.140 +
   1.141 +function trimL(s)
   1.142 +{
   1.143 +  if (!s) {return cnEmptyString;};
   1.144 +  for (var i = 0; i!=s.length; i++) {if (s[i] != ' ') {break;}}
   1.145 +  return s.substring(i);
   1.146 +}
   1.147 +
   1.148 +
   1.149 +function trimR(s)
   1.150 +{
   1.151 +  if (!s) {return cnEmptyString;};
   1.152 +  for (var i = (s.length - 1); i!=-1; i--) {if (s[i] != ' ') {break;}}
   1.153 +  return s.substring(0, i+1);
   1.154 +}

mercurial