date_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. `
  68. func TestDateUTC(t *testing.T) {
  69. const SCRIPT = `
  70. assert.sameValue(Date.UTC(1970, 0), 0, '1970, 0');
  71. assert.sameValue(Date.UTC(2016, 0), 1451606400000, '2016, 0');
  72. assert.sameValue(Date.UTC(2016, 6), 1467331200000, '2016, 6');
  73. assert.sameValue(Date.UTC(2016, 6, 1), 1467331200000, '2016, 6, 1');
  74. assert.sameValue(Date.UTC(2016, 6, 5), 1467676800000, '2016, 6, 5');
  75. assert.sameValue(Date.UTC(2016, 6, 5, 0), 1467676800000, '2016, 6, 5, 0');
  76. assert.sameValue(Date.UTC(2016, 6, 5, 15), 1467730800000, '2016, 6, 5, 15');
  77. assert.sameValue(
  78. Date.UTC(2016, 6, 5, 15, 0), 1467730800000, '2016, 6, 5, 15, 0'
  79. );
  80. assert.sameValue(
  81. Date.UTC(2016, 6, 5, 15, 34), 1467732840000, '2016, 6, 5, 15, 34'
  82. );
  83. assert.sameValue(
  84. Date.UTC(2016, 6, 5, 15, 34, 0), 1467732840000, '2016, 6, 5, 15, 34, 0'
  85. );
  86. assert.sameValue(
  87. Date.UTC(2016, 6, 5, 15, 34, 45), 1467732885000, '2016, 6, 5, 15, 34, 45'
  88. );
  89. `
  90. testScript1(TESTLIB+SCRIPT, _undefined, t)
  91. }
  92. func TestNewDate(t *testing.T) {
  93. const SCRIPT = `
  94. var d1 = new Date("2016-09-01T12:34:56Z");
  95. d1.getUTCHours() === 12;
  96. `
  97. testScript1(SCRIPT, valueTrue, t)
  98. }
  99. func TestNewDate0(t *testing.T) {
  100. const SCRIPT = `
  101. (new Date(0)).toUTCString();
  102. `
  103. testScript1(SCRIPT, asciiString("Thu, 01 Jan 1970 00:00:00 GMT"), t)
  104. }
  105. func TestSetHour(t *testing.T) {
  106. l := time.Local
  107. defer func() {
  108. time.Local = l
  109. }()
  110. var err error
  111. time.Local, err = time.LoadLocation("America/New_York")
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. const SCRIPT = `
  116. var d = new Date(2016, 8, 1, 12, 23, 45)
  117. assert.sameValue(d.getHours(), 12);
  118. assert.sameValue(d.getUTCHours(), 16);
  119. d.setHours(13);
  120. assert.sameValue(d.getHours(), 13);
  121. assert.sameValue(d.getMinutes(), 23);
  122. assert.sameValue(d.getSeconds(), 45);
  123. d.setUTCHours(13);
  124. assert.sameValue(d.getHours(), 9);
  125. assert.sameValue(d.getMinutes(), 23);
  126. assert.sameValue(d.getSeconds(), 45);
  127. `
  128. testScript1(TESTLIB+SCRIPT, _undefined, t)
  129. }
  130. func TestSetMinute(t *testing.T) {
  131. l := time.Local
  132. defer func() {
  133. time.Local = l
  134. }()
  135. time.Local = time.FixedZone("Asia/Delhi", 5*60*60+30*60)
  136. const SCRIPT = `
  137. var d = new Date(2016, 8, 1, 12, 23, 45)
  138. assert.sameValue(d.getHours(), 12);
  139. assert.sameValue(d.getUTCHours(), 6);
  140. assert.sameValue(d.getMinutes(), 23);
  141. assert.sameValue(d.getUTCMinutes(), 53);
  142. d.setMinutes(55);
  143. assert.sameValue(d.getMinutes(), 55);
  144. assert.sameValue(d.getSeconds(), 45);
  145. d.setUTCMinutes(52);
  146. assert.sameValue(d.getMinutes(), 22);
  147. assert.sameValue(d.getHours(), 13);
  148. `
  149. testScript1(TESTLIB+SCRIPT, _undefined, t)
  150. }
  151. func TestTimezoneOffset(t *testing.T) {
  152. const SCRIPT = `
  153. var d = new Date(0);
  154. d.getTimezoneOffset();
  155. `
  156. l := time.Local
  157. defer func() {
  158. time.Local = l
  159. }()
  160. var err error
  161. time.Local, err = time.LoadLocation("Europe/London")
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. testScript1(SCRIPT, intToValue(-60), t)
  166. }
  167. func TestDateValueOf(t *testing.T) {
  168. const SCRIPT = `
  169. var d9 = new Date(1.23e15);
  170. d9.valueOf();
  171. `
  172. testScript1(SCRIPT, intToValue(1.23e15), t)
  173. }
  174. func TestDateSetters(t *testing.T) {
  175. const SCRIPT = `
  176. assert.sameValue((new Date(0)).setMilliseconds(2345), 2345, "setMilliseconds(2345)");
  177. assert.sameValue(new Date(1000).setMilliseconds(23450000000000), 23450000001000, "setMilliseconds(23450000000000)");
  178. assert.sameValue((new Date(0)).setUTCMilliseconds(2345), 2345, "setUTCMilliseconds()");
  179. assert.sameValue((new Date(0)).setSeconds(12), 12000, "setSeconds()");
  180. assert.sameValue((new Date(0)).setUTCSeconds(12), 12000, "setUTCSeconds()");
  181. assert.sameValue((new Date(0)).setMinutes(12), 12 * 60 * 1000, "setMinutes()");
  182. assert.sameValue((new Date(0)).setUTCMinutes(12), 12 * 60 * 1000, "setUTCMinutes()");
  183. assert.sameValue((new Date("2016-06-01")).setHours(1), 1464739200000, "setHours()");
  184. assert.sameValue((new Date("2016-06-01")).setUTCHours(1), 1464742800000, "setUTCHours()");
  185. assert.sameValue((new Date(0)).setDate(2), 86400000, "setDate()");
  186. assert.sameValue((new Date(0)).setUTCDate(2), 86400000, "setUTCDate()");
  187. assert.sameValue((new Date(0)).setMonth(2), 5097600000, "setMonth()");
  188. assert.sameValue((new Date(0)).setUTCMonth(2), 5097600000, "setUTCMonth()");
  189. assert.sameValue((new Date(0)).setFullYear(1971), 31536000000, "setFullYear()");
  190. assert.sameValue((new Date(0)).setFullYear(1971, 2, 3), 36806400000, "setFullYear(Y,M,D)");
  191. assert.sameValue((new Date(0)).setUTCFullYear(1971), 31536000000, "setUTCFullYear()");
  192. assert.sameValue((new Date(0)).setUTCFullYear(1971, 2, 3), 36806400000, "setUTCFullYear(Y,M,D)");
  193. var d = new Date();
  194. d.setTime(1151877845000);
  195. assert.sameValue(d.getHours(), 23, "d.getHours()");
  196. `
  197. l := time.Local
  198. defer func() {
  199. time.Local = l
  200. }()
  201. var err error
  202. time.Local, err = time.LoadLocation("Europe/London")
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. testScript1(TESTLIB+SCRIPT, _undefined, t)
  207. }
  208. func TestDateParse(t *testing.T) {
  209. const SCRIPT = `
  210. var zero = new Date(0);
  211. assert.sameValue(zero.valueOf(), Date.parse(zero.toString()),
  212. "Date.parse(zeroDate.toString())");
  213. assert.sameValue(zero.valueOf(), Date.parse(zero.toUTCString()),
  214. "Date.parse(zeroDate.toUTCString())");
  215. assert.sameValue(zero.valueOf(), Date.parse(zero.toISOString()),
  216. "Date.parse(zeroDate.toISOString())");
  217. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 MST"), 1136239445000,
  218. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 MST\")");
  219. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 GMT-07:00 (MST)"), 1136239445000,
  220. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 GMT-07:00 (MST)\")");
  221. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 -07:00 (MST)"), 1136239445000,
  222. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 -07:00 (MST)\")");
  223. assert.sameValue(Date.parse("Monday, 02 Jan 2006 15:04:05 -0700 (MST)"), 1136239445000,
  224. "Date.parse(\"Monday, 02 Jan 2006 15:04:05 -0700 (MST)\")");
  225. assert.sameValue(Date.parse("Mon Jan 02 2006 15:04:05 GMT-0700 (GMT Standard Time)"), 1136239445000,
  226. "Date.parse(\"Mon Jan 02 2006 15:04:05 GMT-0700 (GMT Standard Time)\")");
  227. assert.sameValue(Date.parse("2006-01-02T15:04:05.000Z"), 1136214245000,
  228. "Date.parse(\"2006-01-02T15:04:05.000Z\")");
  229. assert.sameValue(Date.parse("2006-06-02T15:04:05.000"), 1149260645000,
  230. "Date.parse(\"2006-01-02T15:04:05.000\")");
  231. assert.sameValue(Date.parse("2006-01-02T15:04:05"), 1136214245000,
  232. "Date.parse(\"2006-01-02T15:04:05\")");
  233. assert.sameValue(Date.parse("2006-01-02"), 1136160000000,
  234. "Date.parse(\"2006-01-02\")");
  235. assert.sameValue(Date.parse("2006T15:04-0700"), 1136153040000,
  236. "Date.parse(\"2006T15:04-0700\")");
  237. assert.sameValue(Date.parse("2006T15:04Z"), 1136127840000,
  238. "Date.parse(\"2006T15:04Z\")");
  239. assert.sameValue(Date.parse("Mon Jan 2 15:04:05 MST 2006"), 1136239445000,
  240. "Date.parse(\"Mon Jan 2 15:04:05 MST 2006\")");
  241. assert.sameValue(Date.parse("Mon Jan 02 15:04:05 MST 2006"), 1136239445000,
  242. "Date.parse(\"Mon Jan 02 15:04:05 MST 2006\")");
  243. assert.sameValue(Date.parse("Mon Jan 02 15:04:05 -0700 2006"), 1136239445000,
  244. "Date.parse(\"Mon Jan 02 15:04:05 -0700 2006\")");
  245. assert.sameValue(Date.parse("2019-01-01T12:00:00.52Z"), 1546344000520,
  246. "Date.parse(\"2019-01-01T12:00:00.52\")");
  247. var d = new Date("Mon, 02 Jan 2006 15:04:05 MST");
  248. assert.sameValue(d.getUTCHours(), 22,
  249. "new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getUTCHours()");
  250. assert.sameValue(d.getHours(), 17,
  251. "new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getHours()");
  252. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 zzz"), NaN,
  253. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 zzz\")");
  254. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 ZZZ"), NaN,
  255. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 ZZZ\")");
  256. var minDateStr = "-271821-04-20T00:00:00.000Z";
  257. var minDate = new Date(-8640000000000000);
  258. assert.sameValue(minDate.toISOString(), minDateStr, "minDateStr");
  259. assert.sameValue(Date.parse(minDateStr), minDate.valueOf(), "parse minDateStr");
  260. var maxDateStr = "+275760-09-13T00:00:00.000Z";
  261. var maxDate = new Date(8640000000000000);
  262. assert.sameValue(maxDate.toISOString(), maxDateStr, "maxDateStr");
  263. assert.sameValue(Date.parse(maxDateStr), maxDate.valueOf(), "parse maxDateStr");
  264. var belowRange = "-271821-04-19T23:59:59.999Z";
  265. var aboveRange = "+275760-09-13T00:00:00.001Z";
  266. assert.sameValue(Date.parse(belowRange), NaN, "parse below minimum time value");
  267. assert.sameValue(Date.parse(aboveRange), NaN, "parse above maximum time value");
  268. `
  269. l := time.Local
  270. defer func() {
  271. time.Local = l
  272. }()
  273. var err error
  274. time.Local, err = time.LoadLocation("America/New_York")
  275. if err != nil {
  276. t.Fatal(err)
  277. }
  278. testScript1(TESTLIB+SCRIPT, _undefined, t)
  279. }