source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/M.cs@ 1172

Last change on this file since 1172 was 1146, checked in by Sam Habiel, 14 years ago

Initial Import of BMX4

File size: 7.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace IndianHealthService.BMXNet
6{
7 /// <summary>
8 /// Common M[umps] related function libraries implemented with static members.
9 /// </summary>
10 public static class M
11 {
12
13 #region Piece Functions
14
15 /// <summary>
16 /// M-like utility method to answer the number of pieces limited by aDelimiter. Corresponds to M's $L(STRING,DELIMITER)
17 /// </summary>
18 /// <param name="thePieces">Delimited input string e.g. one^two^three</param>
19 /// <param name="aDelimiter">Delimiter character or string, commonly ~ ^ or |</param>
20 /// <returns>Count of pieces in thePieces delimited by aDelimiter</returns>
21 /// <example>
22 /// <code>
23 /// String team="DOE,JOHN^SMITH, PETER^JONES, SALLY";
24 /// if (M.PieceLength(team,"~") >=5 ) {
25 /// this.PlayBasketBall();
26 /// }
27 /// else
28 /// {
29 /// this.PlayTrackAndField();
30 /// }
31 /// </code>
32 /// </example>
33 public static int PieceLength(string thePieces, string aDelimiter)
34 {
35 char[] cDelim = aDelimiter.ToCharArray();
36 string[] cSplit = thePieces.Split(cDelim);
37 return cSplit.GetLength(0);
38 }
39
40
41 /// <summary>
42 /// M-like utility method to answer the piece at a 1-based offset. Corresponds to M's $$Piece function
43 /// </summary>
44 /// <param name="thePieces">Delimited input string e.g. one^two^three</param>
45 /// <param name="aDelimiter">Delimiter character or string, commonly ~ ^ or |</param>
46 /// <param name="nthPiece">The 1-based index of the piece</param>
47 /// <returns>The piece at the index, or an empty string if the index is higher than the explicit number of pieces</returns>
48 /// <example>
49 /// <code>
50 /// String demographics="DOE,JOHN~555-55-5555~34~M";
51 /// int age=0;
52 /// if (int.TryParse(M.Piece(demographics,"~",3), out age)) {
53 /// System.Console.Writeln(M.Piece(demographics,"~",1)+" is "+ (age >= 18 ) ? "an adult" : "a child");
54 /// }
55 /// </code>
56 /// </example>
57 public static string Piece(string thePieces, string aDelimiter, int nthPiece)
58 {
59 try
60 {
61 char[] cDelim = aDelimiter.ToCharArray();
62 string[] cSplit = thePieces.Split(cDelim);
63 int nLength = cSplit.GetLength(0);
64 return ((nLength < nthPiece) || (nthPiece < 1)) ? "" : cSplit[nthPiece - 1];
65 }
66 catch (Exception bmxEx)
67 {
68 string sMessage = bmxEx.Message + bmxEx.StackTrace;
69 throw new BMXNetException(sMessage);
70 }
71
72 }
73
74 /// <summary>
75 /// M-like utility method to answer a delimited string of pieces between two 1-based offsets
76 /// </summary>
77 /// <param name="thePieces">Delimited input string e.g. one^two^three</param>
78 /// <param name="aDelimiter">Delimiter character or string, commonly ~ ^ or |</param>
79 /// <param name="nthStart">Starting 1-based index of the pieces to copy from</param>
80 /// <param name="mthEnd">Ending 1-based index of the pieces to copy to</param>
81 /// <returns>The delimited string from start to end, including delimitered blanks for indexes higher than the explicit number of pieces</returns>
82 public static string Piece(string thePieces, string aDelimiter, int nthStart, int mthEnd)
83 {
84 try
85 {
86 if (nthStart < 0)
87 nthStart = 1;
88
89 if (mthEnd< nthStart)
90 return "";
91
92 if (mthEnd == nthStart)
93 return Piece(thePieces, aDelimiter, nthStart);
94
95 char[] cDelim = aDelimiter.ToCharArray();
96 string[] cSplit = thePieces.Split(cDelim);
97 int nLength = cSplit.GetLength(0);
98 if ((nLength < nthStart) || (nthStart < 1))
99 return "";
100
101 //nNumber = 1-based index of the starting element to return
102 //nLength = count of elements
103 //nEnd = 1-based index of last element to return
104 //nCount = number of elements past nNumber to return
105
106 //convert nNumber to 0-based index:
107 nthStart--;
108
109 //convert nEnd to 0-based index;
110 mthEnd--;
111
112 //Calculate nCount;
113 int nCount = mthEnd - nthStart + 1;
114
115 //Adjust nCount for number of elements
116 if (nCount + nthStart >= nLength)
117 {
118 nCount = nLength - nthStart;
119 }
120
121 string sRet = string.Join(aDelimiter, cSplit, nthStart, nCount);
122 return sRet;
123 }
124 catch (Exception bmxEx)
125 {
126 string sMessage = bmxEx.Message + bmxEx.StackTrace;
127 throw new BMXNetException(sMessage);
128 }
129 }
130
131 #endregion Piece Functions
132
133 /// <summary>
134 /// M-like utility method to convert from a DateTime to the FileMan data/time format
135 /// </summary>
136 /// <param name="aDateTime">DateTime to convert</param>
137 /// <returns>String representing aDateTime in FileMan format</returns>
138 /// <exception cref="Exception">Exception thrown in conversion fails</exception>
139 public static string DateToFileMan(DateTime aDateTime)
140 {
141 int yearsSince1700 = (int)aDateTime.Year - 1700;
142 TimeSpan timeOfDay = aDateTime.TimeOfDay;
143
144 if (timeOfDay.TotalSeconds == 0)
145 {
146 return yearsSince1700.ToString("000") + aDateTime.ToString("MMdd");
147 }
148 else
149 {
150 return yearsSince1700.ToString("000") + ((timeOfDay.Seconds == 0) ? aDateTime.ToString("MMdd.HHmm") : aDateTime.ToString("MMdd.HHmmss"));
151 }
152 }
153
154
155 /// <summary>
156 /// M-like utility method to convert from a FileMan date/time formatted String to a .NET DateTime
157 /// </summary>
158 /// <param name="aString">date/time formated FileMan string</param>
159 /// <returns>.NET DateTime object from parsing input FileMan string</returns>
160 /// <exception cref="Exception">Exception thrown in conversion fails</exception>
161 public static DateTime DateFromFileMan(string aString)
162 {
163 String[] peices = aString.Split(new char[] { '.' });
164
165 if (peices.Length == 1)
166 {
167 return new DateTime(
168 int.Parse(peices[0].Substring(0, 3)) + 1700,
169 int.Parse(peices[0].Substring(3, 2)),
170 int.Parse(peices[0].Substring(5, 2)));
171 }
172 else if (peices.Length >= 2)
173 {
174 String timePiece = peices[1].PadRight(6, '0');
175
176 return new DateTime(
177 int.Parse(peices[0].Substring(0, 3)) + 1700,
178 int.Parse(peices[0].Substring(3, 2)),
179 int.Parse(peices[0].Substring(5, 2)),
180 int.Parse(timePiece.Substring(0, 2)),
181 int.Parse(timePiece.Substring(2, 2)),
182 int.Parse(timePiece.Substring(4, 2)));
183 }
184 else
185 {
186 throw new Exception("Invalid FileMan DateTime");
187 }
188 }
189
190 }
191}
Note: See TracBrowser for help on using the repository browser.