A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.6 KiB

12 years ago
  1. var common = require('../../common');
  2. var connection = common.createConnection();
  3. var assert = require('assert');
  4. common.useTestDb(connection);
  5. var table = 'timezone_test';
  6. connection.query([
  7. 'CREATE TEMPORARY TABLE `' + table + '`(',
  8. '`offset` varchar(10),',
  9. '`dt` datetime',
  10. ') ENGINE=InnoDB DEFAULT CHARSET=utf8'
  11. ].join('\n'));
  12. var tests = [ 0, 1, 5, 12, 26, -1, -5, -20, 'Z', 'local' ];
  13. connection.query('DELETE FROM ' + table);
  14. testNextDate();
  15. function testNextDate() {
  16. if (tests.length === 0) {
  17. return connection.end();
  18. }
  19. var test = tests.pop();
  20. testDate(test, function () {
  21. testNextDate();
  22. });
  23. }
  24. function testDate(offset, cb) {
  25. var dt = new Date();
  26. if (offset == 'Z' || offset == 'local') {
  27. connection.config.timezone = offset;
  28. } else {
  29. connection.config.timezone = (offset < 0 ? "-" : "+") + pad2(Math.abs(offset)) + ":00";
  30. }
  31. connection.query('INSERT INTO ' + table + ' SET ?', { offset: offset, dt: dt });
  32. if (offset == 'Z') {
  33. dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000));
  34. } else if (offset != 'local') {
  35. dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000) + (offset * 3600000));
  36. }
  37. connection.query({
  38. sql: 'SELECT * FROM ' + table + ' WHERE offset = \'' + offset + '\'',
  39. typeCast: function (field, next) {
  40. if (field.type != 'DATETIME') return next();
  41. return new Date(field.string());
  42. }
  43. }, function (err, result) {
  44. if (err) throw err;
  45. assert.strictEqual(dt.toString(), result[0].dt.toString());
  46. return cb();
  47. });
  48. }
  49. function pad2(v) {
  50. return (v < 10 ? "0" : "") + v;
  51. }