Index: ruby/trunk/lib/open_ehr/am/archetype/constraint_model/primitive.rb
===================================================================
--- ruby/trunk/lib/open_ehr/am/archetype/constraint_model/primitive.rb	(revision 400)
+++ ruby/trunk/lib/open_ehr/am/archetype/constraint_model/primitive.rb	(revision 401)
@@ -256,4 +256,38 @@
             end
           end
+
+          class CDuration < CPrimitive
+            attr_reader :range
+            attr_accessor :years_allowed, :months_allowed, :weeks_allowed,
+                          :days_allowed, :hours_allowed, :minutes_allowed,
+                          :seconds_allowed, :fractional_seconds_allowed
+
+            def initialize(args = { })
+              super
+              self.fractional_seconds_allowed = args[:fractional_seconds_allowed]
+              self.seconds_allowed = args[:seconds_allowed]
+              self.minutes_allowed = args[:minutes_allowed]
+              self.hours_allowed = args[:hours_allowed]
+              self.days_allowed = args[:days_allowed]
+              self.months_allowed = args[:months_allowed]
+              self.weeks_allowed = args[:weeks_allowed]
+              self.years_allowed = args[:years_allowed]
+              self.range = args[:range]
+            end
+
+            def range=(range)
+              if range.nil? && !(@years_allowed ||
+                    @months_allowed ||
+                    @weeks_allowed ||
+                    @days_allowed ||
+                    @hours_allowed ||
+                    @minutes_allowed ||
+                    @seconds_allowed ||
+                    @fractional_seconds_allowed)
+                raise ArgumentError, 'invalid range'
+              end
+              @range = range
+            end
+          end
         end # of Primitive
       end # of CostraintModel
Index: ruby/trunk/lib/open_ehr/assumed_library_types.rb
===================================================================
--- ruby/trunk/lib/open_ehr/assumed_library_types.rb	(revision 400)
+++ ruby/trunk/lib/open_ehr/assumed_library_types.rb	(revision 401)
@@ -165,4 +165,11 @@
       end
 
+      def to_days
+        days = nilthenzero(@year)*TimeDefinitions::NOMINAL_DAYS_IN_YEAR +
+          nilthenzero(@month)*TimeDefinitions::NOMINAL_DAYS_IN_MONTH +
+          nilthenzero(@day)
+        return days
+      end
+
       def month_unknown?
         @month.nil?
@@ -217,13 +224,5 @@
 
       def <=>(other)
-        magnitude =
-          nilthenzero(@year)*TimeDefinitions::NOMINAL_DAYS_IN_YEAR +
-          nilthenzero(@month)*TimeDefinitions::NOMINAL_DAYS_IN_MONTH +
-          nilthenzero(@day)
-        other_magnitude =
-          nilthenzero(other.year)*TimeDefinitions::NOMINAL_DAYS_IN_YEAR +
-          nilthenzero(other.month)*TimeDefinitions::NOMINAL_DAYS_IN_MONTH +
-          nilthenzero(other.day)
-        magnitude <=> other_magnitude
+        self.to_days <=> other.to_days
       end
 
@@ -322,5 +321,10 @@
       end
 
-      
+      def to_second
+        second = (nilthenzero(@hour)*60 + nilthenzero(@minute))*60 +
+          nilthenzero(@second) +
+          nilthenzero(@fractional_second)
+        return second
+      end
     end
 
@@ -357,12 +361,5 @@
 
       def <=>(other)
-        magnitude = (nilthenzero(@hour)*60 + nilthenzero(@minute))*60 +
-          nilthenzero(@second) +
-          nilthenzero(@fractional_second)
-        other_magnitude = (nilthenzero(other.hour) * 60 +
-                           nilthenzero(other.minute)) * 60 +
-          nilthenzero(other.second) +
-          nilthenzero(other.fractional_second)
-        magnitude <=> other_magnitude
+        self.to_second <=> other.to_second
       end
 
@@ -480,4 +477,14 @@
         end
       end
+
+      def <=>(other)
+        self.magnitude <=> other.magnitude
+      end
+
+      protected
+      def magnitude
+        return self.to_days*HOURS_IN_DAY*MINUTES_IN_HOUR*SECONDS_IN_MINUTE +
+          self.to_second
+      end
     end
   
