Skip to content

Instantly share code, notes, and snippets.

@jojosati
Last active September 9, 2023 04:03
Show Gist options
  • Save jojosati/3d98fb5514a9cb0f6e489e9f7e5d8199 to your computer and use it in GitHub Desktop.
Save jojosati/3d98fb5514a9cb0f6e489e9f7e5d8199 to your computer and use it in GitHub Desktop.
$now - Thai/Eng date expression translator
// jsat66@gmail.com 2014-8-3
// for all people with love, use at your own risks.
/*
// $now object initial with current date
$now
// following method return self instance
.adj({d: d, m: m, y: y})
.adj(value, ['day', 'week', 'month', 'quater', 'year'])
.set({d: d, m: m, y: y})
.set(value, ['day', 'month', 'year']) // default is day
.clone()
// following method return $period object
.period(['day','week','month','quater','year']) // with period keyword
.period({d: d, m: m, y: y}[, {d: d, m: m, y: m}]) // with custom adj begin [, end]
.toString() // return ISO date YYYY-MM-DD
.valueOf() // return timestamp (gmtime)
// $period object
$period({ from: $now, to: $now} or $now or null[, $now or null])
.begin([$now or null])
.end([$now or null])
.toString() // return json string [begin,end] ["YYYY-MM-DD","YYYY-MM-DD"]
.valueOf() //
// derivative function
$today() // $now.clone()
$this(['day','week','month','quater','year']) // $today().period(%1)
$last([n=1,] ['day','week','month','quater','year']) // $today().adj(-n, %2).period(%2)
$next([n=1,] ['day','week','month','quater','year']) // $today().adj(n, %2).period(%2)
$tomorrow() // $today().adj(1)
$yesterday() // $today().adj(-1)
$begin(['week','month','quater','year']) // $this(%1).begin()
$end(['week','month','quater','year']) // $this(%1).end()
example:
$period($last(2, 'month').begin(), $today()) // 2 months ago -> today
$until($last(2, 'year').end()) // all from start -> end of last 2 year.
*/
(function () {
' use strict'
// helper
function extend(a, b) {
for (var i in b) {
if (b.hasOwnProperty(i)) {
a[i] = b[i];
}
}
return a;
}
function isNumber(n) {
return (typeof n === 'number')
}
function isArray(obj) {
return (Object.prototype.toString.call(obj) === '[object Array]')
}
function isString(s) {
return (typeof s === 'string')
}
function isNull(o) {
return (o === null)
}
function isUndefined(v) {
return (typeof v === 'undefined')
}
function isObject(o) {
return (o !== null && typeof o === 'object')
}
function isDate(d) {
return (Object.prototype.toString.call(d) === '[object Date]')
}
function noop() {
/* no op */
}
function zip(keys, vals) {
var obj = {}
for (var i=0; i < Math.min(keys.length, vals.length); i++)
obj[keys[i]] = vals[i]
return obj
}
/*
return list of [{d:n, m:n, y:y}, {...}]
accept:
- sequence of numbers for d, m, y
- array of [d, m, y]
- mapping of {d: n, m: n, y: n}
- Date instance
*/
function parse_dmy(args) {
var kargs = ['d', 'm', 'y']
var tfn = {
d: function(x) { return { d: x} }
, w: function(x) { return { d: x*7 } }
, m: function(x) { return { m: x } }
, q: function(x) { return { m: x*3 } }
, y: function(x) { return { y: x } }
}
var outlist = []
var _ka = [].slice.apply(kargs)
var pname
var xflag
// make a copy
args = [].slice.apply(args)
while (args.length) {
var v = args.shift()
if (isNull(v) || (v instanceof At)) {
outlist.push(v)
continue
}
if (isString(v)) {
// period name without number, assume to 1
if (v.match(/^(day|week|month|quarter|year)$/i)) {
outlist.push({adj: tfn[v[0]](1)})
pname = v[0]
continue
}
if (v.match(/at|adj/i)) {
xflag = v
continue
}
var d = new Date(v)
if (isDate(d))
v = d
}
if (isDate(v)) {
outlist.push(v)
continue
}
if (isArray(v)) {
outlist.push(zip(kargs, v))
continue
}
if (!isNaN(v)) {
// number element
var name, fn
v = (+v)
name = args[0]
if (isNaN(name) && isString(name)
&& name.match(/^(day|week|month|quarter|year)s?$/i)) {
// number + string as period name
v = {adj: tfn[name[0]](v) }
args.shift()
pname = name[0]
name = null
}
else {
if (pname) {
v = {at: tfn[pname](v) }
pname = name = null
}
else
name = _ka.shift() // without period name, assume sequence of d, m, y
}
if (name && v) {
v = (tfn[name] || noop)(v)
if (xflag) {
xv = {}
xv[xflag] = v
v = xv
}
}
}
if (isObject(v))
outlist.push(v)
}
return outlist
}
function daysInMonth(y, m) {
var d = [31,28,31,30,31,30,31,31,30,31,30,31][m-1]
if (d==28 && (y % 4)==0)
d += 1
return d
}
// prototype object
function At(/* date */) {
// initialize
this.value = {d: 0, m: 0, y: 0, weekday: 1}
this.set.apply(this, arguments.length ? arguments : [new Date()])
}
At.prototype = {
normalize: function(){
if (!(this.value.d || this.value.m || this.value.y))
return this
var self = this
function _norm_month() {
while (self.value.m > 12) {
self.value.m -= 12
self.value.y += 1
}
while (self.value.m < 1) {
self.value.m += 12
self.value.y -= 1
}
}
_norm_month()
while (this.value.d > daysInMonth(this.value.y, this.value.m)) {
this.value.d -= daysInMonth(this.value.y, this.value.m)
this.value.m += 1
_norm_month()
}
while(this.value.d < 1) {
this.value.m -= 1
_norm_month()
this.value.d += daysInMonth(this.value.y, this.value.m)
}
return this
}
/*
.adj(number[,...]) // sequence of d, m, y value
.adj(number, string]) // string: 'day', 'week', 'month', 'quarter', 'year'
.adj(array) // [d,m,y]
.adj(map) // {d:d, m:m, y:y}
*/
,
adj: function() {
function _adj(parm) {
if (isDate(parm) || (parm instanceof At) || (parm instanceof Period)) {
this.set(parm)
return
}
if (!isObject(parm))
return
if (isNull(this.valueOf()))
this.set(new Date())
for (var x in this.value){
if (isNumber(parm[x]))
this.value[x] += parm[x]
}
var adjparm = parm.adj
if (isObject(adjparm)) {
for (var x in this.value){
if (isNumber(adjparm[x]))
this.value[x] += adjparm[x]
}
}
}
var d = this.value.d, m = this.value.m
parse_dmy(arguments).forEach(_adj, this)
if (d == this.value.d && m != this.value.m && d > 28) {
// month change may require adjust end of month
this.value.d = 1
this.normalize()
this.value.d = d
if (d > daysInMonth(this.value.y, this.value.m))
this.value.d = daysInMonth(this.value.y, this.value.m)
return this
}
return this.normalize()
}
/*
.set(Date)
.adj(number[,...]) // sequence of d, m, y value
.adj(number, string]) // string: 'day', 'week', 'month', 'quarter', 'year'
.adj(array) // [d,m,y]
.adj(map) // {d:d, m:m, y:y}
*/
,
set: function() {
function _set(parm) {
if (isNull(parm))
parm = {d: 0, m: 0, y: 0}
if (parm instanceof At)
parm = parm.value
if (parm instanceof Period)
parm = parm.begin().value
if (isDate(parm)) {
parm = {d: parm.getDate(), m: parm.getMonth()+1, y: parm.getFullYear()}
}
if (!isObject(parm))
return;
for (var x in this.value) {
if (isNumber(parm[x])) {
this.value[x] = parm[x]
}
}
var atparm = parm.at
if (isObject(atparm)) {
for (var x in this.value){
if (isNumber(atparm[x])) {
this.value[x] = atparm[x]
}
}
}
}
parse_dmy(arguments).forEach(_set, this)
// resolve when date out of range in certain month
if (this.value.d && this.value.m
&& this.value.d <= 31 && this.value.m <= 12
&& this.value.d > daysInMonth(this.value.y, this.value.m))
this.value.d = daysInMonth(this.value.y, this.value.m)
return this.normalize()
}
,
clone: function() {
return new At().set(this.value)
}
,
toDate: function() {
return new Date(Date.UTC(this.value.y, this.value.m-1, this.value.d))
}
,
parse: function(s) {
return this.set(s)
}
,
valueOf: function() {
return (this.value.d || this.value.m || this.value.y) ? this.toDate().valueOf() : null
}
,
toString: function() {
// return (this.value.d || this.value.m || this.value.y) ? [this.value.y, this.value.m, this.value.d].join('-') : ''
return (this.value.d || this.value.m || this.value.y) ? this.toDate().toISOString().slice(0, 10) : ''
}
,
weekday: function(dayno, ahead) {
var step = ahead?1 :-1
dayno = (dayno || 0) % 7
for (;dayno != this.toDate().getDay(); this.adj(step)) ;
return this
}
,
beginOf: function() {
for (var arg in arguments) {
arg = arguments[arg]
if (isDate(arg) || arg instanceof At || arg instanceof Period) {
this.set(arg)
continue
}
if (!isString(arg))
continue
if (arg.match(/^weeks?$/i)) {
var wd = this.toDate().getDay() - (this.value.weekday || 0)
if (wd) {
if (wd < 0)
wd += 7
this.adj(-wd)
}
continue
}
if (arg.match(/^months?$/i)) {
this.value.d = 1
continue
}
if (arg.match(/^quarters?$/i)) {
this.value.d = 1
this.value.m -= ((this.value.m -1) % 3)
continue
}
if (arg.match(/^years?$/i)) {
this.value.d = this.value.m = 1
continue
}
var d = Date.parse(arg)
if (!isNaN(d)) {
this.set(new Date(d))
continue
}
}
this.set.apply(this,arguments).toString()
return this
}
,
endOf: function() {
this.beginOf.apply(this, arguments)
if (arguments.length) {
var that = +this
this.adj.apply(this, arguments)
if (+this > that)
this.adj(-1)
}
}
/*
.period(adj) // with period keyword ['day','week','month','quarter','year']
*/
,
slice: function() {
var args = []
args.push.apply(args,arguments)
var u = args[args.length -1]
if (u && !isNumber(u)) {
var preserve
// note:
// for full declare _parse_dmy look nearby number of period unit
// 'day', 'month', 'year'
// for short declare number with no period unit treated as sequence of d, m, y
if (u.match(/^years?$/i))
// yes! must be ..., 'year', y, d, m for mix full & short
preserve = [this.value.y, this.value.d, this.value.m]
else if (u.match(/^months?$/i))
preserve = [this.value.m, this.value.d]
if (preserve)
args.push.apply(args, preserve)
}
return this.period.apply(this, args)
}
,
period: function() {
var begin = this.clone()
var args = arguments
if (!args.length)
return new Period(begin, begin)
if (args.length==1 && isNull(args[0]))
return new Period(begin, null)
args = [].concat.apply(['at'],args)
begin.set.apply(begin, args)
if (+begin)
begin.beginOf.apply(begin, args)
var end = begin.clone()
if (!+end) {
end.set(new Date())
end.beginOf.apply(end, args)
}
//end.adj.apply(end, arguments)
end.endOf.apply(end, args)
if (+begin > +end)
begin.adj(-1)
return new Period(begin, end)
}
,
due: function() {
var args = []
var op
args.push.apply(args, arguments)
if (!isNumber(args[0])) {
if (/^\-?\d+[\-\+\~]$/.test(args[0])) {
op = args[0].slice(-1)
args[0] = +(args[0].slice(0, -1))
}
else if (!isNaN(args[0])) {
args[0] = +(args[0])
}
}
this.adj.apply(this, args)
if (op) {
if (op == '+' || (op == '~' && this.value.d > 15)) {
this.set(this.period('month').end())
}
else if (op == '-' || (op == '~' && now.value.d <= 15)) {
this.set(this.period('month').begin())
}
}
return this
}
}
// prototype object
function Period(begin, end) {
if (isUndefined(begin))
begin = null
if (isUndefined(end))
end = begin
if (begin instanceof Period)
begin = begin.begin()
if (end instanceof Period)
end = end.end()
this.value = {
begin: new At(begin),
end: new At(end)
}
this.normalize()
}
Period.prototype = {
normalize: function(){
if (+this.value.begin && +this.value.end && this.value.begin > this.value.end) {
// swap value <-> till
var x = this.value.begin
this.value.begin = this.value.end
this.value.end = x
}
return this
}
,
bound: function(side) {
if (side) {
var now = this.value[side]
if (arguments.length > 1) {
var args = [].slice.call(arguments, 1)
var m = 'set'
if (args[0] == 'adj' || args[0] == 'due') {
m = args.shift()
}
now[m].apply(now, args)
}
return now
}
}
/*
get/set begin property, return At
*/
,
begin: function() {
return this.bound.apply(this, [].concat.apply(['begin'],arguments))
}
/*
get/set end property, return At
*/
,
end: function() {
return this.bound.apply(this, [].concat.apply(['end'],arguments))
}
,
slice: function() {
if (!arguments.length)
return new Period(this, this)
return At.prototype.slice.apply(this.begin(), arguments)
}
,
eslice: function() {
if (!arguments.length)
return new Period(this, this)
return At.prototype.slice.apply(this.end().adj(1), arguments)
}
,
due: function() {
this.begin.apply(this, [].concat.apply(['due'],arguments))
this.end.apply(this, [].concat.apply(['due'],arguments))
return this
}
,
period: function() {
if (!arguments.length)
return new Period(this, this)
return At.prototype.period.apply(this.begin(), arguments)
}
/*
get/set begin property, return Period
*/
,
from: function(obj) {
var args = arguments
if (!args.length)
args = [null]
if (obj instanceof Period) {
if (obj.value.begin > this.value.end) {
this.till.apply(this, args)
return this
}
obj = obj.begin()
}
this.begin.apply(this, args)
this.normalize()
return this
}
/*
get/set end property, return Period
*/
,
till: function(obj) {
var args = arguments
if (!args.length)
args = [null]
if (obj instanceof Period) {
if (obj.value.end < this.value.begin) {
this.from.apply(this, args)
return this
}
obj = obj.end()
}
this.end.apply(this, args)
this.normalize()
return this
}
,toString: function() {
return [this.value.begin.toString(), this.value.end.toString()].join(' - ')
}
,valueOf: function() {
return this.value.valueOf()
}
}
/*
$Now : export scope & derivative
*/
function $Now(/* nowbase */) {
var reserved_words = ['day', 'week', 'month', 'quarter', 'year','days', 'weeks', 'months', 'quarters', 'years', 'adj']
var args = arguments
if (args.length==1 && !args[0])
args = []
this.nowbase = this.$at.apply(this, args)
this.scope = {}
for (var n in reserved_words) {
n = reserved_words[n]
this.scope[n] = n
}
}
$Now.prototype = {
$at: function() { return new (At.bind.apply(At, [].concat.apply([At],arguments))) }
,
$today: function() { return this.nowbase.clone() }
,
$yesterday: function() { return this.$today().adj(-1) }
,
$tomorrow: function() { return this.$today().adj(1) }
,
$this: function() {
var args = []
var now = this.$today()
args.push.apply(args,arguments)
var prd = now.period.apply(now, args)
if (isNumber(args[0]) && args[0] < 0)
return now.period.apply(now, args.slice(1)).from(prd)
return prd
}
,
$due: function(/* prd */) {
var args = []
var now = this.$today()
args.push.apply(args,arguments)
return now.due.apply(now, args).period()
}
,
$next: function(/* prd */) {
var args = []
args.push.apply(args,arguments)
if (!isNumber(args[0]))
args.unshift(1)
var now = this.$today()
now.adj.apply(now, args)
return now.period.apply(now, args.slice(1))
}
,
$last: function(/* prd */) {
var args = []
args.push.apply(args,arguments)
var passed = (!isNumber(args[0]) && args.length > 1)
if (!isNumber(args[0]))
args.unshift(1)
args[0] = -args[0]
var prd = this.$next.apply(this, args)
if (passed) {
var now = this.$today()
var begin = prd.value.begin, end = prd.value.end
var step = [-1, 'month']
if (begin.toString() != end.toString())
step[1] = 'year'
while (end >= now) {
begin.adj.apply(begin, step)
end.adj.apply(end, step)
}
}
return prd
}
,
$recent: function(/* prd */) {
var args = []
args.push.apply(args,arguments)
if (!isNumber(args[0]))
args.unshift(0)
if (args[0] < 0)
args[0] = 0
var now = +this.$today()
//var unit = (begin.toString() != end.toString())? 'year' : 'month'
var prd
while (true) {
prd = this.$last.apply(this, args)
if (+prd.value.begin <= now)
break
args[0] += 1
}
return prd
}
,
$since: function(at) {
if (at instanceof Period)
at = at.begin()
return new Period (at, null)
}
,
$until: function(at) {
if (at instanceof Period)
at = at.end()
return new Period (null, at)
}
,
$before: function(at) {
if (at instanceof Period)
at = at.begin()
return new Period (null, at.clone().adj(-1))
}
,
$after: function(at) {
if (at instanceof Period)
at = at.end()
return new Period (at.clone().adj(+1), null)
}
,
parse: function (expr ,scope) {
with (this) {
with (this.scope) {
if (scope) {
with (scope) {
return eval(expr)
}
}
else {
return eval(expr)
}
}
}
}
}
var translator = { }
translator.BE = {
century: 2500,
short: [40, 99],
full: [2400, 2599],
}
translator.en = {
prepare: function (expr, fiscalfn) {
expr = expr.replace(/^\s*(\d+)\s*(\-)\s+(\d+)(\/\d+(?:\/\d+)?|\s+\S.*?)\s*$/,'$1$4 $2 $3$4')
var _parsefiscal = function (_expr) {
// 2=fstart, 3=numyear, 4=last/this, 5=last/this, 6=num years
var fregex = /(\s|^)(?:((?:\~?\d*)?\/\d+)\s+)?(?:(\-?\d+)\s*(?:\s*(last|this|next)?\s+)|(?:\s*(last|this|next)?\s+)?(\-?\d+)?\s*)fiscal(?:\s*year)?/i
var fiscal = _expr.match(fregex)
if (fiscal) {
var fstart = fiscal[2] || ''
var prefix = fiscal[4] || fiscal[5]
var numyr = fiscal[3] || fiscal[6]
var fmonths = ((prefix=='next')? 12 : -12) * +(numyr || 1)
var m12 = prefix=='last' || prefix=='next'
if (!m12 || prefix=='next')
fmonths += 12
if (fstart[0] == '/')
fstart = fstart.slice(1)
var fexp = fstart
if (!fstart) {
fexp = '~1/' + (fiscalfn? fiscalfn() : '1')
}
else if (!isNaN(fstart) && (+fstart >= 1 && +fstart <= 12)) {
fexp = '~1/' + +(fstart)
}
else if (!isNaN(fstart)) {
var m = +fstart
if (m > 12) {
var fyr = m
if (fyr >= 2400 && fyr <= 2599)
fyr -= 543
m = +((fiscalfn && fiscalfn(fyr)) || 1)
}
fexp = '1/' + m
if (fyr)
fexp += '/' + fyr
}
if (m > 8)
fmonths -= 12
if (fmonths)
fexp += ' slice ' + fmonths + ' months '
if (!m12 || (fmonths != 12 && fmonths != -12)) {
var m = (fmonths > 0)? -12 : 12
if (!m12) {
m = -fmonths + 12
}
if (fmonths <= 0)
fexp += ' slice ' + m + ' months '
else
fexp += ' eslice ' + m + ' months '
}
fexp = (fiscal[1] || '') + fexp
_expr = _parsefiscal(_expr.replace(fregex, fexp))
}
return _expr
}
expr = _parsefiscal(expr)
// shortcut d. => date of last month
if (/^\d{1,2}\.$/.test(expr)) {
var a1 = expr.slice(0, -1)
if (+a1>= 1 && +a1 <= 31) {
var m1 = new Date()
m1.setMonth(m1.getMonth() - 1)
expr = a1 + '/' + (m1.getMonth() + 1) + '/' + m1.getFullYear()
}
}
// shortcut d.m, d.mm, dd.m, dd.mm
if (/^\d{1,2}\.\d{1,2}\.?$/.test(expr)) {
var dm = expr.split('.')
if (+dm[0]>= 1 && +dm[0] <= 31 && +dm[1] >= 1 && +dm[1] <= 12) {
var m1 = new Date()
var m = m1.getMonth() + 1
var y = m1.getFullYear()
expr = dm[0] + '/' + dm[1] + '/' + ((m >= +dm[1])? y : y-1)
}
}
if (/^[/]\d{4}$/.test(expr))
expr = expr.slice(1)
// yyyy/mm => mm/yyyy
expr = expr.replace(/^\s*(\d{4})[\/\-](\d{1,2})\s*$/,'$2/$1')
//expr = expr.replace(/^\s*(\d{1,2})(\s*\-\s*)(\d{1,2})(\/\d+(?:\/\d+)?)\s*$/,'$1$4 $2 $3$4')
// d - d/m
expr = expr.replace(/^\s*(\d+)(\s*\-\s*)(\d+)(\/\d+(?:\/\d+)?)\s*$/,'$1$4 $2 $3$4')
expr = expr.replace(/^\s*(\d+(?:\/\d+)?\/)(\d+)(\s*\-\s*)(\d+)\s*$/,'$1$2 $3 $1$4')
expr = expr.replace(/^\s*([^\-\s]+)([\-])([^\-\s]+)\s*$/,'$1 - $3')
expr = expr.replace(/^\s*([^\-\s]+)([\-])\s*$/,'$1 -')
expr = expr.replace(/^\s*([\-])([^\-\s\d]+|\d{4}|\d+\/[\d\/]+)\s*$/,'- $2')
expr = expr.replace(/^\s*\+(\d+[\+\-\~]?)\s*$/, 'due $1 day')
expr = expr.replace(/((?:^|\s*)?[\-\+]?\d+)d(?=\s*|$)/gi, '$1 day')
expr = expr.replace(/((?:^|\s*)?[\-\+]?\d+)w(?=\s*|$)/gi, '$1 week')
expr = expr.replace(/((?:^|\s*)?[\-\+]?\d+)m(?=\s*|$)/gi, '$1 month')
expr = expr.replace(/((?:^|\s*)?[\-\+]?\d+)q(?=\s*|$)/gi, '$1 quater')
expr = expr.replace(/((?:^|\s*)?[\-\+]?\d+)y(?=\s*|$)/gi, '$1 year')
expr = expr.replace(/^\s*\+(\d+[\+\-\~]?)\s*(day|week|month|quater|year)\s*$/i, 'due $1 $2')
//expr = expr.replace(/^\s*(\d{4})\s*$/,'year $1')
//expr = expr.replace(/^\s*(\d{1,2})[\/\-](\d{4})$/,'year $2 period month $1')
//expr = expr.replace(/^\s*(\d{4})[\/\-\,](\d{1,2})$/,'year $1 period month $2')
expr = expr.replace(/^\s*(\d{1,2})[\/\-]$/, '$1')
var xs = expr.split(/(\d+(?:\/\d*){1,2})/)
// try to fix short year
for (var i=1; i < xs.length; i += 2) {
var xn = xs[i].split('/')
var n = xn.pop()
if (n.length >= 4)
continue;
var nval = +n
if (nval >= 100)
continue;
// maybe d/m
if (xn.length < 2 && nval >= 1 && nval <= 12)
continue;
if (nval < translator.BE.short[0])
nval += 2000
xn.push('' + nval)
xs[i] = xn.join('/')
}
expr = xs.join('')
return expr
},
mapping : function (_phase) {
var maps = [
['$this() ' , /(?:\s|^)today\s*/gi],
['$next() ' , /(?:\s|^)tomorrow\s*/gi],
['$last() ' , /(?:\s|^)yesterday\s*/gi],
['$this($this(week).begin().weekday(0,1)) ' , /(?:\s|^)this\s+[Ss]un(?:day)?(?:\s|$)/gi],
['$this($this(week).begin().weekday(1,1)) ' , /(?:\s|^)this\s+[Mm]on(?:day)?(?:\s|$)/gi],
['$this($this(week).begin().weekday(2,1)) ' , /(?:\s|^)this\s+[Tt]ue(?:sday)?(?:\s|$)/gi],
['$this($this(week).begin().weekday(3,1)) ' , /(?:\s|^)this\s+[Ww]ed(?:nesday)?(?:\s|$)/gi],
['$this($this(week).begin().weekday(4,1)) ' , /(?:\s|^)this\s+[Tt]hu(?:rsday)?(?:\s|$)/gi],
['$this($this(week).begin().weekday(5,1)) ' , /(?:\s|^)this\s+[Ff]ri(?:day)?(?:\s|$)/gi],
['$this($this(week).begin().weekday(6,1)) ' , /(?:\s|^)this\s+[Ss]at(?:urday)?(?:\s|$)/gi],
['$this($tomorrow().weekday(0,1)) ' , /(?:\s|^)incoming\s+[Ss]un(?:day)?(?:\s|$)/gi],
['$this($tomorrow().weekday(1,1)) ' , /(?:\s|^)incoming\s+[Mm]on(?:day)?(?:\s|$)/gi],
['$this($tomorrow().weekday(2,1)) ' , /(?:\s|^)incoming\s+[Tt]ue(?:sday)?(?:\s|$)/gi],
['$this($tomorrow().weekday(3,1)) ' , /(?:\s|^)incoming\s+[Ww]ed(?:nesday)?(?:\s|$)/gi],
['$this($tomorrow().weekday(4,1)) ' , /(?:\s|^)incoming\s+[Tt]hu(?:rsday)?(?:\s|$)/gi],
['$this($tomorrow().weekday(5,1)) ' , /(?:\s|^)incoming\s+[Ff]ri(?:day)?(?:\s|$)/gi],
['$this($tomorrow().weekday(6,1)) ' , /(?:\s|^)incoming\s+[Ss]at(?:urday)?(?:\s|$)/gi],
['$this($today().weekday(0)) ' , /^\s*[Ss]un(?:day)?\s*$/i],
['$this($today().weekday(1)) ' , /^\s*[Mm]on(?:day)?\s*$/i],
['$this($today().weekday(2)) ' , /^\s*[Tt]ue(?:sday)?\s*$/i],
['$this($today().weekday(3)) ' , /^\s*[Ww]ed(?:nesday)?\s*$/i],
['$this($today().weekday(4)) ' , /^\s*[Tt]hu(?:rsday)?\s*$/i],
['$this($today().weekday(5)) ' , /^\s*[Ff]ri(?:day)?\s*$/i],
['$this($today().weekday(6)) ' , /^\s*[Ss]at(?:urday)?\s*$/i],
['$this($yesterday().weekday(0)) ' , /^\s*last\s+[Ss]un(?:day)?\s*$/i],
['$this($yesterday().weekday(1)) ' , /^\s*last\s+[Mm]on(?:day)?\s*$/i],
['$this($yesterday().weekday(2)) ' , /^\s*last\s+[Tt]ue(?:sday)?\s*$/i],
['$this($yesterday().weekday(3)) ' , /^\s*last\s+[Ww]ed(?:nesday)?\s*$/i],
['$this($yesterday().weekday(4)) ' , /^\s*last\s+[Tt]hu(?:rsday)?\s*$/i],
['$this($yesterday().weekday(5)) ' , /^\s*last\s+[Ff]ri(?:day)?\s*$/i],
['$this($yesterday().weekday(6)) ' , /^\s*last\s+[Ss]at(?:urday)?\s*$/i],
['$this(year,$1).period(month,1)', /^\s*jan(?:uary)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,2)', /^\s*feb(?:ruary)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,3)', /^\s*mar(?:ch)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,4)', /^\s*apr(?:il)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,5)', /^\s*may\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,6)', /^\s*jun(?:e)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,7)', /^\s*jul(?:y)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,8)', /^\s*aug(?:ust)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,9)', /^\s*sep(?:tember)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,10)', /^\s*oct(?:ober)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,11)', /^\s*nov(?:november)?\s+(\d{4})\s*$/i],
['$this(year,$1).period(month,12)', /^\s*dec(?:ember)?\s+(\d{4})\s*$/i],
['$last(month,1)', /^\s*last\s+jan(?:uary)?\s*$/i],
['$last(month,2)', /^\s*last\s+feb(?:ruary)?\s*$/i],
['$last(month,3)', /^\s*last\s+mar(?:ch)?\s*$/i],
['$last(month,4)', /^\s*last\s+apr(?:il)?\s*$/i],
['$last(month,5)', /^\s*last\s+may\s*$/i],
['$last(month,6)', /^\s*last\s+jun(?:e)?\s*$/i],
['$last(month,7)', /^\s*last\s+jul(?:y)?\s*$/i],
['$last(month,8)', /^\s*last\s+aug(?:ust)?\s*$/i],
['$last(month,9)', /^\s*last\s+sep(?:tember)?\s*$/i],
['$last(month,10)', /^\s*last\s+oct(?:ober)?\s*$/i],
['$last(month,11)', /^\s*last\s+nov(?:november)?\s*$/i],
['$last(month,12)', /^\s*last\s+dec(?:ember)?\s*$/i],
['month,1', /^\s*this\s+jan(?:uary)?\s*$/i],
['month,2', /^\s*this\s+feb(?:ruary)?\s*$/i],
['month,3', /^\s*this\s+mar(?:ch)?\s*$/i],
['month,4', /^\s*this\s+apr(?:il)?\s*$/i],
['month,5', /^\s*this\s+may\s*$/i],
['month,6', /^\s*this\s+jun(?:e)?\s*$/i],
['month,7', /^\s*this\s+jul(?:y)?\s*$/i],
['month,8', /^\s*this\s+aug(?:ust)?\s*$/i],
['month,9', /^\s*this\s+sep(?:tember)?\s*$/i],
['month,10', /^\s*this\s+oct(?:ober)?\s*$/i],
['month,11', /^\s*this\s+nov(?:november)?\s*$/i],
['month,12', /^\s*this\s+dec(?:ember)?\s*$/i],
['$recent(month,1)', /^\s*jan(?:uary)?\s*$/i],
['$recent(month,2)', /^\s*feb(?:ruary)?\s*$/i],
['$recent(month,3)', /^\s*mar(?:ch)?\s*$/i],
['$recent(month,4)', /^\s*apr(?:il)?\s*$/i],
['$recent(month,5)', /^\s*may\s*$/i],
['$recent(month,6)', /^\s*jun(?:e)?\s*$/i],
['$recent(month,7)', /^\s*jul(?:y)?\s*$/i],
['$recent(month,8)', /^\s*aug(?:ust)?\s*$/i],
['$recent(month,9)', /^\s*sep(?:tember)?\s*$/i],
['$recent(month,10)', /^\s*oct(?:ober)?\s*$/i],
['$recent(month,11)', /^\s*nov(?:november)?\s*$/i],
['$recent(month,12)', /^\s*dec(?:ember)?\s*$/i],
['$2 $1 $3' , /(?:\s|^)(\-?\d+)\s*(due|this|next|last)\s*(day|week|month|quarter|year)s?(?:\s|$)/gi],
['$$$1($2) ' , /(?:\s|^)(due|this|next|last)\s*(day|week|month|quarter|year)(?:\s|$)/gi],
['$$$1(1-($2),$3) ' , /(?:\s|^)(this)\s*(\-?\d+)\s*(day|week|month|quarter|year)s?(?:\s|$)/gi],
['$$$1($2,$3) ' , /(?:\s|^)(due|this|next|last)\s*(\-?\d+)\s*(day|week|month|quarter|year)s?(?:\s|$)/gi],
['$$$1("$2",$3) ' , /(?:\s|^)(due)\s*(\-?\d+[\-\+\~])\s*(day|week|month|quarter|year)s?(?:\s|$)/gi],
['$this($at($3,$2,$1)) ' , /(?:\s|^)(\d{4})(?:[-,\/]|\s+)(\d{1,2})(?:[-,\/]|\s+)(\d{1,2})(?:\s|$)/gi],
['$this($at($1,$2,$3)) ' , /(?:\s|^)(\d{1,2})(?:[-,\/]|\s+)(\d{1,2})(?:[-,\/]|\s+)(\d{4})(?:\s|$)/gi],
['$1,$2 ' , /(?:\s|^)(\-?\d+)\s*(day|week|month|quarter|year)s?(?:\s|$)/gi],
['$1,$2 ' , /(?:\s|^)(day|week|month|quarter|year)\s*(\-?\d+)(?:\s|$)/gi],
['.end().period() ' , /\send(?:\s|$)/gi],
['.begin().period() ' , /\sbegin(?:\s|$)/gi],
['$recent(month,$2,$1)', /\~(\d{1,2})\/(\d{1,2})(?=\s|$)/gi],
['$recent(day,$1)', /\~(\d{1,2})(?=\s|$)/gi],
['$this(year,$2).period(month,$1)', /^\s*(\d{1,2})\/(\d{4})\s*$/],
['$recent(month,$1)', /\~\/(\d{1,2})(?=\s|$)/gi],
['$this(month,$1)', /^\s*\/(\d{1,2})\s*$/],
[',', /\s*\/\s*/gi],
]
//if (Date.parse(phase))
// return phase
maps.forEach(function(m){ _phase = _phase.replace(m[1], m[0]) })
return _phase
}
}
translator.executer = function(locale, fiscal) {
locale = locale || 'en'
return function (expr) {
// expr = expr.toLowerCase()
if (expr.indexOf('$')>=0)
return expr
var trn = translator[locale] || translator.en
expr = trn.prepare.call(translator, expr, fiscal)
expr = expr
.replace(/^\s*\<\s+/,'before ')
.replace(/^\s*\<\=\s+(.*)$/,'$1 - ')
.replace(/^\s*\>\s+/,'after ')
.replace(/^\s*\>\=\s+(.*)$/,'- $1')
//if (expr.match(/^\s*-\s*$/))
// return '$this().from().till()'
var paraphase = function (xs) {
var oparen = 0
var iparen = 0
for (var i=0; i < xs.length; i++) {
var phase = xs[i]
if (i % 2 == 0) {
//phase = trn.prepare.call(translator, phase)
phase = trn.mapping.call(translator, phase)
if (phase.match(/^\s*[^\.\$]/)) {
// YYYY[/-]MM
phase = phase.replace(/^\s*(\d{4})[\/\-](\d{1,2})\s*$/,'$2/$1')
// MM[/-]YYYY
phase = phase.replace(/^\s*(\d{1,2})[\/\-](\d{4})\s*$/,'$this(year,$2).period(month,$1)')
phase = phase.replace(/^\s*(\d{4})\s*$/,'$this(year,$1)')
if (isNaN(phase)) {
if (phase.match(/^\d[\d\s\/]+$/)) {
phase = phase.replace(/[\/]/g,',')
}
if(!phase.match(',')) {
if (phase.match(/^\d[\d\s\-]+$/)) {
phase = phase.replace(/[\-]/g,',')
}
else if (phase.match(/[^\d\s\,\'\""]/)) {
phase = "'" + phase + "'"
}
}
}
}
if (i==0) {
phase = phase.replace(/^\'null\'$/, '.from(null)')
if (phase || !xs[1] || !/before|after|due/.test(xs[1])) {
if (!phase)
phase = '.from()'
if (phase.match(/^\s*[^\.\$]/))
phase = '$this(' + phase + ')'
}
}
else {
while (iparen > 0) {
phase += ')'
iparen -= 1
}
if (i == xs.length-1) {
while(oparen > 0) {
phase += ')'
oparen -= 1
}
}
}
}
else {
if (/before|after/.test(phase) || (/due/.test(phase) && !xs[i-1])) {
phase = '$' + phase + '('
}
else {
phase = '.'
+ phase
.replace(/\-/,'till')
.replace(/for|\//,'period')
+ '('
}
if (/[.](till|from)/.test(phase)) {
while(oparen > 0) {
xs[i-1] += ')'
oparen -= 1
}
}
if (/[.](till|from)/.test(phase)) {
oparen += 1
}
else {
iparen += 1
}
}
xs[i] = phase.trim().replace(/\s+([.$()])/g,'$1')
}
return xs
}
var re1 = /(?:^|\s)(from|till|\-|\/|eslice|slice|due|period|for|before|after)(?:\s|$)/g
expr = paraphase(expr.split(re1)).join('')
expr = expr.replace(/^\s*\./, '$this().')
return expr
}
}
function $now (expr, trnfn, fiscalfn) {
if (trnfn && isString(trnfn))
trnfn = $now.translator.executer(trnfn, fiscalfn)
var _trans = function (_x, _prd) {
if (trnfn)
_x = trnfn(_x)
var _n = new $Now(_prd)
if (_x) {
with(_n) {
with (scope) {
return eval(_x)
}
}
}
return _n
}
if (isArray(expr)) {
var prd
expr.forEach(function(x){
prd = _trans(x, prd)
})
return prd
}
return _trans(expr)
}
$now.translator = translator
/* transcode for thai locale */
translator.th = {
prepare: function (expr, fiscal) {
var xs = expr.split(/([\d]+(?:\/\d+)+)/)
if (xs.length > 1) {
for (var i=1; i < xs.length; i += 2) {
var xn = xs[i].replace(/\d+$/, '')
var n = xs[i].slice(xn.length)
if (n.length==2) {
var x = +n
if (x >= translator.BE.short[0] && x <= translator.BE.short[1]) {
x = translator.BE.century + x
if (xn[0] == '/')
xn = xn.slice(1)
if (x)
xs[i] = '' + xn + x
}
}
}
expr = xs.join('')
}
xs = expr.split(/(\d+)/)
if (xs.length > 1) {
for (var i=1; i < xs.length; i += 2) {
var n = +xs[i]
if (n >= translator.BE.full[0] && n <= translator.BE.full[1]) {
xs[i] = '' + (n - 543)
}
}
expr = xs.join('')
}
expr = expr.replace(/(?:รอบ)?(?:ปี)?บัญชี(?:ก่อน|ที่แล้ว)/g,' last fiscal ')
expr = expr.replace(/(?:รอบ)?(?:ปี)?บัญชี(?:นี้|ล่าสุด)?/g,' fiscal ')
expr = expr
.replace(/(?:วัน)?จันทร์นี้/g,' this Monday ')
.replace(/(?:วัน)?อังคารนี้/g,' this Tuesday ')
.replace(/(?:วัน)?พุธนี้/g,' this Wednesday ')
.replace(/(?:วัน)?พฤหัสนี้/g,' this Thursday ')
.replace(/(?:วัน)?ศุกร์นี้/g,' this Friday ')
.replace(/(?:วัน)?เสาร์นี้/g,' this Saturday ')
.replace(/(?:วัน)?อาทิตย์นี้/g,' this Sunday ')
.replace(/(?:วัน)?จันทร์(?:หน้า|ที่จะถึง)/g,' incoming Monday ')
.replace(/(?:วัน)?อังคาร(?:หน้า|ที่จะถึง)/g,' incoming Tuesday ')
.replace(/(?:วัน)?พุธ(?:หน้า|ที่จะถึง)/g,' incoming Wednesday ')
.replace(/(?:วัน)?พฤหัส(?:หน้า|ที่จะถึง)/g,' incoming Thursday ')
.replace(/(?:วัน)?ศุกร์(?:หน้า|ที่จะถึง)/g,' incoming Friday ')
.replace(/(?:วัน)?เสาร์(?:หน้า|ที่จะถึง)/g,' incoming Saturday ')
.replace(/(?:วัน)?อาทิตย์(?:หน้า|ที่จะถึง)/g,' incoming Sunday ')
.replace(/(?:วัน)?จันทร์(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Monday ')
.replace(/(?:วัน)?อังคาร(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Tuesday ')
.replace(/(?:วัน)?พุธ(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Wednesday ')
.replace(/(?:วัน)?พฤหัส(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Thursday ')
.replace(/(?:วัน)?ศุกร์(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Friday ')
.replace(/(?:วัน)?เสาร์(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Saturday ')
.replace(/(?:วัน)?อาทิตย์(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Sunday ')
.replace(/(?:(?:วัน)?จันทร์|(?:^|\s)จ(?:$|\s))/g,' Monday ')
.replace(/(?:(?:วัน)?อังคาร|(?:^|\s)อ(?:$|\s))/g,' Tuesday ')
.replace(/(?:(?:วัน)?พุธ|(?:^|\s)พ(?:$|\s))/g,' Wednesday ')
.replace(/(?:(?:วัน)?พฤหัส|(?:^|\s)พฤ(?:$|\s))/g,' Thursday ')
.replace(/(?:(?:วัน)?ศุกร์|(?:^|\s)ศ(?:$|\s))/g,' Friday ')
.replace(/(?:(?:วัน)?เสาร์|(?:^|\s)ส(?:$|\s))/g,' Saturday ')
.replace(/(?:(?:วัน)?อาทิตย์|(?:^|\s)อา(?:$|\s))/g,' Sunday ')
.replace(/(?:ม\.ค\.|มกรา(?:คม)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Jan ')
.replace(/(?:ก\.พ\.|กุมภา(?:พันธ์)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Feb ')
.replace(/(?:มี\.ค\.|มีนา(?:คม)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Mar ')
.replace(/(?:เม\.ย\.|เมษา(?:ยน)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Apr ')
.replace(/(?:พ\.ค\.|พฤษภา(?:คม)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last May ')
.replace(/(?:มิ\.ย\.|มิถุนา(?:ยน)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Jun ')
.replace(/(?:ก\.ค\.|กรก[ฏฎ]า(?:คม)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Jul ')
.replace(/(?:ส\.ค\.|สิงหา(?:คม)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Aug ')
.replace(/(?:ก\.ย\.|กันยา(?:ยน)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Sep ')
.replace(/(?:ต\.ค\.|ตุลา(?:คม)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Oct ')
.replace(/(?:พ\.ย\.|พฤศจิกา(?:ยน)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Nov ')
.replace(/(?:ธ\.ค\.|ธันวา(?:คม)?)\s*(?:ก่อน|ที่แล้ว|ที่ผ่านมา)/g,' last Dec ')
.replace(/(?:ม\.ค\.|มกรา(?:คม)?)\s*นี้/g,' this Jan ')
.replace(/(?:ก\.พ\.|กุมภา(?:พันธ์)?)\s*นี้/g,' this Feb ')
.replace(/(?:มี\.ค\.|มีนา(?:คม)?)\s*นี้/g,' this Mar ')
.replace(/(?:เม\.ย\.|เมษา(?:ยน)?)\s*นี้/g,' this Apr ')
.replace(/(?:พ\.ค\.|พฤษภา(?:คม)?)\s*นี้/g,' this May ')
.replace(/(?:มิ\.ย\.|มิถุนา(?:ยน)?)\s*นี้/g,' this Jun ')
.replace(/(?:ก\.ค\.|กรก[ฏฎ]า(?:คม)?)\s*นี้/g,' this Jul ')
.replace(/(?:ส\.ค\.|สิงหา(?:คม)?)\s*นี้/g,' this Aug ')
.replace(/(?:ก\.ย\.|กันยา(?:ยน)?)\s*นี้/g,' this Sep ')
.replace(/(?:ต\.ค\.|ตุลา(?:คม)?)\s*นี้/g,' this Oct ')
.replace(/(?:พ\.ย\.|พฤศจิกา(?:ยน)?)\s*นี้/g,' this Nov ')
.replace(/(?:ธ\.ค\.|ธันวา(?:คม)?)\s*นี้/g,' this Dec ')
.replace(/ม\.ค\.|มกรา(?:คม)?/g,' Jan ')
.replace(/ก\.พ\.|กุมภา(?:พันธ์)?/g,' Feb ')
.replace(/มี\.ค\.|มีนา(?:คม)?/g,' Mar ')
.replace(/เม\.ย\.|เมษา(?:ยน)?/g,' Apr ')
.replace(/พ\.ค\.|พฤษภา(?:คม)?/g,' May ')
.replace(/มิ\.ย\.|มิถุนา(?:ยน)?/g,' Jun ')
.replace(/ก\.ค\.|กรก[ฏฎ]า(?:คม)?/g,' Jul ')
.replace(/ส\.ค\.|สิงหา(?:คม)?/g,' Aug ')
.replace(/ก\.ย\.|กันยา(?:ยน)?/g,' Sep ')
.replace(/ต\.ค\.|ตุลา(?:คม)?/g,' Oct ')
.replace(/พ\.ย\.|พฤศจิกา(?:ยน)?/g,' Nov ')
.replace(/ธ\.ค\.|ธันวา(?:คม)?/g,' Dec ')
.replace(/(.*)ล่าสุด/,'this $1 ')
.replace(/\sก่อน(?:หน้า|ถึง)?|ก่อน(?:หน้า|ถึง)|เก่ากว่า/g,' before ')
.replace(/หลัง(?:จาก)?/g,' after ')
.replace(/ตั้งแต่|จาก/g, ' from ')
.replace(/(?:(?:ไป|จน)?ถึง|ไม่เกิน)/g,' till ')
.replace(/งวด|นาน/g,' period ')
return this.en.prepare(expr, fiscal)
},
mapping: function (phase) {
var maps = [
['$next($1,week).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*สัปดาห์หน้า\s*/g],
['$next($1,month).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*เดือนหน้า\s*/g],
['$next($1,quarter).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*ไตรมาสหน้า\s*/g],
['$next($1,year).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*ปีหน้า\s*/g],
['$next($1,week).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*สัปดาห์หน้า\s*/g],
['$next($1,month).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*เดือนหน้า\s*/g],
['$next($1,quarter).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*ไตรมาสหน้า\s*/g],
['$next($1,year).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*ปีหน้า\s*/g],
['$next(week).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นสัปดาห์หน้า\s*/g],
['$next(month).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นเดือนหน้า\s*/g],
['$next(quarter).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นไตรมาสหน้า\s*/g],
['$next(year).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นปีหน้า\s*/g],
['$next(week).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)สัปดาห์หน้า\s*/g],
['$next(month).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)เดือนหน้า\s*/g],
['$next(quarter).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)ไตรมาสหน้า\s*/g],
['$next(year).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)ปีหน้า\s*/g],
['$last($1,week).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*สัปดาห์(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,month).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*เดือน(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,quarter).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*ไตรมาส(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,year).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*ปี(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,week).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*สัปดาห์(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,month).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*เดือน(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,quarter).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*ไตรมาส(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,year).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*ปี(?:ก่อน|ที่แล้ว)\s*/g],
['$last(week).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นสัปดาห์(?:ก่อน|ที่แล้ว)\s*/g],
['$last(month).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นเดือน(?:ก่อน|ที่แล้ว)\s*/g],
['$last(quarter).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นไตรมาส(?:ก่อน|ที่แล้ว)\s*/g],
['$last(year).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นปี(?:ก่อน|ที่แล้ว)\s*/g],
['$last(week).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)สัปดาห์(?:ก่อน|ที่แล้ว)\s*/g],
['$last(month).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)เดือน(?:ก่อน|ที่แล้ว)\s*/g],
['$last(quarter).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)ไตรมาส(?:ก่อน|ที่แล้ว)\s*/g],
['$last(year).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)ปี(?:ก่อน|ที่แล้ว)\s*/g],
['$this($1,week).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*สัปดาห์นี้\s*/g],
['$this($1,month).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*เดือนนี้\s*/g],
['$this($1,quarter).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*ไตรมาสนี้\s*/g],
['$this($1,year).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้น(?:งวด)?\s*(\-?\d+)\s*ปีนี้\s*/g],
['$this($1,week).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*สัปดาห์นี้\s*/g],
['$this($1,month).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*เดือนนี้\s*/g],
['$this($1,quarter).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*ไตรมาสนี้\s*/g],
['$this($1,year).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)(?:งวด)?\s*(\-?\d+)\s*ปีนี้\s*/g],
['$this(week).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นสัปดาห์(?:นี้)?\s*/g],
['$this(month).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นเดือน(?:นี้)?\s*/g],
['$this(quarter).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นไตรมาส(?:นี้)?\s*/g],
['$this(year).begin().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?ต้นปี(?:นี้)?\s*/g],
['$this(week).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)สัปดาห์(?:นี้)?\s*/g],
['$this(month).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)เดือน(?:นี้)?\s*/g],
['$this(quarter).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)ไตรมาส(?:นี้)?\s*/g],
['$this(year).end().period() ' , /(?:\s|^)(?:วัน|ณ\s+)?(?:ปลาย|สิ้น)ปี(?:นี้)?\s*/g],
['$next($1) ' , /(?:\s|^)(\-?\d+)\s*วันหน้า\s*/g],
['$next($1,week) ' , /(?:\s|^)(\-?\d+)\s*สัปดาห์หน้า\s*/g],
['$next($1,month) ' , /(?:\s|^)(\-?\d+)\s*เดือนหน้า\s*/g],
['$next($1,quarter) ' , /(?:\s|^)(\-?\d+)\s*ไตรมาสหน้า\s*/g],
['$next($1,year) ' , /(?:\s|^)(\-?\d+)\s*ปีหน้า\s*/g],
['$last($1) ' , /(?:\s|^)(\-?\d+)\s*วัน(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,week) ' , /(?:\s|^)(\-?\d+)\s*สัปดาห์(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,month) ' , /(?:\s|^)(\-?\d+)\s*เดือน(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,quarter) ' , /(?:\s|^)(\-?\d+)\s*ไตรมาส(?:ก่อน|ที่แล้ว)\s*/g],
['$last($1,year) ' , /(?:\s|^)(\-?\d+)\s*ปี(?:ก่อน|ที่แล้ว)\s*/g],
['$this(1-($1$2),day) ' , /(?:\s|^)(\-?)(\d+)\s*วันนี้\s*/g],
['$this(1-($1$2),week) ' , /(?:\s|^)(\-?)(\d+)\s*สัปดาห์นี้\s*/g],
['$this(1-($1$2),month) ' , /(?:\s|^)(\-?)(\d+)\s*เดือนนี้\s*/g],
['$this(1-($1$2),quarter) ' , /(?:\s|^)(\-?)(\d+)\s*ไตรมาสนี้\s*/g],
['$this(1-($1$2),year) ' , /(?:\s|^)(\-?)(\d+)\s*ปีนี้\s*/g],
['$next() ' , /(?:\s|^)(?:วันหน้า|(?:วัน)?พรุ่งนี้)\s*/g],
['$next(week) ' , /(?:\s|^)สัปดาห์หน้า\s*/g],
['$next(month) ' , /(?:\s|^)เดือนหน้า\s*/g],
['$next(quarter) ' , /(?:\s|^)ไตรมาสหน้า\s*/g],
['$next(year) ' , /(?:\s|^)ปีหน้า\s*/g],
['$last() ' , /(?:\s|^)(?:วัน(?:ก่อน|ที่แล้ว)|(?:เมื่อ)?วานนี้|เมื่อวาน)\s*/g],
['$last(week) ' , /(?:\s|^)สัปดาห์(?:ก่อน|ที่แล้ว)\s*/g],
['$last(month) ' , /(?:\s|^)เดือน(?:ก่อน|ที่แล้ว)\s*/g],
['$last(quarter) ' , /(?:\s|^)ไตรมาส(?:ก่อน|ที่แล้ว)\s*/g],
['$last(year) ' , /(?:\s|^)ปี(?:ก่อน|ที่แล้ว)\s*/g],
['$this() ' , /(?:\s|^)วันนี้\s*/g],
['$this(week) ' , /(?:\s|^)สัปดาห์นี้\s*/g],
['$this(month) ' , /(?:\s|^)เดือนนี้\s*/g],
['$this(quarter) ' , /(?:\s|^)ไตรมาสนี้\s*/g],
['$this(year) ' , /(?:\s|^)ปีนี้\s*/g],
['$1 day ' , /(?:\s|^)(\-?\d+)\s*วัน(?:\s|$)/g],
['$1 week ' , /(?:\s|^)(\-?\d+)\s*สัปดาห์(?:\s|$)/g],
['$1 month ' , /(?:\s|^)(\-?\d+)\s*เดือน(?:\s|$)/g],
['$1 quarter ' , /(?:\s|^)(\-?\d+)\s*ไตรมาส(?:\s|$)/g],
['$1 year ' , /(?:\s|^)(\-?\d+)\s*ปี(?:\s|$)/g],
['day $1 ' , /(?:\s|^)วัน(?:ที่)?\s*(\-?\d+)(?:\s|$)/g],
['week $1 ' , /(?:\s|^)สัปดาห์(?:ที่)?\s*(\-?\d+)(?:\s|$)/g],
['month $1 ' , /(?:\s|^)เดือน(?:ที่)?\s*(\-?\d+)(?:\s|$)/g],
['quarter $1 ' , /(?:\s|^)ไตรมาส(?:ที่)?\s*(\-?\d+)(?:\s|$)/g],
['year $1 ' , /(?:\s|^)ปี\s*(\-?\d+)(?:\s|$)/g],
['.end().period() ' , /\s(?:วัน|ณ\s+)?ปลายงวด\s*/g],
['.begin().period() ' , /\s(?:วัน|ณ\s+)?ต้นงวด\s*/g],
['null ' , /เริ่ม(?:ต้น)?|สุดท้าย/g],
['next', /อีก/g],
['day' , /วัน/g],
['week' , /สัปดาห์/g],
['month' , /เดือน/g],
['quarter' , /ไตรมาส/g],
['year' , /ปี/g],
]
maps.forEach(function(m){ phase = phase.replace(m[1], m[0]) })
return this.en.mapping(phase)
}
}
/************************************
Exposing
************************************/
// CommonJS module is defined
if (typeof module !== 'undefined' && module.exports) {
module.exports = $now
}
/*global ender:false */
if (typeof ender === 'undefined') {
// here, `this` means `window` in the browser, or `global` on the server
// add `$now` as a global object via a string identifier,
// for Closure Compiler "advanced" mode
this['$now'] = $now
}
/*global define:false */
if (typeof define === "function" && define.amd) {
define("$now", [], function () {
return $now
})
}
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment