Skip to content

Commit

Permalink
Timezone support
Browse files Browse the repository at this point in the history
fixes #33
  • Loading branch information
talkhabi committed Aug 15, 2019
1 parent 66b85d2 commit d234e40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/picker/VuePersianDatetimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ import Btn from './components/Btn.vue'
import CalendarIcon from './components/CalendarIcon.vue'
import TimeIcon from './components/TimeIcon.vue'
import CoreModule from './modules/core'
import moment from 'moment'
export default {
components: { Arrow, Btn, CalendarIcon, TimeIcon },
model: {
Expand Down Expand Up @@ -727,7 +727,16 @@ export default {
* en: { ... }
* }
*/
localeConfig: { type: Object, default: () => ({}) }
localeConfig: { type: Object, default: () => ({}) },
/**
* Timezone configuration
* @type String | Boolean | Function
* @default false
* @example true | false | +03:30 | +04:30
* @version 2.1.0
*/
timezone: { type: [Boolean, String, Function], default: false }
},
data() {
let coreModule = new CoreModule('fa')
Expand Down Expand Up @@ -998,6 +1007,7 @@ export default {
let output = this.output.clone()
let format = this.selfFormat
if (/j\w/.test(format)) output.locale('fa')
this.setTimezone(output, 'out')
return output.format(format)
},
displayValue() {
Expand Down Expand Up @@ -1049,6 +1059,7 @@ export default {
value: { handler: 'updateDates', immediate: true },
min: { handler: 'setMinMax', immediate: true },
max: { handler: 'setMinMax', immediate: true },
timezone: { handler: 'updateDates' },
inline: {
handler(val) {
if (!this.disabled) this.visible = !!val
Expand Down Expand Up @@ -1273,6 +1284,8 @@ export default {
this.date = d.isValid() ? d : this.core.moment()
this.date = this.setTimezone(this.date, 'in')
if (!this.hasStep('t')) this.date.set({ hour: 0, minute: 0, second: 0 })
if (this.isLower(this.date)) {
Expand Down Expand Up @@ -1518,6 +1531,25 @@ export default {
this.date = this.date.clone()
this.selectedDate = this.selectedDate.clone()
this.$forceUpdate()
},
setTimezone(date, mode) {
let tz = this.timezone
let r = mode === 'in' ? 1 : -1
let moment = this.core.moment
if (tz) {
if (typeof tz === 'string') {
let t =
moment()
.utc()
.format('YYYY-MM-DDTHH:mm:ss') + tz
date.add(moment.parseZone(t).utcOffset() * r, 'minutes')
} else if (typeof tz === 'boolean' && tz) {
date.subtract(new Date().getTimezoneOffset() * r, 'minutes')
} else if (typeof tz === 'function') {
date = tz(date, mode, this)
}
}
return date.clone()
}
},
install(Vue, options) {
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ if (process.env.NODE_ENV === 'export') {

module.exports.externals = (module.exports.externals || []).concat([
'vue',
'moment',
'moment-jalaali'
])

Expand Down

0 comments on commit d234e40

Please sign in to comment.