@@ -28,20 +28,26 @@ internal static class CqlDateTimeMath
28
28
/// <seealso href="https://cql.hl7.org/09-b-cqlreference.html#difference"/>
29
29
internal static int ? BoundariesBetween ( DateTimeOffset ? low , DateTimeOffset ? high , string ? precision )
30
30
{
31
- if ( low == null || high == null || precision == null )
31
+ if ( low is not { } firstDto || high is not { } secondDto || precision is null )
32
32
return null ;
33
33
34
- var firstDto = low . Value ;
35
- var secondDto = high . Value ;
36
34
switch ( precision )
37
35
{
36
+ case UCUMUnits . Year :
37
+ throw new NotImplementedException ( ) ;
38
+
38
39
case "year" :
39
40
var yearDiff = ( secondDto . Year - firstDto . Year ) ;
40
41
return yearDiff ;
42
+
43
+ case UCUMUnits . Month :
44
+ throw new NotImplementedException ( ) ;
45
+
41
46
case "month" :
42
47
var monthDiff = ( 12 * ( secondDto . Year - firstDto . Year ) + secondDto . Month - firstDto . Month ) ;
43
48
return monthDiff ;
44
- case "week" :
49
+
50
+ case "week" or UCUMUnits . Week :
45
51
{
46
52
var span = secondDto . Subtract ( firstDto ) ;
47
53
var weeks = span . TotalDays / 7d ;
@@ -52,7 +58,8 @@ internal static class CqlDateTimeMath
52
58
return asInt + 1 ;
53
59
else return asInt ;
54
60
}
55
- case "day" :
61
+
62
+ case "day" or UCUMUnits . Day :
56
63
{
57
64
var span = secondDto . Subtract ( firstDto ) ;
58
65
var asInt = ( int ) span . TotalDays ;
@@ -64,7 +71,8 @@ internal static class CqlDateTimeMath
64
71
}
65
72
else return asInt ;
66
73
}
67
- case "hour" :
74
+
75
+ case "hour" or UCUMUnits . Hour :
68
76
{
69
77
var span = secondDto . Subtract ( firstDto ) ;
70
78
var asInt = ( int ) span . TotalHours ;
@@ -76,7 +84,8 @@ internal static class CqlDateTimeMath
76
84
}
77
85
else return asInt ;
78
86
}
79
- case "minute" :
87
+
88
+ case "minute" or UCUMUnits . Minute :
80
89
{
81
90
var span = secondDto . Subtract ( firstDto ) ;
82
91
var asInt = ( int ) span . TotalMinutes ;
@@ -88,7 +97,8 @@ internal static class CqlDateTimeMath
88
97
}
89
98
else return asInt ;
90
99
}
91
- case "second" :
100
+
101
+ case "second" or UCUMUnits . Second :
92
102
{
93
103
var span = secondDto . Subtract ( firstDto ) ;
94
104
var asInt = ( int ) span . TotalSeconds ;
@@ -100,7 +110,8 @@ internal static class CqlDateTimeMath
100
110
}
101
111
else return asInt ;
102
112
}
103
- case "millisecond" :
113
+
114
+ case "millisecond" or UCUMUnits . Millisecond :
104
115
{
105
116
var span = secondDto . Subtract ( firstDto ) ;
106
117
var asInt = ( int ) span . TotalMilliseconds ;
@@ -112,20 +123,22 @@ internal static class CqlDateTimeMath
112
123
}
113
124
else return asInt ;
114
125
}
126
+
115
127
default : throw new ArgumentException ( $ "Unit '{ precision } ' is not supported.") ;
116
128
}
117
129
}
118
130
119
- internal static int ? WholeCalendarPeriodsBetween ( DateTimeOffset ? low , DateTimeOffset ? high , string precision )
131
+ internal static int ? WholeCalendarPeriodsBetween ( DateTimeOffset ? low , DateTimeOffset ? high , string ? precision )
120
132
{
121
- if ( low == null || high == null || precision == null )
133
+ if ( low is not { } firstDto || high is not { } secondDto || precision == null )
122
134
return null ;
123
135
124
136
var calendar = new GregorianCalendar ( ) ;
125
- var firstDto = low . Value ;
126
- var secondDto = high . Value ;
127
137
switch ( precision )
128
138
{
139
+ case UCUMUnits . Year :
140
+ throw new NotImplementedException ( ) ;
141
+
129
142
case "year" :
130
143
var yearDiff = secondDto . Year - firstDto . Year ;
131
144
var firstDayInYear = firstDto . DayOfYear ;
@@ -192,20 +205,25 @@ internal static class CqlDateTimeMath
192
205
else if ( yearDiff < 0 && firstDayInYear < secondDayInYear )
193
206
yearDiff += 1 ;
194
207
return yearDiff ;
208
+
209
+ case UCUMUnits . Month :
210
+ throw new NotImplementedException ( ) ;
211
+
195
212
case "month" :
196
213
var monthDiff = ( 12 * ( secondDto . Year - firstDto . Year ) + secondDto . Month - firstDto . Month ) ;
197
214
if ( monthDiff > 0 && secondDto . Day < firstDto . Day )
198
215
monthDiff -= 1 ;
199
216
else if ( monthDiff < 0 && firstDto . Day < secondDto . Day )
200
217
monthDiff += 1 ;
201
218
return monthDiff ;
202
- case "week" : return ( int ) ( secondDto . Subtract ( firstDto ) . TotalDays / DaysPerWeekDouble ) ;
203
- case "day" : return ( int ) secondDto . Subtract ( firstDto ) . TotalDays ;
204
- case "hour" : return ( int ) secondDto . Subtract ( firstDto ) . TotalHours ;
205
- case "minute" : return ( int ) secondDto . Subtract ( firstDto ) . TotalMinutes ;
206
- case "second" : return ( int ) secondDto . Subtract ( firstDto ) . TotalSeconds ;
207
- case "millisecond" : return ( int ) secondDto . Subtract ( firstDto ) . TotalMilliseconds ;
208
- default : throw new ArgumentException ( $ "Unit '{ precision } ' is not supported.") ;
219
+
220
+ case "week" or UCUMUnits . Week : return ( int ) ( secondDto . Subtract ( firstDto ) . TotalDays / DaysPerWeekDouble ) ;
221
+ case "day" or UCUMUnits . Day : return ( int ) secondDto . Subtract ( firstDto ) . TotalDays ;
222
+ case "hour" or UCUMUnits . Hour : return ( int ) secondDto . Subtract ( firstDto ) . TotalHours ;
223
+ case "minute" or UCUMUnits . Minute : return ( int ) secondDto . Subtract ( firstDto ) . TotalMinutes ;
224
+ case "second" or UCUMUnits . Second : return ( int ) secondDto . Subtract ( firstDto ) . TotalSeconds ;
225
+ case "millisecond" or UCUMUnits . Millisecond : return ( int ) secondDto . Subtract ( firstDto ) . TotalMilliseconds ;
226
+ default : throw new ArgumentException ( $ "Unit '{ precision } ' is not supported.") ;
209
227
}
210
228
}
211
229
@@ -220,67 +238,67 @@ internal static class CqlDateTimeMath
220
238
{ DateTimePrecision . Year , new CqlQuantity ( 1m , "year" ) } ,
221
239
} ;
222
240
223
- /// <summary>
224
- /// For datetime addition and subtraction, when quantity is more precise than the datetime,
225
- /// the quantity has to be normalized to the lesser precision and truncated.
226
- /// </summary>
227
- /// <see href="https://cql.hl7.org/09-b-cqlreference.html#add-1" />
228
- internal static CqlQuantity NormalizeTo ( this CqlQuantity quantity , DateTimePrecision target )
229
- {
230
- // using the table found here:
231
- // https://cql.hl7.org/09-b-cqlreference.html#equivalent
232
- return ( quantity . unit , target ) switch
233
- {
234
- ( null , _ ) => quantity ,
235
- ( "mo" , DateTimePrecision . Year ) =>
236
- new CqlQuantity ( Math . Truncate ( ( quantity . value ?? 0 ) / 12 ) ! , UCUMUnits . Year ) ,
237
-
238
- ( "d" , DateTimePrecision . Year ) =>
239
- new CqlQuantity ( Math . Truncate ( ( quantity . value ?? 0 ) / 365 ) ! , UCUMUnits . Year ) ,
240
- ( "d" , DateTimePrecision . Month ) =>
241
- new CqlQuantity ( Math . Truncate ( ( quantity . value ?? 0 ) / 30 ) ! , UCUMUnits . Month ) ,
242
-
243
- ( "h" , DateTimePrecision . Year ) =>
244
- new CqlQuantity ( Math . Truncate ( ( ( quantity . value ?? 0 ) / 24 ) / 365 ) ! , UCUMUnits . Year ) ,
245
- ( "h" , DateTimePrecision . Month ) =>
246
- new CqlQuantity ( Math . Truncate ( ( ( quantity . value ?? 0 ) / 24 ) / 30 ) ! , UCUMUnits . Month ) ,
247
- ( "h" , DateTimePrecision . Day ) =>
248
- new CqlQuantity ( Math . Truncate ( ( quantity . value ?? 0 ) / 24 ) ! , UCUMUnits . Day ) ,
249
-
250
- ( "mi" , DateTimePrecision . Year ) =>
251
- new CqlQuantity ( Math . Truncate ( ( ( ( quantity . value ?? 0 ) / 60 ) / 24 ) / 365 ) ! , UCUMUnits . Year ) ,
252
- ( "mi" , DateTimePrecision . Month ) =>
253
- new CqlQuantity ( Math . Truncate ( ( ( ( quantity . value ?? 0 ) / 60 ) / 24 ) / 30 ) ! , UCUMUnits . Month ) ,
254
- ( "mi" , DateTimePrecision . Day ) =>
255
- new CqlQuantity ( Math . Truncate ( ( ( quantity . value ?? 0 ) / 60 ) / 24 ) ! , UCUMUnits . Day ) ,
256
- ( "mi" , DateTimePrecision . Hour ) =>
257
- new CqlQuantity ( Math . Truncate ( ( quantity . value ?? 0 ) / 60 ) ! , UCUMUnits . Hour ) ,
258
-
259
- ( "s" , DateTimePrecision . Year ) =>
260
- new CqlQuantity ( Math . Truncate ( ( ( ( ( quantity . value ?? 0 ) / 60 ) / 60 ) / 24 ) / 365 ) ! , UCUMUnits . Year ) ,
261
- ( "s" , DateTimePrecision . Month ) =>
262
- new CqlQuantity ( Math . Truncate ( ( ( ( ( quantity . value ?? 0 ) / 60 ) / 60 ) / 24 ) / 30 ) ! , UCUMUnits . Month ) ,
263
- ( "s" , DateTimePrecision . Day ) =>
264
- new CqlQuantity ( Math . Truncate ( ( ( ( quantity . value ?? 0 ) / 60 ) / 60 ) / 24 ) ! , UCUMUnits . Day ) ,
265
- ( "s" , DateTimePrecision . Hour ) =>
266
- new CqlQuantity ( Math . Truncate ( ( ( quantity . value ?? 0 ) / 60 ) / 60 ) ! , UCUMUnits . Hour ) ,
267
- ( "s" , DateTimePrecision . Minute ) =>
268
- new CqlQuantity ( Math . Truncate ( ( quantity . value ?? 0 ) / 60 ) ! , UCUMUnits . Minute ) ,
269
-
270
- ( "ms" , DateTimePrecision . Year ) =>
271
- new CqlQuantity ( Math . Truncate ( ( ( ( ( ( quantity . value ?? 0 ) / 1000 ) / 60 ) / 60 ) / 24 ) / 365 ) ! , UCUMUnits . Year ) ,
272
- ( "ms" , DateTimePrecision . Month ) =>
273
- new CqlQuantity ( Math . Truncate ( ( ( ( ( ( quantity . value ?? 0 ) / 1000 ) / 60 ) / 60 ) / 24 ) / 30 ) ! , UCUMUnits . Month ) ,
274
- ( "ms" , DateTimePrecision . Day ) =>
275
- new CqlQuantity ( Math . Truncate ( ( ( ( ( quantity . value ?? 0 ) / 1000 ) / 60 ) / 60 ) / 24 ) ! , UCUMUnits . Day ) ,
276
- ( "ms" , DateTimePrecision . Hour ) =>
277
- new CqlQuantity ( Math . Truncate ( ( ( ( quantity . value ?? 0 ) / 1000 ) / 60 ) / 60 ) ! , UCUMUnits . Hour ) ,
278
- ( "ms" , DateTimePrecision . Minute ) =>
279
- new CqlQuantity ( Math . Truncate ( ( ( quantity . value ?? 0 ) / 1000 ) / 60 ) ! , UCUMUnits . Minute ) ,
280
- ( "ms" , DateTimePrecision . Second ) =>
281
- new CqlQuantity ( Math . Truncate ( ( quantity . value ?? 0 ) / 1000 ) ! , UCUMUnits . Second ) ,
282
- ( _, _) => quantity
283
- } ;
284
- }
241
+ // // / <summary>
242
+ // /// For datetime addition and subtraction, when quantity is more precise than the datetime,
243
+ // // / the quantity has to be normalized to the lesser precision and truncated.
244
+ // // / </summary>
245
+ // // / <see href="https://cql.hl7.org/09-b-cqlreference.html#add-1" />
246
+ // internal static CqlQuantity NormalizeTo(this CqlQuantity quantity, DateTimePrecision target)
247
+ // {
248
+ // // using the table found here:
249
+ // // https://cql.hl7.org/09-b-cqlreference.html#equivalent
250
+ // return (quantity.unit, target) switch
251
+ // {
252
+ // (null, _) => quantity,
253
+ // ("mo", DateTimePrecision.Year) =>
254
+ // new CqlQuantity(Math.Truncate((quantity.value ?? 0) / 12)!, UCUMUnits.Year),
255
+ //
256
+ // ("d", DateTimePrecision.Year) =>
257
+ // new CqlQuantity(Math.Truncate((quantity.value ?? 0) / 365)!, UCUMUnits.Year),
258
+ // ("d", DateTimePrecision.Month) =>
259
+ // new CqlQuantity(Math.Truncate((quantity.value ?? 0) / 30)!, UCUMUnits.Month),
260
+ //
261
+ // ("h", DateTimePrecision.Year) =>
262
+ // new CqlQuantity(Math.Truncate(((quantity.value ?? 0) / 24) / 365)!, UCUMUnits.Year),
263
+ // ("h", DateTimePrecision.Month) =>
264
+ // new CqlQuantity(Math.Truncate(((quantity.value ?? 0) / 24) / 30)!, UCUMUnits.Month),
265
+ // ("h", DateTimePrecision.Day) =>
266
+ // new CqlQuantity(Math.Truncate((quantity.value ?? 0) / 24)!, UCUMUnits.Day),
267
+ //
268
+ // ("mi", DateTimePrecision.Year) =>
269
+ // new CqlQuantity(Math.Truncate((((quantity.value ?? 0) / 60) / 24) / 365)!, UCUMUnits.Year),
270
+ // ("mi", DateTimePrecision.Month) =>
271
+ // new CqlQuantity(Math.Truncate((((quantity.value ?? 0) / 60) / 24) / 30)!, UCUMUnits.Month),
272
+ // ("mi", DateTimePrecision.Day) =>
273
+ // new CqlQuantity(Math.Truncate(((quantity.value ?? 0) / 60) / 24)!, UCUMUnits.Day),
274
+ // ("mi", DateTimePrecision.Hour) =>
275
+ // new CqlQuantity(Math.Truncate((quantity.value ?? 0) / 60)!, UCUMUnits.Hour),
276
+ //
277
+ // ("s", DateTimePrecision.Year) =>
278
+ // new CqlQuantity(Math.Truncate(((((quantity.value ?? 0) / 60) / 60) / 24) / 365)!, UCUMUnits.Year),
279
+ // ("s", DateTimePrecision.Month) =>
280
+ // new CqlQuantity(Math.Truncate(((((quantity.value ?? 0) / 60) / 60) / 24) / 30)!, UCUMUnits.Month),
281
+ // ("s", DateTimePrecision.Day) =>
282
+ // new CqlQuantity(Math.Truncate((((quantity.value ?? 0) / 60) / 60) / 24)!, UCUMUnits.Day),
283
+ // ("s", DateTimePrecision.Hour) =>
284
+ // new CqlQuantity(Math.Truncate(((quantity.value ?? 0) / 60) / 60)!, UCUMUnits.Hour),
285
+ // ("s", DateTimePrecision.Minute) =>
286
+ // new CqlQuantity(Math.Truncate((quantity.value ?? 0) / 60)!, UCUMUnits.Minute),
287
+ //
288
+ // ("ms", DateTimePrecision.Year) =>
289
+ // new CqlQuantity(Math.Truncate((((((quantity.value ?? 0) / 1000) / 60) / 60) / 24) / 365)!, UCUMUnits.Year),
290
+ // ("ms", DateTimePrecision.Month) =>
291
+ // new CqlQuantity(Math.Truncate((((((quantity.value ?? 0) / 1000) / 60) / 60) / 24) / 30)!, UCUMUnits.Month),
292
+ // ("ms", DateTimePrecision.Day) =>
293
+ // new CqlQuantity(Math.Truncate(((((quantity.value ?? 0) / 1000) / 60) / 60) / 24)!, UCUMUnits.Day),
294
+ // ("ms", DateTimePrecision.Hour) =>
295
+ // new CqlQuantity(Math.Truncate((((quantity.value ?? 0) / 1000) / 60) / 60)!, UCUMUnits.Hour),
296
+ // ("ms", DateTimePrecision.Minute) =>
297
+ // new CqlQuantity(Math.Truncate(((quantity.value ?? 0) / 1000) / 60)!, UCUMUnits.Minute),
298
+ // ("ms", DateTimePrecision.Second) =>
299
+ // new CqlQuantity(Math.Truncate((quantity.value ?? 0) / 1000)!, UCUMUnits.Second),
300
+ // (_,_) => quantity
301
+ // };
302
+ // }
285
303
}
286
304
}
0 commit comments