| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals |
| 4 |
|
| 5 |
UNDEFINED_DATE_ISO = '0101-01-01T00:00:00+00:00' |
| 6 |
UNDEFINED_DATE = Date(UNDEFINED_DATE_ISO) |
| 7 |
|
| 8 |
|
| 9 |
def is_date_undefined(date): |
| 10 |
dy, uy = date.getUTCFullYear(), UNDEFINED_DATE.getUTCFullYear() |
| 11 |
return dy < uy or ( |
| 12 |
dy is uy and |
| 13 |
date.getUTCMonth() is UNDEFINED_DATE.getUTCMonth() and |
| 14 |
date.getUTCDate() <= UNDEFINED_DATE.getUTCDate() + 1) |
| 15 |
|
| 16 |
# format_date() {{{ |
| 17 |
|
| 18 |
lcdata = { |
| 19 |
'abday': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], |
| 20 |
'abmon': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], |
| 21 |
'day': ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], |
| 22 |
'mon': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], |
| 23 |
} |
| 24 |
|
| 25 |
supports_locales = False |
| 26 |
try: |
| 27 |
Date().toLocaleString('dfkjghdfkgj') |
| 28 |
except RangeError: |
| 29 |
supports_locales = True |
| 30 |
except: |
| 31 |
pass |
| 32 |
|
| 33 |
|
| 34 |
def fmt_date_pat(): |
| 35 |
ans = fmt_date_pat.ans |
| 36 |
if not ans?: |
| 37 |
ans = fmt_date_pat.ans = /(s{1,2})|(m{1,2})|(h{1,2})|(H{1,2})|(zzz)|(z)|(ap)|(AP)|(Ap)|(aP)|(a)|(A)|(t)|(d{1,4}|M{1,4}|(?:yyyy|yy))/g |
| 38 |
return ans |
| 39 |
|
| 40 |
|
| 41 |
def fd_format_hour(dt, ampm, hr, as_utc): |
| 42 |
h = dt.getUTCHours() if as_utc else dt.getHours() |
| 43 |
if ampm: |
| 44 |
h %= 12 |
| 45 |
if h is 0: |
| 46 |
h = 12 |
| 47 |
return h.toString() if hr.length is 1 else str.format('{:02d}', h) |
| 48 |
|
| 49 |
|
| 50 |
def fd_format_hour24(dt, ampm, hr, as_utc): |
| 51 |
return fd_format_hour(dt, False, hr, as_utc) |
| 52 |
|
| 53 |
|
| 54 |
def fd_format_minute(dt, ampm, min, as_utc): |
| 55 |
m = dt.getUTCMinutes() if as_utc else dt.getMinutes() |
| 56 |
return m.toString() if min.length is 1 else str.format('{:02d}', m) |
| 57 |
|
| 58 |
|
| 59 |
def fd_format_second(dt, ampm, min, as_utc): |
| 60 |
s = dt.getUTCSeconds() if as_utc else dt.getSeconds() |
| 61 |
return s.toString() if min.length is 1 else str.format('{:02d}', s) |
| 62 |
|
| 63 |
|
| 64 |
def fd_format_ms(dt, ampm, min, as_utc): |
| 65 |
s = dt.getUTCMilliseconds() if as_utc else dt.getSeconds() |
| 66 |
return s.toString() if min.length is 1 else str.format('{:03d}', s) |
| 67 |
|
| 68 |
|
| 69 |
def fd_format_timezone(dt, ampm, t, as_utc): |
| 70 |
short = dt.toLocaleTimeString() |
| 71 |
full = dt.toLocaleTimeString([], {'timeZoneName': 'long'}) |
| 72 |
idx = full.indexOf(short) |
| 73 |
if idx > -1: |
| 74 |
return full[idx + short.length:].strip() |
| 75 |
return window.Intl.DateTimeFormat().resolvedOptions().timeZone |
| 76 |
|
| 77 |
|
| 78 |
def get_ampm_text(which): |
| 79 |
ans = get_ampm_text[which] |
| 80 |
if not ans: |
| 81 |
try: |
| 82 |
d = Date() |
| 83 |
if which is 'am': |
| 84 |
d.setHours(1, 0, 0, 0) |
| 85 |
else: |
| 86 |
d.setHours(13, 0, 0, 0) |
| 87 |
x = window.Intl.DateTimeFormat('default', {'hour': 'numeric', 'hour12': True}).format(d) |
| 88 |
ans = x.replace(/\d+/g, '').trim() or which.toUpperCase() |
| 89 |
except: |
| 90 |
ans = which.toUpperCase() |
| 91 |
get_ampm_text[which] = ans |
| 92 |
return ans |
| 93 |
|
| 94 |
|
| 95 |
def fd_format_ampm(dt, ampm, ap, as_utc): |
| 96 |
h = dt.getUTCHours() if as_utc else dt.getHours() |
| 97 |
ans = 'am' if h < 12 else 'pm' |
| 98 |
ans = get_ampm_text(ans) |
| 99 |
if ans is 'Ap' or ans is 'aP': |
| 100 |
return ans |
| 101 |
return ans.toLowerCase() if (ap is 'ap' or ap is 'a') else ans.toUpperCase() |
| 102 |
|
| 103 |
|
| 104 |
def fd_format_day(dt, ampm, dy, as_utc): |
| 105 |
d = dt.getUTCDate() if as_utc else dt.getDate() |
| 106 |
if dy.length is 1: |
| 107 |
return d.toString() |
| 108 |
if dy.length is 2: |
| 109 |
return str.format('{:02d}', d) |
| 110 |
if supports_locales: |
| 111 |
options = {'weekday': ('short' if dy.length is 3 else 'long')} |
| 112 |
if as_utc: |
| 113 |
options['timeZone'] = 'UTC' |
| 114 |
return dt.toLocaleString(window.navigator.language, options) |
| 115 |
w = dt.getUTCDay() if as_utc else dt.getDay() |
| 116 |
return lcdata['abday' if dy.length is 3 else 'day'][(w + 1) % 7] |
| 117 |
|
| 118 |
|
| 119 |
def fd_format_month(dt, ampm, mo, as_utc): |
| 120 |
m = dt.getUTCMonth() if as_utc else dt.getMonth() |
| 121 |
if mo.length is 1: |
| 122 |
return (m + 1).toString() |
| 123 |
if mo.length is 2: |
| 124 |
return str.format('{:02d}', m + 1) |
| 125 |
if supports_locales: |
| 126 |
options = {'month': {1: 'numeric', 2: '2-digit', 3: 'short', 4: 'long'}[mo.length] or 'long'} |
| 127 |
if as_utc: |
| 128 |
options['timeZone'] = 'UTC' |
| 129 |
return dt.toLocaleString(window.navigator.language, options) |
| 130 |
return lcdata['abmon' if mo.length is 3 else 'mon'][m] |
| 131 |
|
| 132 |
|
| 133 |
def fd_format_year(dt, ampm, yr, as_utc): |
| 134 |
y = dt.getUTCFullYear() if as_utc else dt.getFullYear() |
| 135 |
if yr.length is 2: |
| 136 |
return str.format('{:02d}', y % 100) |
| 137 |
return str.format('{:04d}', y) |
| 138 |
|
| 139 |
fd_function_index = { |
| 140 |
'd': fd_format_day, |
| 141 |
'M': fd_format_month, |
| 142 |
'y': fd_format_year, |
| 143 |
'h': fd_format_hour, |
| 144 |
'H': fd_format_hour24, |
| 145 |
'm': fd_format_minute, |
| 146 |
's': fd_format_second, |
| 147 |
'a': fd_format_ampm, |
| 148 |
'A': fd_format_ampm, |
| 149 |
'ap': fd_format_ampm, |
| 150 |
'AP': fd_format_ampm, |
| 151 |
'Ap': fd_format_ampm, |
| 152 |
'aP': fd_format_ampm, |
| 153 |
'z': fd_format_ms, |
| 154 |
't': fd_format_timezone, |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
def am_pm_pat(): |
| 159 |
ans = am_pm_pat.ans |
| 160 |
if not ans?: |
| 161 |
ans = am_pm_pat.ans = /(ap)|(AP)|(Ap)|(aP)|(a)|(A)/ |
| 162 |
return ans |
| 163 |
|
| 164 |
|
| 165 |
def as_iso(date): |
| 166 |
tzo = -date.getTimezoneOffset() |
| 167 |
dif = '+' if tzo >= 0 else '-' |
| 168 |
|
| 169 |
def pad(num): |
| 170 |
return ('0' if num < 10 else '') + num |
| 171 |
|
| 172 |
return ( |
| 173 |
date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + |
| 174 |
':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds()) + dif + pad(Math.floor(Math.abs(tzo) / 60)) + |
| 175 |
':' + pad(Math.abs(tzo) % 60) |
| 176 |
) |
| 177 |
|
| 178 |
|
| 179 |
def format_date(date=None, fmt='dd MMM yyyy', as_utc=False): |
| 180 |
fmt = fmt or 'dd MMM yyyy' |
| 181 |
ampm = am_pm_pat().test(fmt) |
| 182 |
if jstype(date) is 'string': |
| 183 |
date = Date(date) |
| 184 |
date = date or Date() |
| 185 |
if is_date_undefined(date): |
| 186 |
return '' |
| 187 |
if fmt is 'iso': |
| 188 |
return as_iso(date) |
| 189 |
return fmt.replace(fmt_date_pat(), def (match): |
| 190 |
if not match: |
| 191 |
return '' |
| 192 |
try: |
| 193 |
return fd_function_index[match[0]](date, ampm, match, as_utc) |
| 194 |
except Exception: |
| 195 |
print(str.format('Failed to format date with format: {} and match: {}', fmt, match)) |
| 196 |
return '' |
| 197 |
) |
| 198 |
# }}} |