Skip to content

Commit 4b325f3

Browse files
refactor(moment): remove unused timezone setting (#5654)
* rename function adjustDateForTimezone * code style update
1 parent a18e974 commit 4b325f3

File tree

8 files changed

+22
-54
lines changed

8 files changed

+22
-54
lines changed

lib/models/page.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ export = (ctx: Hexo) => {
1111
title: {type: String, default: ''},
1212
date: {
1313
type: Moment,
14-
default: moment,
15-
language: ctx.config.languages,
16-
timezone: ctx.config.timezone
14+
default: moment
1715
},
1816
updated: {
19-
type: Moment,
20-
language: ctx.config.languages,
21-
timezone: ctx.config.timezone
17+
type: Moment
2218
},
2319
comments: {type: Boolean, default: true},
2420
layout: {type: String, default: 'page'},

lib/models/post.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ export = (ctx: Hexo) => {
2323
title: {type: String, default: ''},
2424
date: {
2525
type: Moment,
26-
default: moment,
27-
language: ctx.config.languages,
28-
timezone: ctx.config.timezone
26+
default: moment
2927
},
3028
updated: {
31-
type: Moment,
32-
language: ctx.config.languages,
33-
timezone: ctx.config.timezone
29+
type: Moment
3430
},
3531
comments: {type: Boolean, default: true},
3632
layout: {type: String, default: 'post'},

lib/models/types/moment.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import warehouse from 'warehouse';
2-
import { moment, toMomentLocale } from '../../plugins/helper/date';
2+
import { moment } from '../../plugins/helper/date';
33

44
// It'll pollute the moment module.
55
// declare module 'moment' {
@@ -20,13 +20,7 @@ class SchemaTypeMoment extends warehouse.SchemaType<moment.Moment> {
2020
value = super.cast(value, data);
2121
if (value == null) return value;
2222

23-
const { options } = this;
24-
value = toMoment(value);
25-
26-
if (options.language) value = value.locale(toMomentLocale(options.language));
27-
if (options.timezone) value = value.tz(options.timezone);
28-
29-
return value;
23+
return toMoment(value);
3024
}
3125

3226
validate(value, data?) {

lib/plugins/processor/asset.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { timezone, toDate, isExcludedFile, isMatch } from './common';
1+
import { adjustDateForTimezone, toDate, isExcludedFile, isMatch } from './common';
22
import Promise from 'bluebird';
33
import { parse as yfm } from 'hexo-front-matter';
44
import { extname, relative } from 'path';
@@ -34,7 +34,7 @@ function processPage(ctx: Hexo, file: _File) {
3434
const { path } = file;
3535
const doc = Page.findOne({source: path});
3636
const { config } = ctx;
37-
const { timezone: timezoneCfg } = config;
37+
const { timezone } = config;
3838
const updated_option = config.updated_option;
3939

4040
if (file.type === 'skip' && doc) {
@@ -62,15 +62,15 @@ function processPage(ctx: Hexo, file: _File) {
6262
data.date = toDate(data.date) as any;
6363

6464
if (data.date) {
65-
if (timezoneCfg) data.date = timezone(data.date, timezoneCfg) as any;
65+
if (timezone) data.date = adjustDateForTimezone(data.date, timezone) as any;
6666
} else {
6767
data.date = stats.ctime as any;
6868
}
6969

7070
data.updated = toDate(data.updated) as any;
7171

7272
if (data.updated) {
73-
if (timezoneCfg) data.updated = timezone(data.updated, timezoneCfg) as any;
73+
if (timezone) data.updated = adjustDateForTimezone(data.updated, timezone) as any;
7474
} else if (updated_option === 'date') {
7575
data.updated = data.date;
7676
} else if (updated_option === 'empty') {

lib/plugins/processor/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function toDate(date?: string | number | Date | moment.Moment): Date | un
4040
return date;
4141
}
4242

43-
export function timezone(date: Date | moment.Moment, timezone: string) {
43+
export function adjustDateForTimezone(date: Date | moment.Moment, timezone: string) {
4444
if (moment.isMoment(date)) date = date.toDate();
4545

4646
const offset = date.getTimezoneOffset();

lib/plugins/processor/post.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toDate, timezone, isExcludedFile, isTmpFile, isHiddenFile, isMatch } from './common';
1+
import { toDate, adjustDateForTimezone, isExcludedFile, isTmpFile, isHiddenFile, isMatch } from './common';
22
import Promise from 'bluebird';
33
import { parse as yfm } from 'hexo-front-matter';
44
import { extname, join, posix, sep } from 'path';
@@ -72,7 +72,7 @@ function processPost(ctx: Hexo, file: _File) {
7272
const { path } = file.params;
7373
const doc = Post.findOne({source: file.path});
7474
const { config } = ctx;
75-
const { timezone: timezoneCfg, updated_option, use_slug_as_post_title } = config;
75+
const { timezone, updated_option, use_slug_as_post_title } = config;
7676

7777
let categories, tags;
7878

@@ -129,15 +129,15 @@ function processPost(ctx: Hexo, file: _File) {
129129
}
130130

131131
if (data.date) {
132-
if (timezoneCfg) data.date = timezone(data.date, timezoneCfg) as any;
132+
if (timezone) data.date = adjustDateForTimezone(data.date, timezone) as any;
133133
} else {
134134
data.date = stats.birthtime as any;
135135
}
136136

137137
data.updated = toDate(data.updated) as any;
138138

139139
if (data.updated) {
140-
if (timezoneCfg) data.updated = timezone(data.updated, timezoneCfg) as any;
140+
if (timezone) data.updated = adjustDateForTimezone(data.updated, timezone) as any;
141141
} else if (updated_option === 'date') {
142142
data.updated = data.date;
143143
} else if (updated_option === 'empty') {

test/scripts/models/moment.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,6 @@ describe('SchemaTypeMoment', () => {
1818
moment.isMoment(type.cast()).should.be.true;
1919
});
2020

21-
it('cast() - language', () => {
22-
const lang = 'zh-tw';
23-
const format = 'LLLL';
24-
const type = new SchemaTypeMoment('test', {language: lang});
25-
const now = Date.now();
26-
27-
type.cast(now).format(format).should.eql(moment(now).locale(lang).format(format));
28-
});
29-
30-
it('cast() - timezone', () => {
31-
const timezone = 'Etc/UTC';
32-
const format = 'LLLL';
33-
const type = new SchemaTypeMoment('test', {timezone});
34-
const now = Date.now();
35-
36-
type.cast(now).format(format).should.eql(moment(now).tz(timezone).format(format));
37-
});
38-
3921
function shouldThrowError(value) {
4022
should.throw(
4123
() => type.validate(value),

test/scripts/processors/common.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import moment from 'moment';
2-
import { isTmpFile, isHiddenFile, toDate, timezone, isMatch } from '../../../lib/plugins/processor/common';
2+
import { isTmpFile, isHiddenFile, toDate, adjustDateForTimezone, isMatch } from '../../../lib/plugins/processor/common';
33
import chai from 'chai';
44
const should = chai.should();
55

@@ -33,16 +33,16 @@ describe('common', () => {
3333

3434
it('timezone() - date', () => {
3535
const d = new Date(Date.UTC(1972, 2, 29, 0, 0, 0));
36-
const d_timezone_UTC = timezone(d, 'UTC').getTime();
37-
(timezone(d, 'Asia/Shanghai').getTime() - d_timezone_UTC).should.eql(-8 * 3600 * 1000);
38-
(timezone(d, 'Asia/Bangkok').getTime() - d_timezone_UTC).should.eql(-7 * 3600 * 1000);
39-
(timezone(d, 'America/Los_Angeles').getTime() - d_timezone_UTC).should.eql(8 * 3600 * 1000);
36+
const d_timezone_UTC = adjustDateForTimezone(d, 'UTC').getTime();
37+
(adjustDateForTimezone(d, 'Asia/Shanghai').getTime() - d_timezone_UTC).should.eql(-8 * 3600 * 1000);
38+
(adjustDateForTimezone(d, 'Asia/Bangkok').getTime() - d_timezone_UTC).should.eql(-7 * 3600 * 1000);
39+
(adjustDateForTimezone(d, 'America/Los_Angeles').getTime() - d_timezone_UTC).should.eql(8 * 3600 * 1000);
4040
});
4141

4242
it('timezone() - moment', () => {
4343
const d = moment(new Date(Date.UTC(1972, 2, 29, 0, 0, 0)));
44-
const d_timezone_UTC = timezone(d, 'UTC').getTime();
45-
(timezone(d, 'Europe/Moscow').getTime() - d_timezone_UTC).should.eql(-3 * 3600 * 1000);
44+
const d_timezone_UTC = adjustDateForTimezone(d, 'UTC').getTime();
45+
(adjustDateForTimezone(d, 'Europe/Moscow').getTime() - d_timezone_UTC).should.eql(-3 * 3600 * 1000);
4646
});
4747

4848
it('isMatch() - string', () => {

0 commit comments

Comments
 (0)