From 620f3e39c3e9859b198f850f01a154fe3d00ee70 Mon Sep 17 00:00:00 2001 From: phasefx Date: Thu, 11 Aug 2005 15:14:26 +0000 Subject: [PATCH] shuffling code around, and proper date validation git-svn-id: svn://svn.open-ils.org/ILS/trunk@1631 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../evergreen/patron/patron_edit_legacy.js | 5 +- .../chrome/content/evergreen/util/datetime.js | 71 +++++++++++++++++++ .../chrome/content/evergreen/util/util.js | 69 ------------------ .../chrome/content/evergreen/util/validate.js | 9 +++ 4 files changed, 84 insertions(+), 70 deletions(-) create mode 100644 Evergreen/staff_client/chrome/content/evergreen/util/datetime.js diff --git a/Evergreen/staff_client/chrome/content/evergreen/patron/patron_edit_legacy.js b/Evergreen/staff_client/chrome/content/evergreen/patron/patron_edit_legacy.js index ca9ca33694..efaf7dc123 100644 --- a/Evergreen/staff_client/chrome/content/evergreen/patron/patron_edit_legacy.js +++ b/Evergreen/staff_client/chrome/content/evergreen/patron/patron_edit_legacy.js @@ -507,7 +507,10 @@ function validate_patron() { var month = darray[1]; if ( (!month) || (month.length !=2) || (!parseInt(month)) ) flag = true; var day = darray[2]; if ( (!day) || (day.length !=2) || (!parseInt(day)) ) flag = true; if (flag) { - s += ('Date Format is YYYY-MM-DD'); + s += ('Date Format is YYYY-MM-DD\n'); + } + if (!valid_year_month_day(year,month,day)) { + s += ('Invalid Date\n'); } } if ( ! PATRON.au.mailing_address() ) { diff --git a/Evergreen/staff_client/chrome/content/evergreen/util/datetime.js b/Evergreen/staff_client/chrome/content/evergreen/util/datetime.js new file mode 100644 index 0000000000..859c748280 --- /dev/null +++ b/Evergreen/staff_client/chrome/content/evergreen/util/datetime.js @@ -0,0 +1,71 @@ +var timer = {}; + +function timer_init(id) { + timer[id] = (new Date).getTime(); +} + +function timer_elapsed(id) { + if (! timer[id]) { timer_init(id); } + var ms = (new Date).getTime() - timer[id]; + return( ms + 'ms (' + ms/1000 + 's)' ); +} + +function db_date2Date(date) { + var y = date.substr(0,4); + var mo = date.substr(5,2); + var d = date.substr(8,2); + var h = date.substr(11,2); + var mi = date.substr(14,2); + var s = date.substr(17,2); + return new Date(y,mo,d,h,mi,s); +} + +function formatted_date(date,format) { + // pass in a Date object or epoch seconds or a postgres style date string (2005-07-19 10:38:25.211964-04) + if (typeof(date) == 'string') { + if (date.match(/:/) || date.match(/-/)) { + date = db_date2Date(date); + } else { + date = new Date( parseInt( date + '000' ) ); + } + } + var mm = date.getMonth() + 1; mm = mm.toString(); if (mm.length == 1) mm = '0' +mm; + var dd = date.getDate().toString(); if (dd.length == 1) dd = '0' +dd; + var yyyy = date.getFullYear().toString(); + var yy = yyyy.substr(2); + var H = date.getHours(); H = H.toString(); if (H.length == 1) H = '0' + H; + var I = date.getHours(); if (I > 12) I -= 12; I = I.toString(); + var M = date.getMinutes(); M = M.toString(); if (M.length == 1) M = '0' + M; + var s = format; + s = s.replace( /%m/g, mm ); + s = s.replace( /%d/g, dd ); + s = s.replace( /%Y/g, yyyy ); + s = s.replace( /%D/g, mm + '/' + dd + '/' + yy ); + s = s.replace( /%F/g, yyyy + '-' + mm + '-' + dd ); + s = s.replace( /%H/g, H ); + s = s.replace( /%I/g, I ); + s = s.replace( /%M/g, M ); + return s; +} + +function interval_to_seconds ( $interval ) { + + $interval = $interval.replace( /and/, ',' ); + $interval = $interval.replace( /,/, ' ' ); + + var $amount = 0; + var results = $interval.match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/g); + for (var i in results) { + var result = results[i].match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/ ); + if (result[2] == 's') $amount += result[1] ; + if (result[2] == 'm') $amount += 60 * result[1] ; + if (result[2] == 'h') $amount += 60 * 60 * result[1] ; + if (result[2] == 'd') $amount += 60 * 60 * 24 * result[1] ; + if (result[2] == 'w') $amount += 60 * 60 * 24 * 7 * result[1] ; + if (result[2] == 'M') $amount += ((60 * 60 * 24 * 365)/12) * result[1] ; + if (result[2] == 'y') $amount += 60 * 60 * 24 * 365 * result[1] ; + } + return $amount; +} + + diff --git a/Evergreen/staff_client/chrome/content/evergreen/util/util.js b/Evergreen/staff_client/chrome/content/evergreen/util/util.js index 78eb9f002d..a95f10518f 100644 --- a/Evergreen/staff_client/chrome/content/evergreen/util/util.js +++ b/Evergreen/staff_client/chrome/content/evergreen/util/util.js @@ -1,6 +1,5 @@ sdump('D_TRACE','Loading util.js\n'); -var timer = {}; var counter = {}; var consider_Timeout_default = false; @@ -151,64 +150,6 @@ function cents_as_dollars( cents ) { return cents.substr(0,cents.length-2) + '.' + cents.substr(cents.length - 2); } -function db_date2Date(date) { - var y = date.substr(0,4); - var mo = date.substr(5,2); - var d = date.substr(8,2); - var h = date.substr(11,2); - var mi = date.substr(14,2); - var s = date.substr(17,2); - return new Date(y,mo,d,h,mi,s); -} - -function formatted_date(date,format) { - // pass in a Date object or epoch seconds or a postgres style date string (2005-07-19 10:38:25.211964-04) - if (typeof(date) == 'string') { - if (date.match(/:/) || date.match(/-/)) { - date = db_date2Date(date); - } else { - date = new Date( parseInt( date + '000' ) ); - } - } - var mm = date.getMonth() + 1; mm = mm.toString(); if (mm.length == 1) mm = '0' +mm; - var dd = date.getDate().toString(); if (dd.length == 1) dd = '0' +dd; - var yyyy = date.getFullYear().toString(); - var yy = yyyy.substr(2); - var H = date.getHours(); H = H.toString(); if (H.length == 1) H = '0' + H; - var I = date.getHours(); if (I > 12) I -= 12; I = I.toString(); - var M = date.getMinutes(); M = M.toString(); if (M.length == 1) M = '0' + M; - var s = format; - s = s.replace( /%m/g, mm ); - s = s.replace( /%d/g, dd ); - s = s.replace( /%Y/g, yyyy ); - s = s.replace( /%D/g, mm + '/' + dd + '/' + yy ); - s = s.replace( /%F/g, yyyy + '-' + mm + '-' + dd ); - s = s.replace( /%H/g, H ); - s = s.replace( /%I/g, I ); - s = s.replace( /%M/g, M ); - return s; -} - -function interval_to_seconds ( $interval ) { - - $interval = $interval.replace( /and/, ',' ); - $interval = $interval.replace( /,/, ' ' ); - - var $amount = 0; - var results = $interval.match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/g); - for (var i in results) { - var result = results[i].match( /\s*\+?\s*(\d+)\s*(\w{1})\w*\s*/ ); - if (result[2] == 's') $amount += result[1] ; - if (result[2] == 'm') $amount += 60 * result[1] ; - if (result[2] == 'h') $amount += 60 * 60 * result[1] ; - if (result[2] == 'd') $amount += 60 * 60 * 24 * result[1] ; - if (result[2] == 'w') $amount += 60 * 60 * 24 * 7 * result[1] ; - if (result[2] == 'M') $amount += ((60 * 60 * 24 * 365)/12) * result[1] ; - if (result[2] == 'y') $amount += 60 * 60 * 24 * 365 * result[1] ; - } - return $amount; -} - /* function debug() { var s = ''; @@ -233,16 +174,6 @@ function counter_peek(id) { return counter[id]; } -function timer_init(id) { - timer[id] = (new Date).getTime(); -} - -function timer_elapsed(id) { - if (! timer[id]) { timer_init(id); } - var ms = (new Date).getTime() - timer[id]; - return( ms + 'ms (' + ms/1000 + 's)' ); -} - function dump_ns_node( node ) { return ( 'id=<' + diff --git a/Evergreen/staff_client/chrome/content/evergreen/util/validate.js b/Evergreen/staff_client/chrome/content/evergreen/util/validate.js index 1e879de0ba..9db5af4d30 100644 --- a/Evergreen/staff_client/chrome/content/evergreen/util/validate.js +++ b/Evergreen/staff_client/chrome/content/evergreen/util/validate.js @@ -1,5 +1,14 @@ sdump('D_TRACE',"Loading validate.js\n"); +function valid_year_month_day(year,month,day) { + var date = new Date(year,month-1,day); + return ( + (date.getFullYear() == year) && + (date.getMonth()+1 == month) && + (date.getDate() == day) + ); +} + function textbox_checkdigit(ev) { if ( check_checkdigit( ev.target.value ) ) { sdump('D_VALIDATE', 'success\n'); -- 2.43.2