Permalink
Browse files

feat(filter): support conversion to timezone other than UTC

Closes #10858
  • Loading branch information...
1 parent 8a2c80c commit c6d8512a1d7345516d1bd9a039d81821b9518bff @shahata shahata committed with petebacondarwin Jan 24, 2015
Showing with 16 additions and 4 deletions.
  1. +7 −4 src/ng/filter/filters.js
  2. +9 −0 test/ng/filter/filtersSpec.js
@@ -485,10 +485,13 @@ function dateFilter($locale) {
}
var dateTimezoneOffset = date.getTimezoneOffset();
- if (timezone && timezone === 'UTC') {
- date = new Date(date.getTime());
- date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
- dateTimezoneOffset = 0;
+ if (timezone) {
+ var requestedTimezoneOffset = Date.parse('Jan 01, 1970 00:00:00 ' + timezone) / 60000;
+ if (!isNaN(requestedTimezoneOffset)) {
+ date = new Date(date.getTime());
+ date.setMinutes(date.getMinutes() + dateTimezoneOffset - requestedTimezoneOffset);
+ dateTimezoneOffset = requestedTimezoneOffset;
+ }
}
forEach(parts, function(value) {
fn = DATE_FORMATS[value];
@@ -458,5 +458,14 @@ describe('filters', function() {
expect(date(new Date(Date.UTC(2003, 8, 10, 3, 2, 4)), 'yyyy-MM-dd HH-mm-ss', 'UTC')).toEqual('2003-09-10 03-02-04');
expect(date(new Date(Date.UTC(2003, 8, 10, 3, 2, 4)), 'yyyy-MM-dd HH-mm-ssZ', 'UTC')).toEqual('2003-09-10 03-02-04+0000');
});
+
+ it('should support conversion to any timezone', function() {
+ expect(date(new Date(Date.UTC(2003, 8, 10, 3, 2, 4)), 'yyyy-MM-dd HH-mm-ssZ', 'GMT+0500')).toEqual('2003-09-10 08-02-04+0500');
+ });
+
+ it('should fallback to default timezone in case an unknown timezone was passed', function() {
+ var value = new angular.mock.TzDate(-2, '2003-09-10T01:02:04.000Z');
+ expect(date(value, 'yyyy-MM-dd HH-mm-ssZ', 'WTF')).toEqual('2003-09-10 03-02-04+0200');
+ });
});
});

0 comments on commit c6d8512

Please sign in to comment.