-
Notifications
You must be signed in to change notification settings - Fork 156
/
TSComponentOne.cs
349 lines (314 loc) · 12.4 KB
/
TSComponentOne.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/// <summary> The contents of this file are subject to the Mozilla Public License Version 1.1
/// (the "License"); you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at http://www.mozilla.org/MPL/
/// Software distributed under the License is distributed on an "AS IS" basis,
/// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
/// specific language governing rights and limitations under the License.
///
/// The Original Code is "TSComponentOne.java". Description:
/// "Represents an HL7 timestamp, which is related to the HL7 TS type."
///
/// The Initial Developer of the Original Code is University Health Network. Copyright (C)
/// 2005. All Rights Reserved.
///
/// Contributor(s): ______________________________________.
///
/// Alternatively, the contents of this file may be used under the terms of the
/// GNU General Public License (the “GPL”), in which case the provisions of the GPL are
/// applicable instead of those above. If you wish to allow use of your version of this
/// file only under the terms of the GPL and not to allow others to use your version
/// of this file under the MPL, indicate your decision by deleting the provisions above
/// and replace them with the notice and other provisions required by the GPL License.
/// If you do not delete the provisions above, a recipient may use your version of
/// this file under either the MPL or the GPL.
/// </summary>
using System;
using System.Globalization;
using System.Threading;
using NHapi.Base.Model;
namespace NHapi.Base.Model.Primitive
{
/// <summary> Represents an HL7 timestamp, which is related to the HL7 TS type. In version 2.5,
/// TS is a composite type. The first component is type DTM, which corresponds to this class
/// (actually Model.v25.datatype.DTM inherits from this class at time of writing). In HL7 versions
/// 2.2-2.4, it wasn't perfectly clear whether TS was composite or primitive. HAPI interprets
/// it as composite, with the first component having a type that isn't defined by HL7, and we call
/// this type TSComponentOne. In v2.1, TS is primitive, and corresponds one-to-one with this class.
///
/// </summary>
/// <author> <a href="mailto:neal.acharya@uhn.on.ca">Neal Acharya</a>
/// </author>
/// <author> <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
/// </author>
/// <version> $Revision: 1.4 $ updated on $Date: 2005/06/14 20:09:39 $ by $Author: bryan_tripp $
/// </version>
public class TSComponentOne : AbstractPrimitive
{
private CommonTS Detail
{
get
{
if (myDetail == null)
{
myDetail = new CommonTS(Value);
}
return myDetail;
}
}
/// <throws> DataTypeException if the value is incorrectly formatted and either validation is </throws>
/// <summary> enabled for this primitive or detail setters / getters have been called, forcing further
/// parsing.
/// </summary>
public override String Value
{
get
{
String result = base.Value;
if (myDetail != null)
{
result = myDetail.Value;
}
return result;
}
set
{
base.Value = value;
if (myDetail != null)
{
myDetail.Value = value;
}
}
}
/// <seealso cref="CommonTS.setOffset(int)">
/// </seealso>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual int Offset
{
set { Detail.Offset = value; }
}
/// <summary> Returns the year as an integer.</summary>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual int Year
{
get { return Detail.Year; }
}
/// <summary> Returns the month as an integer.</summary>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual int Month
{
get { return Detail.Month; }
}
/// <summary> Returns the day as an integer.</summary>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual int Day
{
get { return Detail.Day; }
}
/// <summary> Returns the hour as an integer.</summary>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual int Hour
{
get { return Detail.Hour; }
}
/// <summary> Returns the minute as an integer.</summary>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual int Minute
{
get { return Detail.Minute; }
}
/// <summary> Returns the second as an integer.</summary>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual int Second
{
get { return Detail.Second; }
}
/// <summary> Returns the fractional second value as a float.</summary>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual float FractSecond
{
get { return Detail.FractSecond; }
}
/// <summary> Returns the GMT offset value as an integer.</summary>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual int GMTOffset
{
get { return Detail.GMTOffset; }
/// <summary>Returns the name of the type (used in XML encoding and profile checking) </summary>
// public String getName() {
// return "NM"; //seems to be called an NM in XML representation prior to 2.5
// }
}
private CommonTS myDetail;
/// <param name="theMessage">message to which this Type belongs
/// </param>
public TSComponentOne(IMessage theMessage)
: base(theMessage)
{
}
public TSComponentOne(IMessage theMessage, string description)
: base(theMessage, description)
{
}
/// <seealso cref="CommonTS.setDatePrecision(int, int, int)">
/// </seealso>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual void setDatePrecision(int yr, int mnth, int dy)
{
Detail.setDatePrecision(yr, mnth, dy);
}
/// <seealso cref="CommonTS.setDateMinutePrecision(int, int, int, int, int)">
/// </seealso>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual void setDateMinutePrecision(int yr, int mnth, int dy, int hr, int min)
{
Detail.setDateMinutePrecision(yr, mnth, dy, hr, min);
}
/// <seealso cref="CommonTS.setDateSecondPrecision(int, int, int, int, int, float)">
/// </seealso>
/// <throws> DataTypeException if the value is incorrectly formatted. If validation is enabled, this </throws>
/// <summary> exception should be thrown at setValue(), but if not, detailed parsing may be deferred until
/// this method is called.
/// </summary>
public virtual void setDateSecondPrecision(int yr, int mnth, int dy, int hr, int min, float sec)
{
Detail.setDateSecondPrecision(yr, mnth, dy, hr, min, sec);
}
/// <summary>
/// Used for setting the format of a long date (Year, Month, Day, Hour, Minute)
/// </summary>
protected virtual string LongDateTimeFormat
{
get { return "yyyyMMddHHmm"; }
}
/// <summary>
/// Used for setting the format of a long date (Year, Month, Day, Hour, Minute, Second)
/// </summary>
protected virtual string LongDateTimeFormatWithSecond
{
get { return "yyyyMMddHHmmss"; }
}
/// <summary>
/// Used for setting the format of a long date (Year, Month, Day, Hour, Minute, Second, Offset from GMT)
/// </summary>
protected virtual string LongDateTimeFormatWithOffset
{
get { return "yyyyMMddHHmmsszzz"; }
}
/// <summary>
/// Used for setting the format of a long date (Year, Month, Day, Hour, Minute, Second, Fraction of second)
/// </summary>
protected virtual string LongDateTimeFormatWithFactionOfSecond
{
get { return "yyyyMMddHHmmss.FFFF"; }
}
/// <summary>
/// Used for setting the format of a short date (Year, Month, Day)
/// </summary>
protected virtual string ShortDateTimeFormat
{
get { return "yyyyMMdd"; }
}
/// <summary>
/// Get the value as a date. Throws hl7Exception if error.
/// </summary>
/// <returns>Data/Time</returns>
public virtual DateTime GetAsDate()
{
try
{
string[] dateFormats = new string[] {LongDateTimeFormat, ShortDateTimeFormat, LongDateTimeFormatWithSecond, LongDateTimeFormatWithOffset };
DateTime val = DateTime.MinValue;
CultureInfo culture = Thread.CurrentThread.CurrentCulture;
if (Value != null && Value.Length > 0)
val = DateTime.ParseExact(Value, dateFormats, culture, DateTimeStyles.NoCurrentDateDefault);
return val;
}
catch (Exception)
{
throw new HL7Exception("Could not get field as dateTime");
}
}
/// <summary>
/// Set the value as a short date
/// </summary>
/// <param name="value"></param>
public virtual void SetShortDate(DateTime value)
{
Set(value, ShortDateTimeFormat);
}
/// <summary>
/// Set the value as a long date
/// </summary>
/// <param name="value"></param>
public virtual void SetLongDate(DateTime value)
{
Set(value, LongDateTimeFormat);
}
/// <summary>
/// Set the value as a lond date with second
/// </summary>
/// <param name="value"></param>
public virtual void SetLongDateWithSecond(DateTime value)
{
Set(value, LongDateTimeFormatWithSecond);
}
/// <summary>
/// Set the value as a lond date with fraction of second
/// </summary>
/// <param name="value"></param>
public virtual void SetLongDateWithFractionOfSecond(DateTime value)
{
Set(value, LongDateTimeFormatWithFactionOfSecond);
}
/// <summary>
/// Sets the value (to the format specified) using a date.
/// </summary>
/// <param name="value">Valid date/time</param>
/// <param name="format">The format to set the value (yyyyMMdd, etc)</param>
public virtual void Set(DateTime value, string format)
{
try
{
Value = value.ToString(format);
}
catch (FormatException)
{
throw new HL7Exception("Could not format the date " + value + " to a long date. Format must be " +
LongDateTimeFormat);
}
}
}
}