1/*global jhInstance, jhRegex*/ 2 3(function(jh){ 4 'use strict'; 5 6 var $this; 7 8 $this = jh.init(jh, 'date', {}); 9 10 $this.f = function jhDate_f(date, format, options){ 11 12 var ts = $this.tsMs(date) 13 , formats = $this.dateFormats 14 , options = options || null 15 ; 16 17 if( ts === null ){ return null; } 18 19 format = format || 'm/d/Y'; 20 21 if( formats[format] !== undefined ){ format = formats[format]; } 22 23 return typeof format === 'string' ? $this.date(format, ts) : format(ts, options); 24 25 }; 26 27 $this.ts = function jhDate_ts(){ 28 29 var ts = $this.tsMs.apply(this, arguments); 30 31 if( ts !== null ){ ts = Math.floor(ts/1000); } 32 33 return ts; 34 35 }; 36 37 $this.tsMs = function jhDate_ts(date, def){ 38 39 if( arguments.length === 0 ){ date = 'ts'; } 40 41 var ts = def || null 42 , dtype = typeof date 43 ; 44 45 if( date === undefined || date === null || date === '' ){ 46 return ts; 47 } 48 49 if( dtype === 'object' ){ 50 ts = date.getTime(); 51 }else if( dtype === 'string' ){ 52 ts = $this.str2ts(date); 53 }else if( dtype === 'number' ){ 54 ts = date; 55 } 56 57 return ts; 58 59 }; 60 61 $this.change = function jhDate_change(date, change, mode){ 62 63 mode = mode || 'date'; 64 65 var ts = $this.tsMs(date); 66 if( ts === null ){ return null; } 67 68 date = $this.date('H/i/s/m/d/Y', ts).split('/'); 69 70 if( mode === 'date' ){ 71 date[4] = date[4] * 1 + change * 1;//change by days 72 }else if( mode === 'month' ){ 73 date[3] = date[3] * 1 + change * 1;//change by months 74 } 75 76 ts = $this.mkts(date[0], date[1], date[2], date[3], date[4], date[5]); 77 78 return ts; 79 80 }; 81 82 $this.mkts = function jhDate_mkts(hour, minute, second, month, day, year){ 83 84 hour = hour * 1; minute = minute * 1; second = second * 1; 85 month = month * 1; day = day * 1; year = year * 1; 86 87 //hmmmmm, zero year? 88 if( year === 0 ){ return null; } 89 90 month--;//fix, js month starts from 0 91 92 return (new Date(year, month, day, hour, minute, second)).getTime(); 93 94 }; 95 96 $this.str2ts = function jhDate_str2ts(str){ 97 98 if( str === 'now' ){ return $this.roundTs(new Date()); } 99 else if( str === 'ts' ){ return new Date().getTime(); } 100 101 var ts = null 102 , parsers = $this.str2tsParsers 103 , regex 104 ; 105 106 str = str.replace(/[^\d]/g, '/'); 107 108 for(regex in parsers){ 109 if( !parsers.hasOwnProperty(regex) ){ continue; } 110 111 if( jhRegex.match(regex, str) ){ 112 ts = parsers[regex](str); 113 break; 114 } 115 116 } 117 118 return ts; 119 120 }; 121 122 $this.roundTs = function jhDate_roundTs(date, dir){ 123 124 dir = dir || 'floor'; 125 126 var ts = $this.tsMs(date); 127 if( ts === null ){ return null; } 128 129 if( typeof dir === 'array' ){ 130 var 131 i, i_max, dir_i //for+ 132 ; 133 for(i = 0, i_max = dir.length; i < i_max; i++){ 134 ts = roundTs(ts, dir[i]); 135 } 136 return ts; 137 } 138 139 date = $this.date('m/d/Y/t', ts).split('/'); 140 141 if( dir === 'monthCeil' ){ 142 date[1] = date[3]; 143 dir = 'ceil'; 144 }else if( dir == 'monthFloor' ){ 145 date[1] = 1; 146 dir = 'floor'; 147 } 148 149 if( dir === 'floor'){ 150 ts = $this.mkts(0, 0, 0, date[0], date[1], date[2]); 151 }else if( dir === 'ceil' ){ 152 ts = $this.mkts(23, 59, 59, date[0], date[1], date[2]); 153 } 154 155 return ts; 156 157 }; 158 159 $this.compare = function jhDate_compare(date1, expr, date2, format){ 160 161 format = format || 'Date'; 162 163 var ts1 = $this.tsMs(date1) 164 , ts2 = $this.tsMs(date2) 165 , eq 166 ; 167 168 if( ts1 === null || ts2 === null ){ return null; } 169 if( $this.f(ts1, format) === $this.f(ts2, format) ){ 170 eq = 0; 171 }else{ 172 eq = ts1 > ts2 ? 1 : -1; 173 } 174 175 if( expr === null ){ 176 }else if( expr === '<' ){ 177 eq = (eq === -1); 178 }else if( expr === '<=' ){ 179 eq = (eq === -1 || eq === 0); 180 }else if( expr === '=' ){ 181 eq = (eq === 0); 182 }else if( expr === '!=' ){ 183 eq = (eq !== 0); 184 }else if( expr === '>' ){ 185 eq = (eq === 1); 186 }else if( expr === '>=' ){ 187 eq = (eq === 1 || eq === 0); 188 }else{ 189 eq = null; 190 } 191 192 return eq; 193 194 }; 195 196 $this.str2tsParsers = { 197 '/^[\\d]{2}\\/[\\d]{2}\\/[\\d]{4}$/': function(date){ 198 //m/d/Y 199 date = date.split('/'); 200 return $this.mkts(0, 0, 0, date[0], date[1], date[2]); 201 }, 202 '/^[\\d]{4}\\/[\\d]{2}\\/[\\d]{2}$/': function(date){ 203 //Y/m/d 204 date = date.split('/'); 205 return $this.mkts(0, 0, 0, date[1], date[2], date[0]); 206 }, 207 '/^[\\d]+$/': function(date){ 208 //timestamp 209 return date * 1; 210 } 211 }; 212 213 $this.dateFormats = { 214 215 Date: 'm/d/Y', 216 DateLong: 'F d, Y', 217 218 DateTime: 'm/d/Y H:i', 219 DateTimeLong: 'F d, Y H:i', 220 221 DateTime12: 'm/d/Y h:i A', 222 DateTime12Long: 'F d, Y h:i A', 223 224 DateTimeS: 'm/d/Y H:i:s', 225 DateTimeSLong: 'F d, Y H:i:s', 226 227 DateTimeS12: 'm/d/Y h:i:s A', 228 DateTimeS12Long: 'F d, Y h:i:s A', 229 230 Month: 'm/Y', 231 MonthLong: 'F, Y', 232 233 Obj: function(ts){ return new Date(ts); }, 234 235 Ts: function(ts){ return ts; } 236 237 }; 238 239 $this.date = function jhDate_date(format, ts){ 240 241 var date = new Date(ts) 242 , letters = $this.date.letters 243 , formated = "" 244 ; 245 246 var 247 i, i_max, format_i //for+ 248 ; 249 for(i = 0, i_max = format.length; i < i_max; i++){ 250 format_i = format.charAt(i); 251 if( letters[format_i] === undefined ){ 252 formated += format_i; 253 }else{ 254 formated += letters[format_i](date) + ''; 255 } 256 } 257 258 return formated; 259 260 }; 261 262 $this.date.months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "
262November", "December"]; 263 $this.date.months_short = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; 264 $this.date.addZero = function(number){ return number * 1 >= 10 ? number : '0' + number; }; 265 $this.date.letters = { 266 A: function(date){ return date.getHours() > 11 ? "PM" : "AM"; }, 267 d: function(date){ return $this.date.addZero(date.getDate()); }, 268 F: function(date){ return $this.date.months[date.getMonth()]; }, 269 M: function(date){ return $this.date.months_short[date.getMonth()]; }, 270 H: function(date){ return $this.date.addZero(date.getHours()); }, 271 h: function(date){ return $this.date.addZero(date.getHours % 12 || 12); }, 272 i: function(date){ return $this.date.addZero(date.getMinutes()); }, 273 m: function(date){ return $this.date.addZero(date.getMonth() + 1); }, 274 s: function(date){ return $this.date.addZero(date.getSeconds()); }, 275 Y: function(date){ return date.getFullYear(); }, 276 t: function(date){ return (new Date(date.getFullYear(), date.getMonth() + 1, 0)).getDate(); }, 277 z: function(date){
278 var year = date.getFullYear(), month = date.getMonth(), day = date.getDate() 279 , date1 = Date.UTC(year, 0, 1, 0, 0, 0) // noon on Jan. 1 280 , date2 = Date.UTC(year, month, day, 0, 0, 0) // noon on input date 281 ; 282 if( (date2 - date1)/(86400 * 1000) != Math.floor((date2 - date1)/(86400 * 1000)) ) 283 return Math.floor((date2 - date1)/(86400 * 1000)); 284 } 285 }; 286 287}(jhInstance));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.