date_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. package goja
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. const TESTLIB = `
  7. function $ERROR(message) {
  8. throw new Error(message);
  9. }
  10. function Test262Error() {
  11. }
  12. function assert(mustBeTrue, message) {
  13. if (mustBeTrue === true) {
  14. return;
  15. }
  16. if (message === undefined) {
  17. message = 'Expected true but got ' + String(mustBeTrue);
  18. }
  19. $ERROR(message);
  20. }
  21. assert._isSameValue = function (a, b) {
  22. if (a === b) {
  23. // Handle +/-0 vs. -/+0
  24. return a !== 0 || 1 / a === 1 / b;
  25. }
  26. // Handle NaN vs. NaN
  27. return a !== a && b !== b;
  28. };
  29. assert.sameValue = function (actual, expected, message) {
  30. if (assert._isSameValue(actual, expected)) {
  31. return;
  32. }
  33. if (message === undefined) {
  34. message = '';
  35. } else {
  36. message += ' ';
  37. }
  38. message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true';
  39. $ERROR(message);
  40. };
  41. assert.throws = function (expectedErrorConstructor, func, message) {
  42. if (typeof func !== "function") {
  43. $ERROR('assert.throws requires two arguments: the error constructor ' +
  44. 'and a function to run');
  45. return;
  46. }
  47. if (message === undefined) {
  48. message = '';
  49. } else {
  50. message += ' ';
  51. }
  52. try {
  53. func();
  54. } catch (thrown) {
  55. if (typeof thrown !== 'object' || thrown === null) {
  56. message += 'Thrown value was not an object!';
  57. $ERROR(message);
  58. } else if (thrown.constructor !== expectedErrorConstructor) {
  59. message += 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
  60. $ERROR(message);
  61. }
  62. return;
  63. }
  64. message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
  65. $ERROR(message);
  66. };
  67. function compareArray(a, b) {
  68. if (b.length !== a.length) {
  69. return false;
  70. }
  71. for (var i = 0; i < a.length; i++) {
  72. if (b[i] !== a[i]) {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. `
  79. func TestDateUTC(t *testing.T) {
  80. const SCRIPT = `
  81. assert.sameValue(Date.UTC(1970, 0), 0, '1970, 0');
  82. assert.sameValue(Date.UTC(2016, 0), 1451606400000, '2016, 0');
  83. assert.sameValue(Date.UTC(2016, 6), 1467331200000, '2016, 6');
  84. assert.sameValue(Date.UTC(2016, 6, 1), 1467331200000, '2016, 6, 1');
  85. assert.sameValue(Date.UTC(2016, 6, 5), 1467676800000, '2016, 6, 5');
  86. assert.sameValue(Date.UTC(2016, 6, 5, 0), 1467676800000, '2016, 6, 5, 0');
  87. assert.sameValue(Date.UTC(2016, 6, 5, 15), 1467730800000, '2016, 6, 5, 15');
  88. assert.sameValue(
  89. Date.UTC(2016, 6, 5, 15, 0), 1467730800000, '2016, 6, 5, 15, 0'
  90. );
  91. assert.sameValue(
  92. Date.UTC(2016, 6, 5, 15, 34), 1467732840000, '2016, 6, 5, 15, 34'
  93. );
  94. assert.sameValue(
  95. Date.UTC(2016, 6, 5, 15, 34, 0), 1467732840000, '2016, 6, 5, 15, 34, 0'
  96. );
  97. assert.sameValue(
  98. Date.UTC(2016, 6, 5, 15, 34, 45), 1467732885000, '2016, 6, 5, 15, 34, 45'
  99. );
  100. `
  101. testScript1(TESTLIB+SCRIPT, _undefined, t)
  102. }
  103. func TestNewDate(t *testing.T) {
  104. const SCRIPT = `
  105. var d1 = new Date("2016-09-01T12:34:56Z");
  106. d1.getUTCHours() === 12;
  107. `
  108. testScript1(SCRIPT, valueTrue, t)
  109. }
  110. func TestNewDate0(t *testing.T) {
  111. const SCRIPT = `
  112. (new Date(0)).toUTCString();
  113. `
  114. testScript1(SCRIPT, asciiString("Thu, 01 Jan 1970 00:00:00 GMT"), t)
  115. }
  116. func TestSetHour(t *testing.T) {
  117. l := time.Local
  118. defer func() {
  119. time.Local = l
  120. }()
  121. var err error
  122. time.Local, err = time.LoadLocation("America/New_York")
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. const SCRIPT = `
  127. var d = new Date(2016, 8, 1, 12, 23, 45)
  128. assert.sameValue(d.getHours(), 12);
  129. assert.sameValue(d.getUTCHours(), 16);
  130. d.setHours(13);
  131. assert.sameValue(d.getHours(), 13);
  132. assert.sameValue(d.getMinutes(), 23);
  133. assert.sameValue(d.getSeconds(), 45);
  134. d.setUTCHours(13);
  135. assert.sameValue(d.getHours(), 9);
  136. assert.sameValue(d.getMinutes(), 23);
  137. assert.sameValue(d.getSeconds(), 45);
  138. `
  139. testScript1(TESTLIB+SCRIPT, _undefined, t)
  140. }
  141. func TestSetMinute(t *testing.T) {
  142. l := time.Local
  143. defer func() {
  144. time.Local = l
  145. }()
  146. time.Local = time.FixedZone("Asia/Delhi", 5*60*60+30*60)
  147. const SCRIPT = `
  148. var d = new Date(2016, 8, 1, 12, 23, 45)
  149. assert.sameValue(d.getHours(), 12);
  150. assert.sameValue(d.getUTCHours(), 6);
  151. assert.sameValue(d.getMinutes(), 23);
  152. assert.sameValue(d.getUTCMinutes(), 53);
  153. d.setMinutes(55);
  154. assert.sameValue(d.getMinutes(), 55);
  155. assert.sameValue(d.getSeconds(), 45);
  156. d.setUTCMinutes(52);
  157. assert.sameValue(d.getMinutes(), 22);
  158. assert.sameValue(d.getHours(), 13);
  159. `
  160. testScript1(TESTLIB+SCRIPT, _undefined, t)
  161. }
  162. func TestTimezoneOffset(t *testing.T) {
  163. const SCRIPT = `
  164. var d = new Date(0);
  165. d.getTimezoneOffset();
  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. testScript1(SCRIPT, intToValue(-60), t)
  177. }
  178. func TestDateValueOf(t *testing.T) {
  179. const SCRIPT = `
  180. var d9 = new Date(1.23e15);
  181. d9.valueOf();
  182. `
  183. testScript1(SCRIPT, intToValue(1.23e15), t)
  184. }
  185. func TestDateSetters(t *testing.T) {
  186. const SCRIPT = `
  187. assert.sameValue((new Date(0)).setMilliseconds(2345), 2345, "setMilliseconds(2345)");
  188. assert.sameValue(new Date(1000).setMilliseconds(23450000000000), 23450000001000, "setMilliseconds(23450000000000)");
  189. assert.sameValue((new Date(0)).setUTCMilliseconds(2345), 2345, "setUTCMilliseconds()");
  190. assert.sameValue((new Date(0)).setSeconds(12), 12000, "setSeconds()");
  191. assert.sameValue((new Date(0)).setUTCSeconds(12), 12000, "setUTCSeconds()");
  192. assert.sameValue((new Date(0)).setMinutes(12), 12 * 60 * 1000, "setMinutes()");
  193. assert.sameValue((new Date(0)).setUTCMinutes(12), 12 * 60 * 1000, "setUTCMinutes()");
  194. assert.sameValue((new Date("2016-06-01")).setHours(1), 1464739200000, "setHours()");
  195. assert.sameValue((new Date("2016-06-01")).setUTCHours(1), 1464742800000, "setUTCHours()");
  196. assert.sameValue((new Date(0)).setDate(2), 86400000, "setDate()");
  197. assert.sameValue((new Date(0)).setUTCDate(2), 86400000, "setUTCDate()");
  198. assert.sameValue((new Date(0)).setMonth(2), 5097600000, "setMonth()");
  199. assert.sameValue((new Date(0)).setUTCMonth(2), 5097600000, "setUTCMonth()");
  200. assert.sameValue((new Date(0)).setFullYear(1971), 31536000000, "setFullYear()");
  201. assert.sameValue((new Date(0)).setFullYear(1971, 2, 3), 36806400000, "setFullYear(Y,M,D)");
  202. assert.sameValue((new Date(0)).setUTCFullYear(1971), 31536000000, "setUTCFullYear()");
  203. assert.sameValue((new Date(0)).setUTCFullYear(1971, 2, 3), 36806400000, "setUTCFullYear(Y,M,D)");
  204. var d = new Date();
  205. d.setTime(1151877845000);
  206. assert.sameValue(d.getHours(), 23, "d.getHours()");
  207. `
  208. l := time.Local
  209. defer func() {
  210. time.Local = l
  211. }()
  212. var err error
  213. time.Local, err = time.LoadLocation("Europe/London")
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. testScript1(TESTLIB+SCRIPT, _undefined, t)
  218. }
  219. func TestDateParse(t *testing.T) {
  220. const SCRIPT = `
  221. var zero = new Date(0);
  222. assert.sameValue(zero.valueOf(), Date.parse(zero.toString()),
  223. "Date.parse(zeroDate.toString())");
  224. assert.sameValue(zero.valueOf(), Date.parse(zero.toUTCString()),
  225. "Date.parse(zeroDate.toUTCString())");
  226. assert.sameValue(zero.valueOf(), Date.parse(zero.toISOString()),
  227. "Date.parse(zeroDate.toISOString())");
  228. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 MST"), 1136239445000,
  229. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 MST\")");
  230. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 GMT-07:00 (MST)"), 1136239445000,
  231. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 GMT-07:00 (MST)\")");
  232. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 -07:00 (MST)"), 1136239445000,
  233. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 -07:00 (MST)\")");
  234. assert.sameValue(Date.parse("Monday, 02 Jan 2006 15:04:05 -0700 (MST)"), 1136239445000,
  235. "Date.parse(\"Monday, 02 Jan 2006 15:04:05 -0700 (MST)\")");
  236. assert.sameValue(Date.parse("Mon Jan 02 2006 15:04:05 GMT-0700 (GMT Standard Time)"), 1136239445000,
  237. "Date.parse(\"Mon Jan 02 2006 15:04:05 GMT-0700 (GMT Standard Time)\")");
  238. assert.sameValue(Date.parse("2006-01-02T15:04:05.000Z"), 1136214245000,
  239. "Date.parse(\"2006-01-02T15:04:05.000Z\")");
  240. assert.sameValue(Date.parse("2006-06-02T15:04:05.000"), 1149260645000,
  241. "Date.parse(\"2006-01-02T15:04:05.000\")");
  242. assert.sameValue(Date.parse("2006-01-02T15:04:05"), 1136214245000,
  243. "Date.parse(\"2006-01-02T15:04:05\")");
  244. assert.sameValue(Date.parse("2006-01-02"), 1136160000000,
  245. "Date.parse(\"2006-01-02\")");
  246. assert.sameValue(Date.parse("2006T15:04-0700"), 1136153040000,
  247. "Date.parse(\"2006T15:04-0700\")");
  248. assert.sameValue(Date.parse("2006T15:04Z"), 1136127840000,
  249. "Date.parse(\"2006T15:04Z\")");
  250. assert.sameValue(Date.parse("Mon Jan 2 15:04:05 MST 2006"), 1136239445000,
  251. "Date.parse(\"Mon Jan 2 15:04:05 MST 2006\")");
  252. assert.sameValue(Date.parse("Mon Jan 02 15:04:05 MST 2006"), 1136239445000,
  253. "Date.parse(\"Mon Jan 02 15:04:05 MST 2006\")");
  254. assert.sameValue(Date.parse("Mon Jan 02 15:04:05 -0700 2006"), 1136239445000,
  255. "Date.parse(\"Mon Jan 02 15:04:05 -0700 2006\")");
  256. assert.sameValue(Date.parse("2019-01-01T12:00:00.52Z"), 1546344000520,
  257. "Date.parse(\"2019-01-01T12:00:00.52\")");
  258. var d = new Date("Mon, 02 Jan 2006 15:04:05 MST");
  259. assert.sameValue(d.getUTCHours(), 22,
  260. "new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getUTCHours()");
  261. assert.sameValue(d.getHours(), 17,
  262. "new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getHours()");
  263. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 zzz"), NaN,
  264. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 zzz\")");
  265. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 ZZZ"), NaN,
  266. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 ZZZ\")");
  267. var minDateStr = "-271821-04-20T00:00:00.000Z";
  268. var minDate = new Date(-8640000000000000);
  269. assert.sameValue(minDate.toISOString(), minDateStr, "minDateStr");
  270. assert.sameValue(Date.parse(minDateStr), minDate.valueOf(), "parse minDateStr");
  271. var maxDateStr = "+275760-09-13T00:00:00.000Z";
  272. var maxDate = new Date(8640000000000000);
  273. assert.sameValue(maxDate.toISOString(), maxDateStr, "maxDateStr");
  274. assert.sameValue(Date.parse(maxDateStr), maxDate.valueOf(), "parse maxDateStr");
  275. var belowRange = "-271821-04-19T23:59:59.999Z";
  276. var aboveRange = "+275760-09-13T00:00:00.001Z";
  277. assert.sameValue(Date.parse(belowRange), NaN, "parse below minimum time value");
  278. assert.sameValue(Date.parse(aboveRange), NaN, "parse above maximum time value");
  279. `
  280. l := time.Local
  281. defer func() {
  282. time.Local = l
  283. }()
  284. var err error
  285. time.Local, err = time.LoadLocation("America/New_York")
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. testScript1(TESTLIB+SCRIPT, _undefined, t)
  290. }
  291. func TestDateMaxValues(t *testing.T) {
  292. const SCRIPT = `
  293. assert.sameValue((new Date(0)).setUTCMilliseconds(8.64e15), 8.64e15);
  294. assert.sameValue((new Date(0)).setUTCSeconds(8640000000000), 8.64e15);
  295. assert.sameValue((new Date(0)).setUTCMilliseconds(-8.64e15), -8.64e15);
  296. assert.sameValue((new Date(0)).setUTCSeconds(-8640000000000), -8.64e15);
  297. `
  298. testScript1(TESTLIB+SCRIPT, _undefined, t)
  299. }