@@ -41,17 +41,24 @@ func MinDuration() Duration {
4141
4242// Max returns the maximum Carbon instance from some given Carbon instances.
4343func Max (c1 * Carbon , c2 ... * Carbon ) (c * Carbon ) {
44- c = c1
45- if c .IsInvalid () {
46- return
44+ // If first carbon is invalid, return it immediately
45+ if c1 .IsInvalid () {
46+ return c1
4747 }
48+
49+ c = c1
50+ // If no additional arguments, return the first one
4851 if len (c2 ) == 0 {
4952 return
5053 }
54+
55+ // Check all additional arguments
5156 for _ , carbon := range c2 {
57+ // If any carbon is invalid, return it immediately
5258 if carbon .IsInvalid () {
5359 return carbon
5460 }
61+ // Update maximum if current carbon is greater or equal
5562 if carbon .Gte (c ) {
5663 c = carbon
5764 }
@@ -61,17 +68,24 @@ func Max(c1 *Carbon, c2 ...*Carbon) (c *Carbon) {
6168
6269// Min returns the minimum Carbon instance from some given Carbon instances.
6370func Min (c1 * Carbon , c2 ... * Carbon ) (c * Carbon ) {
64- c = c1
65- if c .IsInvalid () {
66- return
71+ // If first carbon is invalid, return it immediately
72+ if c1 .IsInvalid () {
73+ return c1
6774 }
75+
76+ c = c1
77+ // If no additional arguments, return the first one
6878 if len (c2 ) == 0 {
6979 return
7080 }
81+
82+ // Check all additional arguments
7183 for _ , carbon := range c2 {
84+ // If any carbon is invalid, return it immediately
7285 if carbon .IsInvalid () {
7386 return carbon
7487 }
88+ // Update minimum if current carbon is less or equal
7589 if carbon .Lte (c ) {
7690 c = carbon
7791 }
@@ -81,23 +95,34 @@ func Min(c1 *Carbon, c2 ...*Carbon) (c *Carbon) {
8195
8296// Closest returns the closest Carbon instance from some given Carbon instances.
8397func (c * Carbon ) Closest (c1 * Carbon , c2 ... * Carbon ) * Carbon {
98+ // Validate the base carbon instance
8499 if c .IsInvalid () {
85100 return c
86101 }
102+
103+ // Validate the first comparison instance
87104 if c1 .IsInvalid () {
88105 return c1
89106 }
107+
108+ // If no additional arguments, return the first one
90109 if len (c2 ) == 0 {
91110 return c1
92111 }
112+
113+ // Find the closest among all instances
93114 closest := c1
94115 minDiff := c .DiffAbsInSeconds (closest )
116+
117+ // Check all additional arguments
95118 for _ , arg := range c2 {
119+ // Validate each argument
96120 if arg .IsInvalid () {
97121 return arg
98122 }
99- diff := c .DiffAbsInSeconds (arg )
100- if diff < minDiff {
123+
124+ // Calculate difference and update if closer
125+ if diff := c .DiffAbsInSeconds (arg ); diff < minDiff {
101126 minDiff = diff
102127 closest = arg
103128 }
@@ -107,23 +132,34 @@ func (c *Carbon) Closest(c1 *Carbon, c2 ...*Carbon) *Carbon {
107132
108133// Farthest returns the farthest Carbon instance from some given Carbon instances.
109134func (c * Carbon ) Farthest (c1 * Carbon , c2 ... * Carbon ) * Carbon {
135+ // Validate the base carbon instance
110136 if c .IsInvalid () {
111137 return c
112138 }
139+
140+ // Validate the first comparison instance
113141 if c1 .IsInvalid () {
114142 return c1
115143 }
144+
145+ // If no additional arguments, return the first one
116146 if len (c2 ) == 0 {
117147 return c1
118148 }
149+
150+ // Find the farthest among all instances
119151 farthest := c1
120152 maxDiff := c .DiffAbsInSeconds (farthest )
153+
154+ // Check all additional arguments
121155 for _ , arg := range c2 {
156+ // Validate each argument
122157 if arg .IsInvalid () {
123158 return arg
124159 }
125- diff := c .DiffAbsInSeconds (arg )
126- if diff > maxDiff {
160+
161+ // Calculate difference and update if farther
162+ if diff := c .DiffAbsInSeconds (arg ); diff > maxDiff {
127163 maxDiff = diff
128164 farthest = arg
129165 }
0 commit comments