github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/date_test.go (about)

     1  package goja
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestDateUTC(t *testing.T) {
     9  	const SCRIPT = `
    10  	assert.sameValue(Date.UTC(1970, 0), 0, '1970, 0');
    11  	assert.sameValue(Date.UTC(2016, 0), 1451606400000, '2016, 0');
    12  	assert.sameValue(Date.UTC(2016, 6), 1467331200000, '2016, 6');
    13  
    14  	assert.sameValue(Date.UTC(2016, 6, 1), 1467331200000, '2016, 6, 1');
    15  	assert.sameValue(Date.UTC(2016, 6, 5), 1467676800000, '2016, 6, 5');
    16  
    17  	assert.sameValue(Date.UTC(2016, 6, 5, 0), 1467676800000, '2016, 6, 5, 0');
    18  	assert.sameValue(Date.UTC(2016, 6, 5, 15), 1467730800000, '2016, 6, 5, 15');
    19  
    20  	assert.sameValue(
    21    		Date.UTC(2016, 6, 5, 15, 0), 1467730800000, '2016, 6, 5, 15, 0'
    22  	);
    23  	assert.sameValue(
    24    		Date.UTC(2016, 6, 5, 15, 34), 1467732840000, '2016, 6, 5, 15, 34'
    25  	);
    26  
    27  	assert.sameValue(
    28    		Date.UTC(2016, 6, 5, 15, 34, 0), 1467732840000, '2016, 6, 5, 15, 34, 0'
    29  	);
    30  	assert.sameValue(
    31    		Date.UTC(2016, 6, 5, 15, 34, 45), 1467732885000, '2016, 6, 5, 15, 34, 45'
    32  	);
    33  
    34  	`
    35  
    36  	testScriptWithTestLib(SCRIPT, _undefined, t)
    37  }
    38  
    39  func TestNewDate(t *testing.T) {
    40  	const SCRIPT = `
    41  	var d1 = new Date("2016-09-01T12:34:56Z");
    42  	d1.getUTCHours() === 12;
    43  
    44  	`
    45  	testScript(SCRIPT, valueTrue, t)
    46  }
    47  
    48  func TestNewDate0(t *testing.T) {
    49  	const SCRIPT = `
    50  	(new Date(0)).toUTCString();
    51  
    52  	`
    53  	testScript(SCRIPT, asciiString("Thu, 01 Jan 1970 00:00:00 GMT"), t)
    54  }
    55  
    56  func TestSetHour(t *testing.T) {
    57  	l := time.Local
    58  	defer func() {
    59  		time.Local = l
    60  	}()
    61  	var err error
    62  	time.Local, err = time.LoadLocation("America/New_York")
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	const SCRIPT = `
    68  	var d = new Date(2016, 8, 1, 12, 23, 45)
    69  	assert.sameValue(d.getHours(), 12);
    70  	assert.sameValue(d.getUTCHours(), 16);
    71  
    72  	d.setHours(13);
    73  	assert.sameValue(d.getHours(), 13);
    74  	assert.sameValue(d.getMinutes(), 23);
    75  	assert.sameValue(d.getSeconds(), 45);
    76  
    77  	d.setUTCHours(13);
    78  	assert.sameValue(d.getHours(), 9);
    79  	assert.sameValue(d.getMinutes(), 23);
    80  	assert.sameValue(d.getSeconds(), 45);
    81  
    82  	`
    83  	testScriptWithTestLib(SCRIPT, _undefined, t)
    84  
    85  }
    86  
    87  func TestSetMinute(t *testing.T) {
    88  	l := time.Local
    89  	defer func() {
    90  		time.Local = l
    91  	}()
    92  	time.Local = time.FixedZone("Asia/Delhi", 5*60*60+30*60)
    93  
    94  	const SCRIPT = `
    95  	var d = new Date(2016, 8, 1, 12, 23, 45)
    96  	assert.sameValue(d.getHours(), 12);
    97  	assert.sameValue(d.getUTCHours(), 6);
    98  	assert.sameValue(d.getMinutes(), 23);
    99  	assert.sameValue(d.getUTCMinutes(), 53);
   100  
   101  	d.setMinutes(55);
   102  	assert.sameValue(d.getMinutes(), 55);
   103  	assert.sameValue(d.getSeconds(), 45);
   104  
   105  	d.setUTCMinutes(52);
   106  	assert.sameValue(d.getMinutes(), 22);
   107  	assert.sameValue(d.getHours(), 13);
   108  
   109  	`
   110  	testScriptWithTestLib(SCRIPT, _undefined, t)
   111  
   112  }
   113  
   114  func TestTimezoneOffset(t *testing.T) {
   115  	const SCRIPT = `
   116  	var d = new Date(0);
   117  	d.getTimezoneOffset();
   118  	`
   119  
   120  	l := time.Local
   121  	defer func() {
   122  		time.Local = l
   123  	}()
   124  	var err error
   125  	time.Local, err = time.LoadLocation("Europe/London")
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	testScript(SCRIPT, intToValue(-60), t)
   131  }
   132  
   133  func TestDateValueOf(t *testing.T) {
   134  	const SCRIPT = `
   135  	var d9 = new Date(1.23e15);
   136  	d9.valueOf();
   137  	`
   138  
   139  	testScript(SCRIPT, intToValue(1.23e15), t)
   140  }
   141  
   142  func TestDateSetters(t *testing.T) {
   143  	const SCRIPT = `
   144  	assert.sameValue((new Date(0)).setMilliseconds(2345), 2345, "setMilliseconds(2345)");
   145  	assert.sameValue(new Date(1000).setMilliseconds(23450000000000), 23450000001000, "setMilliseconds(23450000000000)");
   146  	assert.sameValue((new Date(0)).setUTCMilliseconds(2345), 2345, "setUTCMilliseconds()");
   147  	assert.sameValue((new Date(0)).setSeconds(12), 12000, "setSeconds()");
   148  	assert.sameValue((new Date(0)).setUTCSeconds(12), 12000, "setUTCSeconds()");
   149  	assert.sameValue((new Date(0)).setMinutes(12), 12 * 60 * 1000, "setMinutes()");
   150  	assert.sameValue((new Date(0)).setUTCMinutes(12), 12 * 60 * 1000, "setUTCMinutes()");
   151  	assert.sameValue((new Date("2016-06-01")).setHours(1), 1464739200000, "setHours()");
   152  	assert.sameValue((new Date("2016-06-01")).setUTCHours(1), 1464742800000, "setUTCHours()");
   153  	assert.sameValue((new Date(0)).setDate(2), 86400000, "setDate()");
   154  	assert.sameValue((new Date(0)).setUTCDate(2), 86400000, "setUTCDate()");
   155  	assert.sameValue((new Date(0)).setMonth(2), 5097600000, "setMonth()");
   156  	assert.sameValue((new Date(0)).setUTCMonth(2), 5097600000, "setUTCMonth()");
   157  	assert.sameValue((new Date(0)).setFullYear(1971), 31536000000, "setFullYear()");
   158  	assert.sameValue((new Date(0)).setFullYear(1971, 2, 3), 36806400000, "setFullYear(Y,M,D)");
   159  	assert.sameValue((new Date(0)).setUTCFullYear(1971), 31536000000, "setUTCFullYear()");
   160  	assert.sameValue((new Date(0)).setUTCFullYear(1971, 2, 3), 36806400000, "setUTCFullYear(Y,M,D)");
   161  
   162  	var d = new Date();
   163  	d.setTime(1151877845000);
   164  	assert.sameValue(d.getHours(), 23, "d.getHours()");
   165  	`
   166  
   167  	l := time.Local
   168  	defer func() {
   169  		time.Local = l
   170  	}()
   171  	var err error
   172  	time.Local, err = time.LoadLocation("Europe/London")
   173  	if err != nil {
   174  		t.Fatal(err)
   175  	}
   176  
   177  	testScriptWithTestLib(SCRIPT, _undefined, t)
   178  }
   179  
   180  func TestDateParse(t *testing.T) {
   181  	const SCRIPT = `
   182  	var zero = new Date(0);
   183  
   184  	assert.sameValue(zero.valueOf(), Date.parse(zero.toString()),
   185  					 "Date.parse(zeroDate.toString())");
   186  	assert.sameValue(zero.valueOf(), Date.parse(zero.toUTCString()),
   187  					 "Date.parse(zeroDate.toUTCString())");
   188  	assert.sameValue(zero.valueOf(), Date.parse(zero.toISOString()),
   189  					 "Date.parse(zeroDate.toISOString())");
   190  
   191  	function testParse(str, expected) {
   192  		assert.sameValue(Date.parse(str), expected, str);
   193  	}
   194  
   195  	testParse("Mon, 02 Jan 2006 15:04:05 MST",							1136239445000);
   196  	testParse("Tue, 22 Jun 2021 13:54:40 GMT",							1624370080000);
   197  	testParse("Tuesday, 22 Jun 2021 13:54:40 GMT",						1624370080000);
   198  	testParse("Mon, 02 Jan 2006 15:04:05 GMT-07:00 (MST)",				1136239445000);
   199  	testParse("Mon, 02 Jan 2006 15:04:05 -07:00 (MST)",					1136239445000);
   200  	testParse("Monday, 02 Jan 2006 15:04:05 -0700 (MST)",				1136239445000);
   201  	testParse("Mon Jan 02 2006 15:04:05 GMT-0700 (GMT Standard Time)",	1136239445000);
   202  	testParse("Mon Jan 2 15:04:05 MST 2006",							1136239445000);
   203  	testParse("Mon Jan 02 15:04:05 MST 2006",							1136239445000);
   204  	testParse("Mon Jan 02 15:04:05 -0700 2006",							1136239445000);
   205  
   206  	testParse("December 04, 1986",	534038400000);
   207  	testParse("Dec 04, 1986",		534038400000);
   208  	testParse("Dec 4, 1986",		534038400000);
   209  
   210  	testParse("2006-01-02T15:04:05.000Z",	1136214245000);
   211  	testParse("2006-06-02T15:04:05.000",	1149275045000);
   212  	testParse("2006-01-02T15:04:05",		1136232245000);
   213  	testParse("2006-01-02",					1136160000000);
   214  	testParse("2006T15:04-0700",			1136153040000);
   215  	testParse("2006T15:04Z",				1136127840000);
   216  	testParse("2019-01-01T12:00:00.52Z",	1546344000520);
   217  
   218  	var d = new Date("Mon, 02 Jan 2006 15:04:05 MST");
   219  
   220  	assert.sameValue(d.getUTCHours(), 22,
   221  					"new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getUTCHours()");
   222  
   223  	assert.sameValue(d.getHours(), 17,
   224  					"new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getHours()");
   225  
   226  	assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 zzz"), NaN,
   227  					 "Date.parse(\"Mon, 02 Jan 2006 15:04:05 zzz\")");
   228  
   229  	assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 ZZZ"), NaN,
   230  					 "Date.parse(\"Mon, 02 Jan 2006 15:04:05 ZZZ\")");
   231  
   232  	var minDateStr = "-271821-04-20T00:00:00.000Z";
   233  	var minDate = new Date(-8640000000000000);
   234  
   235  	assert.sameValue(minDate.toISOString(), minDateStr, "minDateStr");
   236  	assert.sameValue(Date.parse(minDateStr), minDate.valueOf(), "parse minDateStr");
   237  
   238  	var maxDateStr = "+275760-09-13T00:00:00.000Z";
   239  	var maxDate = new Date(8640000000000000);
   240  
   241  	assert.sameValue(maxDate.toISOString(), maxDateStr, "maxDateStr");
   242  	assert.sameValue(Date.parse(maxDateStr), maxDate.valueOf(), "parse maxDateStr");
   243  
   244  	var belowRange = "-271821-04-19T23:59:59.999Z";
   245  	var aboveRange = "+275760-09-13T00:00:00.001Z";
   246  
   247  	assert.sameValue(Date.parse(belowRange), NaN, "parse below minimum time value");
   248  	assert.sameValue(Date.parse(aboveRange), NaN, "parse above maximum time value");
   249  	`
   250  
   251  	l := time.Local
   252  	defer func() {
   253  		time.Local = l
   254  	}()
   255  	var err error
   256  	time.Local, err = time.LoadLocation("America/New_York")
   257  	if err != nil {
   258  		t.Fatal(err)
   259  	}
   260  
   261  	testScriptWithTestLib(SCRIPT, _undefined, t)
   262  }
   263  
   264  func TestDateMaxValues(t *testing.T) {
   265  	const SCRIPT = `
   266  	assert.sameValue((new Date(0)).setUTCMilliseconds(8.64e15), 8.64e15);
   267  	assert.sameValue((new Date(0)).setUTCSeconds(8640000000000), 8.64e15);
   268  	assert.sameValue((new Date(0)).setUTCMilliseconds(-8.64e15), -8.64e15);
   269  	assert.sameValue((new Date(0)).setUTCSeconds(-8640000000000), -8.64e15);
   270  	`
   271  	testScriptWithTestLib(SCRIPT, _undefined, t)
   272  }
   273  
   274  func TestDateExport(t *testing.T) {
   275  	vm := New()
   276  	res, err := vm.RunString(`new Date(1000)`)
   277  	if err != nil {
   278  		t.Fatal(err)
   279  	}
   280  	exp := res.Export()
   281  	if d, ok := exp.(time.Time); ok {
   282  		if d.UnixNano()/1e6 != 1000 {
   283  			t.Fatalf("Invalid exported date: %v", d)
   284  		}
   285  		if loc := d.Location(); loc != time.Local {
   286  			t.Fatalf("Invalid timezone: %v", loc)
   287  		}
   288  	} else {
   289  		t.Fatalf("Invalid export type: %T", exp)
   290  	}
   291  }
   292  
   293  func TestDateToJSON(t *testing.T) {
   294  	const SCRIPT = `
   295  	Date.prototype.toJSON.call({ toISOString: function () { return 1; } })
   296  	`
   297  	testScript(SCRIPT, intToValue(1), t)
   298  }
   299  
   300  func TestDateExportType(t *testing.T) {
   301  	vm := New()
   302  	v, err := vm.RunString(`new Date()`)
   303  	if err != nil {
   304  		t.Fatal(err)
   305  	}
   306  	if typ := v.ExportType(); typ != typeTime {
   307  		t.Fatal(typ)
   308  	}
   309  }