Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding default tzOffset config and documentation.

  • Loading branch information...
commit df4097e3fe91b984522ca8f9524e5f51c3ee8aa8 1 parent 3ff76ea
@paularmstrong authored
View
10 docs/filters.md
@@ -64,10 +64,14 @@ Convert a valid date into a format as specified. Mostly conforms to [php.net's d
#### Arguments
-1. <var>**format**</var> (_string_) The format to convert the date to.
+1. <var>**format**</var> (_string_) The format to convert the date to. See the [date formatting table](#dateformatting).
+1. <var>**tzOffset**</var> (_number_) The timezone offset in minutes from GMT to display the date in.
+ * For example, if your server is running in PDT, but you want to display your dates in CDT, set this to 360.
+ * This can also be set globally via config variable `tzOffset` on [swig.init](getting-started.md#init).
+
<table style="width: 100%">
- <caption>Date formatting character information from <a href="http://php.net/date">php.net/date</a>.</caption>
+ <caption><a name="dateformatting"></a>Date formatting character information from <a href="http://php.net/date">php.net/date</a>.</caption>
<thead>
<tr>
<th>Format Character</th>
@@ -445,4 +449,4 @@ In your `myfilters.js` file, each filter method is just a simple javascript meth
return input.toString().toLowerCase();
};
-For more examples, view the [filters.js source file](../lib/filters.js)
+For more examples, view the [filters.js source file](../lib/filters.js)
View
7 docs/getting-started.md
@@ -12,7 +12,8 @@ In order to start using Swig, you should initialize it. Swig can be configured u
encoding: 'utf8',
filters: {},
root: '/',
- tags: {}
+ tags: {},
+ tzOffset: 0
});
This step is _optional_, however it is recommended to at least set the `root` key when running Swig from node.js.
@@ -51,6 +52,10 @@ The directory to search for templates. If a template passed to `swig.compileFile
Use this to set any custom tags and/or override any of the built-in tags. For more information on writing your own tags, see the [custom tags guide](custom-tags.md).
+#### tzOffset _optional_
+
+Sets a default timezone offset, in minutes from GMT. Setting this will make the [date filter](filters.md#date) automatically convert dates parsed through the date filter to the appropriate timezone offset.
+
Parsing a Template <a name="parsing" href="#parsing">#</a>
------------------
View
6 index.js
@@ -5,6 +5,7 @@ var fs = require('fs'),
parser = require('./lib/parser'),
filters = require('./lib/filters'),
helpers = require('./lib/helpers'),
+ dateformat = require('./lib/dateformat'),
_ = require('underscore'),
@@ -15,7 +16,8 @@ var fs = require('fs'),
encoding: 'utf8',
filters: filters,
root: '/',
- tags: tags
+ tags: tags,
+ tzOffset: 0
},
_config = _.extend({}, config),
CACHE = {};
@@ -26,6 +28,8 @@ exports.init = function (options) {
_config = _.extend({}, config, options);
_config.filters = _.extend(filters, options.filters);
_config.tags = _.extend(tags, options.tags);
+
+ dateformat.defaultTZOffset = _config.tzOffset;
};
function TemplateError(error) {
View
5 lib/dateformat.js
@@ -16,6 +16,7 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
+exports.defaultTZOffset = 0;
exports.DateZ = function () {
var members = {
'default': ['getUTCDate', 'getUTCDay', 'getUTCFullYear', 'getUTCHours', 'getUTCMilliseconds', 'getUTCMinutes', 'getUTCMonth', 'getUTCSeconds', 'toISOString', 'toGMTString', 'toUTCString', 'valueOf', 'getTime'],
@@ -78,6 +79,10 @@ exports.DateZ = function () {
return d;
};
});
+
+ if (exports.defaultTZOffset) {
+ this.setTimezoneOffset(exports.defaultTZOffset);
+ }
};
exports.DateZ.prototype = {
getTimezoneOffset: function () {
View
23 tests/filters.test.js
@@ -115,6 +115,29 @@ exports.date = function (test) {
test.done();
};
+exports['date with global offset'] = function (test) {
+ swig.init({
+ allowErrors: true,
+ tzOffset: 360
+ });
+
+ var tpl = swig.compile('{{ d|date("H:i:s") }}'),
+ date = new Date(2011, 8, 6, 9, 20, 10),
+ tzOffset = 480,
+ offset = date.getTimezoneOffset();
+
+ // make the date in PDT timezone
+ if (offset !== tzOffset) {
+ date = new Date(date.getTime() - ((offset * 60000) - (tzOffset * 60000)));
+ }
+
+ // date should return as same time in CDT, relative to PDT
+ test.strictEqual('11:20:10', swig.compile('{{ d|date("H:i:s") }}')({ d: date }), 'uses config value 360');
+ test.strictEqual('12:20:10', swig.compile('{{ d|date("H:i:s", 300) }}')({ d: date }), 'uses override 300');
+ test.strictEqual('20:40:10', swig.compile('{{ d|date("H:i:s", -200) }}')({ d: date }), 'uses override -200');
+ test.done();
+};
+
exports['default'] = function (test) {
testFilter(test, 'default("blah")', { v: 'foo' }, 'foo', 'string not overridden by default');
testFilter(test, 'default("blah")', { v: 0 }, '0', 'zero not overridden by default');
Please sign in to comment.
Something went wrong with that request. Please try again.