Revision: 3858
          http://vexi.svn.sourceforge.net/vexi/?rev=3858&view=rev
Author:   clrg
Date:     2010-07-01 02:33:55 +0000 (Thu, 01 Jul 2010)

Log Message:
-----------
Enhance util.date
- add function isDateInRange
- make js:Object instead of ui:box

Modified Paths:
--------------
    trunk/widgets/org.vexi.widgets/src/vexi/util/date.t

Modified: trunk/widgets/org.vexi.widgets/src/vexi/util/date.t
===================================================================
--- trunk/widgets/org.vexi.widgets/src/vexi/util/date.t 2010-07-01 02:33:10 UTC 
(rev 3857)
+++ trunk/widgets/org.vexi.widgets/src/vexi/util/date.t 2010-07-01 02:33:55 UTC 
(rev 3858)
@@ -1,6 +1,6 @@
 <!-- Copyright 2009 - see COPYING for details [LGPL] -->
 
-<vexi xmlns:ui="vexi://ui" xmlns:meta="vexi://meta">
+<vexi xmlns:js="vexi://js" xmlns:meta="vexi://meta" xmlns="vexi.util">
     <meta:doc>
         <author>Charles Goodwin</author>
         <name>Date</name>
@@ -50,28 +50,30 @@
         </usage>
     </meta:doc>
     
-    <ui:box>
+    <js:Object>
         
-        thisbox.date;
-        thisbox.day;
-        thisbox.month;
-        thisbox.monthdays;
-        thisbox.year;
-        thisbox.weekday;
+        thisobj.allowzeroday = false;
+        thisobj.date;
+        thisobj.day;
+        thisobj.month;
+        thisobj.monthdays;
+        thisobj.year;
+        thisobj.weekday;
         
-        thisbox.getDaysInMonth = static.getDaysInMonth;
-        thisbox.getMonthName = static.getMonthName;
-        thisbox.getWeekday = static.getWeekday;
-        thisbox.parseDate = static.parseDate;
+        thisobj.getDaysInMonth = static.getDaysInMonth;
+        thisobj.getMonthName = static.getMonthName;
+        thisobj.getWeekday = static.getWeekday;
+        thisobj.isDateInRange = static.isDateInRange;
+        thisobj.parseDate = static.parseDate;
         
-        thisbox.date  ++= static.dateRead;
-        thisbox.date  ++= static.dateWrite;
-        thisbox.day   ++= static.dayWrite;
-        thisbox.month ++= static.monthWrite;
-        thisbox.weekday ++= static.weekdayRead;
-        thisbox.monthname ++= static.monthnameRead;
+        thisobj.date  ++= static.dateRead;
+        thisobj.date  ++= static.dateWrite;
+        thisobj.day   ++= static.dayWrite;
+        thisobj.month ++= static.monthWrite;
+        thisobj.weekday ++= static.weekdayRead;
+        thisobj.monthname ++= static.monthnameRead;
         
-    </ui:box>
+    </js:Object>
     
     /** date read trap */
     static.dateRead = function() {
@@ -93,10 +95,12 @@
     /** constrain days */
     static.dayWrite = function(v) {
         if (v != null) {
-            if (trapee.allowzeroday and v == 0) 
+            if (trapee.allowzeroday and v == 0) {
                 ;
-                       else {
-                if (typeof(v)=="string") v = vexi.string.parseInt(v);
+                       } else {
+                if (typeof(v)=="string") {
+                    v = vexi.string.parseInt(v);
+                }
                    // NOTE: the order of the monthday +=/-= and month ++/--
                 // is important and dependent on days surplus / shortage
                 while (v>trapee.monthdays) {
@@ -137,25 +141,66 @@
     /** get day of the week for a given year, month, and day */
     static.getWeekday = function(y, m, d) { return vexi.date(y, m-1, 
d).getDay(); }
     
+    /** 
+     *  tests if a date is in the given range
+     * 
+     *  returns:
+     *    -1 : date is before range
+     *     0 : date is in range
+     *     1 : date is after range
+     */
+    static.isDateInRange = function(d, d1, d2) {
+        var dY = d.getYear();
+        var dM = d.getMonth();
+        var dD = d.getDate();
+        var d1Y = d1.getYear();
+        var d1M = d1.getMonth();
+        var d1D = d1.getDate();
+        var d2Y = d2.getYear();
+        var d2M = d2.getMonth();
+        var d2D = d2.getDate();
+        if (d1Y>dY or (d1Y==dY and (d1M>dM or (d1M==dM and d1D>dD)))) {
+            // below range
+            return -1;
+        }
+        if (dY>d2Y or (dy==D2Y and (dM>d2M or (dM==d2M and dD>d2D)))) {
+            // above range
+            return 1;
+        }
+        // date is in range
+        return 0;
+    }
+    
     /** read the name of a month of a date */
     static.monthnameRead = function() { return 
static.getMonthName(trapee.month); }
     
     /** contrains month and set monthdays */
     static.monthWrite = function(v) {
         if (v != null) {
-            if (typeof(v)=="string") v = vexi.string.parseInt(v);
-            while (v>12) { trapee.year = trapee.year+1; v -= 12; }
-            while (1>v) { trapee.year = trapee.year-1; v += 12; }
+            if (typeof(v)=="string") {
+                v = vexi.string.parseInt(v);
+            }
+            while (v>12) {
+                trapee.year = trapee.year+1;
+                v -= 12;
+            }
+            while (1>v) {
+                trapee.year = trapee.year-1;
+                v += 12;
+            }
             // establish month length
             trapee.monthdays = static.getDaysInMonth(trapee.year, v);
-        } else trapee.monthdays = null;
+        } else {
+            trapee.monthdays = null;
+        }
         cascade = v;
     }
     
     /** contrains month and set monthdays */
     static.yearWrite = function(v) {
-        if (v != null and typeof(v)=="string")
+        if (v != null and typeof(v)=="string") {
             v = vexi.string.parseInt(v);
+        }
         cascade = v;
     }
     
@@ -167,8 +212,9 @@
         if (2>=ystr.length) {
             var d = vexi.date().getFullYear();
             // auto-fill year to this year
-            if (ystr=="") return d;
-            else {
+            if (ystr=="") {
+                return d;
+            } else {
                 // auto-expand 2-digit years
                 ystr = vexi.string.parseInt(ystr, 10);
                 return (d+10>=ystr ? 2000 : 1900) + ystr;
@@ -178,7 +224,9 @@
     
     /** takes a string 'v' and parses it as a date for 'o' */
     static.parseDate = function(v, format, o) {
-        if (!o) o = {};
+        if (!o) {
+            o = new .date();
+        }
         // establish how many characters for each part
         var m, m1, m2;
         var m3 = format.length;


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to