date_test.go 9.2 KB

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