1 /*
2  *
3  *  Copyright (C) 2002-2020, OFFIS e.V.
4  *  All rights reserved.  See COPYRIGHT file for details.
5  *
6  *  This software and supporting documentation were developed by
7  *
8  *    OFFIS e.V.
9  *    R&D Division Health
10  *    Escherweg 2
11  *    D-26121 Oldenburg, Germany
12  *
13  *
14  *  Module:  dcmdata
15  *
16  *  Author:  Joerg Riesmeier
17  *
18  *  Purpose: test program for classes DcmDate, DcmTime and DcmDateTime
19  *
20  */
21 
22 
23 #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
24 
25 #include "dcmtk/ofstd/oftest.h"
26 #include "dcmtk/dcmdata/dcvrda.h"
27 #include "dcmtk/dcmdata/dcvrtm.h"
28 #include "dcmtk/dcmdata/dcvrdt.h"
29 #include "dcmtk/dcmdata/dcdeftag.h"
30 #include "dcmtk/dcmdata/dcdict.h"
31 
32 #define CHECK_EQUAL(string) do { \
33     strstream << OFStringStream_ends; \
34     OFSTRINGSTREAM_GETOFSTRING(strstream, res); \
35     OFCHECK_EQUAL(res, string); \
36     strstream.clear(); \
37     strstream.str(""); \
38 } while (0)
39 #define CHECK_STREAM_EQUAL(val, string) do { \
40     strstream << val; \
41     CHECK_EQUAL(string); \
42     strstream.clear(); \
43     strstream.str(""); \
44 } while(0)
45 
OFTEST(dcmdata_dateTime)46 OFTEST(dcmdata_dateTime)
47 {
48     // Make sure data dictionary is loaded
49     if (!dcmDataDict.isDictionaryLoaded())
50     {
51         OFCHECK_FAIL("no data dictionary loaded, check environment variable: " DCM_DICT_ENVIRONMENT_VARIABLE);
52         return;
53     }
54 
55     double timeZone;
56     OFDate dateVal;
57     OFTime timeVal;
58     OFDateTime dateTime;
59     OFString string;
60     DcmDate dcmDate(DCM_StudyDate);
61     DcmTime dcmTime(DCM_StudyTime);
62     DcmDateTime dcmDateTime(DCM_DateTime);
63     OFOStringStream strstream;
64 
65     // Determine the local time zone, needed because setOFTime loses the timezone
66     timeVal.setCurrentTime();
67     const double localTimeZone = timeVal.getTimeZone();
68 
69     OFDate curDateVal(2011, 5, 9);
70     OFTime curTimeVal(10, 35, 20, localTimeZone);
71     OFDateTime curDateTimeVal(curDateVal, curTimeVal);
72 
73     dcmDate.setOFDate(curDateVal);
74     dcmDate.print(strstream);
75     CHECK_EQUAL("(0008,0020) DA [20110509]                               #   8, 1 StudyDate\n");
76     OFCHECK(dcmDate.getOFDate(dateVal).good());
77     OFCHECK_EQUAL(dateVal, curDateVal);
78 
79     dcmTime.setOFTime(curTimeVal);
80     dcmTime.print(strstream);
81     CHECK_EQUAL("(0008,0030) TM [103520]                                 #   6, 1 StudyTime\n");
82     OFCHECK(dcmTime.getOFTime(timeVal).good());
83     OFCHECK_EQUAL(timeVal, curTimeVal);
84 
85     dcmDateTime.setOFDateTime(curDateTimeVal);
86     dcmDateTime.print(strstream);
87     CHECK_EQUAL("(0040,a120) DT [20110509103520]                         #  14, 1 DateTime\n");
88     OFCHECK(dcmDateTime.getOFDateTime(dateTime).good());
89     OFCHECK_EQUAL(dateTime, curDateTimeVal);
90     OFCHECK(dateTime.getISOFormattedDateTime(string, OFTrue /*seconds*/, OFTrue /*fraction*/, OFFalse /*timeZone*/, OFFalse /*delimiter*/));
91     OFCHECK_EQUAL(string, "20110509103520.000000");
92 
93     dcmTime.putString("12");
94     dcmTime.print(strstream);
95     CHECK_EQUAL("(0008,0030) TM [12]                                     #   2, 1 StudyTime\n");
96     OFCHECK(dcmTime.getOFTime(timeVal).good());
97     CHECK_STREAM_EQUAL(timeVal, "12:00:00");
98 
99     dcmTime.putString("1203");
100     dcmTime.print(strstream);
101     CHECK_EQUAL("(0008,0030) TM [1203]                                   #   4, 1 StudyTime\n");
102     OFCHECK(dcmTime.getOFTime(timeVal).good());
103     CHECK_STREAM_EQUAL(timeVal, "12:03:00");
104 
105     dcmTime.putString("120315");
106     dcmTime.print(strstream);
107     CHECK_EQUAL("(0008,0030) TM [120315]                                 #   6, 1 StudyTime\n");
108     OFCHECK(dcmTime.getOFTime(timeVal).good());
109     CHECK_STREAM_EQUAL(timeVal, "12:03:15");
110     dcmTime.putString("120301.99");
111     dcmTime.print(strstream);
112     CHECK_EQUAL("(0008,0030) TM [120301.99]                              #  10, 1 StudyTime\n");
113     OFCHECK(dcmTime.getOFTime(timeVal).good());
114     timeVal.getISOFormattedTime(string, OFTrue /*seconds*/, OFTrue /*fraction*/, OFFalse /*timeZone*/);
115     OFCHECK_EQUAL(string, "12:03:01.990000");
116 
117     dcmTime.putString("12:03");
118     dcmTime.print(strstream);
119     CHECK_EQUAL("(0008,0030) TM [12:03]                                  #   6, 1 StudyTime\n");
120     OFCHECK(dcmTime.getOFTime(timeVal).good());
121     CHECK_STREAM_EQUAL(timeVal, "12:03:00");
122     dcmTime.putString("12:03:15");
123     dcmTime.print(strstream);
124     CHECK_EQUAL("(0008,0030) TM [12:03:15]                               #   8, 1 StudyTime\n");
125     OFCHECK(dcmTime.getOFTime(timeVal).good());
126     CHECK_STREAM_EQUAL(timeVal, "12:03:15");
127 
128     OFCHECK(DcmTime::getTimeZoneFromString("+0030", timeZone).good());
129     OFCHECK_EQUAL(timeZone, 0.5);
130     OFCHECK(DcmTime::getTimeZoneFromString("+1130", timeZone).good());
131     OFCHECK_EQUAL(timeZone, 11.5);
132     OFCHECK(DcmTime::getTimeZoneFromString("-0100", timeZone).good());
133     OFCHECK_EQUAL(timeZone, -1);
134     OFCHECK(DcmTime::getTimeZoneFromString("-0530", timeZone).good());
135     OFCHECK_EQUAL(timeZone, -5.5);
136     OFCHECK(DcmTime::getTimeZoneFromString("01130", timeZone).bad());
137     OFCHECK(DcmTime::getTimeZoneFromString("+100", timeZone).bad());
138     OFCHECK(DcmTime::getTimeZoneFromString("UTC+1", timeZone).bad());
139 
140     dcmDateTime.putString("200204101203+0500");
141     dcmDateTime.print(strstream);
142     CHECK_EQUAL("(0040,a120) DT [200204101203+0500]                      #  18, 1 DateTime\n");
143     OFCHECK(dcmDateTime.getOFDateTime(dateTime).good());
144     CHECK_STREAM_EQUAL(dateTime, "2002-04-10 12:03:00");
145 
146     dcmDateTime.putString("20020410");
147     dcmDateTime.print(strstream);
148     CHECK_EQUAL("(0040,a120) DT [20020410]                               #   8, 1 DateTime\n");
149     OFCHECK(dcmDateTime.getOFDateTime(dateTime).good());
150     dateTime.getISOFormattedDateTime(string, OFTrue /*seconds*/, OFFalse /*fraction*/, OFFalse /*timeZone*/);
151     OFCHECK_EQUAL(string, "2002-04-10 00:00:00");
152 }
153