5 // Chris J Breisch (cjbreisch@altavista.net)
7 // (C) 2002 Chris J Breisch
11 // Copyright (c) 2002-2003 Mainsoft Corporation.
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Runtime.InteropServices;
36 using System.ComponentModel;
37 using System.Globalization;
39 namespace Microsoft.VisualBasic
\r
41 [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute]
42 sealed public class DateAndTime {
44 private DateAndTime ()
46 //Nobody should see constructor
49 public static string DateString {
51 return DateTime.Today.ToString("MM-dd-yyyy");
55 string [] formats = { "M-d-yyyy", "M-d-y", "M/d/yyyy", "M/d/y" };
\r
58 DateTime dtToday = DateTime.ParseExact(value, formats,
59 DateTimeFormatInfo.CurrentInfo,
65 throw new InvalidCastException();
70 public static System.DateTime Today {
71 get { return DateTime.Today; }
73 // FIXME: This needs to use some OS specific code
74 // I've already written it for Windows
75 // and Unix won't be hard, but need an
76 // OS object from the compiler
77 // OS specific code needs to check permissions
78 // too, and throw an ArgumentOutOfRangeException
80 // DateTime dtNow = DateTime.Now;
82 // SysTime.LocalTime = new DateTime(value.Year,
83 // value.Month, value.Day, dtNow.Hour,
84 // dtNow.Minute, dtNow.Second, dtNow.Millisecond);
85 throw new NotImplementedException();
89 public static double Timer {
91 DateTime DTNow = DateTime.Now;
93 return DTNow.Hour * 3600 + DTNow.Minute * 60 +
94 DTNow.Second + DTNow.Millisecond /
99 public static System.DateTime Now {
100 get { return DateTime.Now; }
103 public static System.DateTime TimeOfDay {
105 TimeSpan TSpan = DateTime.Now.TimeOfDay;
107 return new DateTime(1, 1, 1, TSpan.Hours,
108 TSpan.Minutes, TSpan.Seconds,
112 // FIXME: This needs to use some OS specific code
113 // I've already written it for Windows
114 // and Unix won't be hard, but need an
115 // OS object from the compiler
116 // OS specific code needs to check permissions
117 // too, and throw an ArgumentOutOfRangeException
119 // DateTime dtToday = DateTime.Today;
121 // SysTime.LocalTime = new DateTime(dtToday.Year,
122 // dtToday.Month, dtToday.Day, value.Hour,
123 // value.Minute, value.Second, value.Millisecond);
124 throw new NotImplementedException();
128 public static string TimeString {
129 get { return DateTime.Now.ToString("HH:mm:ss"); }
131 string format = "HH:mm:ss";
\r
134 DateTime dtToday = DateTime.ParseExact(value, format,
135 DateTimeFormatInfo.CurrentInfo,
136 DateTimeStyles.None);
141 throw new InvalidCastException();
147 public static System.DateTime DateAdd (DateInterval Interval,
148 double Number, System.DateTime DateValue) {
151 case DateInterval.Year:
152 return DateValue.AddYears((int)Number);
153 case DateInterval.Quarter:
154 return DateValue.AddMonths((int)Number * 3);
155 case DateInterval.Month:
156 return DateValue.AddMonths((int)Number);
157 case DateInterval.WeekOfYear:
158 return DateValue.AddDays(Number * 7);
159 case DateInterval.Day:
160 case DateInterval.DayOfYear:
161 case DateInterval.Weekday:
162 return DateValue.AddDays(Number);
163 case DateInterval.Hour:
164 return DateValue.AddHours(Number);
165 case DateInterval.Minute:
166 return DateValue.AddMinutes(Number);
167 case DateInterval.Second:
168 return DateValue.AddSeconds(Number);
170 throw new ArgumentException();
174 private static DayOfWeek GetDayRule(FirstDayOfWeek StartOfWeek, DayOfWeek DayRule)
176 switch (StartOfWeek) {
177 case FirstDayOfWeek.System:
179 case FirstDayOfWeek.Sunday:
180 return DayOfWeek.Sunday;
181 case FirstDayOfWeek.Monday:
182 return DayOfWeek.Monday;
183 case FirstDayOfWeek.Tuesday:
184 return DayOfWeek.Tuesday;
185 case FirstDayOfWeek.Wednesday:
186 return DayOfWeek.Wednesday;
187 case FirstDayOfWeek.Thursday:
188 return DayOfWeek.Thursday;
189 case FirstDayOfWeek.Friday:
190 return DayOfWeek.Friday;
191 case FirstDayOfWeek.Saturday:
192 return DayOfWeek.Saturday;
194 throw new ArgumentException();
198 private static CalendarWeekRule GetWeekRule(FirstWeekOfYear StartOfYear, CalendarWeekRule WeekRule)
200 switch (StartOfYear) {
201 case FirstWeekOfYear.System:
203 case FirstWeekOfYear.FirstFourDays:
204 return CalendarWeekRule.FirstFourDayWeek;
205 case FirstWeekOfYear.FirstFullWeek:
206 return CalendarWeekRule.FirstFullWeek;
207 case FirstWeekOfYear.Jan1:
208 return CalendarWeekRule.FirstDay;
210 throw new ArgumentException();
214 public static long DateDiff (DateInterval Interval,
215 System.DateTime Date1, System.DateTime Date2,
216 [Optional] [DefaultValue(FirstDayOfWeek.Sunday)]
217 FirstDayOfWeek StartOfWeek,
218 [Optional] [DefaultValue(FirstWeekOfYear.Jan1)]
219 FirstWeekOfYear StartOfYear)
225 CalendarWeekRule WeekRule = CalendarWeekRule.FirstDay;
226 DayOfWeek DayRule = DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;
227 Calendar CurCalendar = CultureInfo.CurrentCulture.Calendar;
230 case DateInterval.Year:
231 return Date2.Year - Date1.Year;
232 case DateInterval.Quarter:
233 YearQuarters = (Date2.Year - Date1.Year) * 4;
234 return Date2.Month / 4 - Date1.Month / 4 + YearQuarters;
235 case DateInterval.Month:
236 YearMonths = (Date2.Year - Date1.Year) * 12;
237 return Date2.Month - Date1.Month + YearMonths;
238 case DateInterval.WeekOfYear:
239 YearWeeks = (Date2.Year - Date1.Year) * 53;
240 DayRule = GetDayRule(StartOfWeek, DayRule);
241 WeekRule = GetWeekRule(StartOfYear, WeekRule);
242 if (CurCalendar == null)
243 throw new NotImplementedException("Looks like CultureInfo.CurrentCulture.Calendar is still returning null");
244 return CurCalendar.GetWeekOfYear(Date2, WeekRule, DayRule) -
245 CurCalendar.GetWeekOfYear(Date1,WeekRule, DayRule) +
247 case DateInterval.Weekday:
248 return ((TimeSpan)(Date2.Subtract(Date1))).Days / 7;
249 case DateInterval.DayOfYear:
250 case DateInterval.Day:
251 return ((TimeSpan)(Date2.Subtract(Date1))).Days;
252 case DateInterval.Hour:
253 return (int)((TimeSpan)(Date2.Subtract(Date1))).TotalHours;
254 case DateInterval.Minute:
255 return (int)((TimeSpan)(Date2.Subtract(Date1))).TotalMinutes;
256 case DateInterval.Second:
257 return (int)((TimeSpan)(Date2.Subtract(Date1))).TotalSeconds;
259 throw new ArgumentException();
263 private static int ConvertWeekDay(DayOfWeek Day, int Offset)
268 int Weekday = (int)Day + 1 - Offset;
277 int Weekday = (int)Day + Offset;
283 switch((DayOfWeek)Weekday) {
284 case DayOfWeek.Sunday:
285 return (int)FirstDayOfWeek.Sunday;
286 case DayOfWeek.Monday:
287 return (int)FirstDayOfWeek.Monday;
288 case DayOfWeek.Tuesday:
289 return (int)FirstDayOfWeek.Tuesday;
290 case DayOfWeek.Wednesday:
291 return (int)FirstDayOfWeek.Wednesday;
292 case DayOfWeek.Thursday:
293 return (int)FirstDayOfWeek.Thursday;
294 case DayOfWeek.Friday:
295 return (int)FirstDayOfWeek.Friday;
296 case DayOfWeek.Saturday:
297 return (int)FirstDayOfWeek.Saturday;
299 throw new ArgumentException();
304 public static int DatePart
306 Microsoft.VisualBasic.DateInterval Interval,
307 System.DateTime DateValue,
308 [Optional] [DefaultValue(FirstDayOfWeek.Sunday)]
309 FirstDayOfWeek StartOfWeek,
310 [Optional] [DefaultValue(FirstWeekOfYear.Jan1)]
311 FirstWeekOfYear StartOfYear) {
313 CalendarWeekRule WeekRule = CalendarWeekRule.FirstDay;
314 DayOfWeek DayRule = DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;
315 Calendar CurCalendar = CultureInfo.CurrentCulture.Calendar;
318 case DateInterval.Year:
319 return DateValue.Year;
320 case DateInterval.Quarter:
321 return DateValue.Month / 4 + 1;
322 case DateInterval.Month:
323 return DateValue.Month;
324 case DateInterval.WeekOfYear:
325 DayRule = GetDayRule(StartOfWeek, DayRule);
326 WeekRule = GetWeekRule(StartOfYear, WeekRule);
327 return CurCalendar.GetWeekOfYear(DateValue, WeekRule, DayRule);
328 case DateInterval.Weekday:
329 return ConvertWeekDay(DateValue.DayOfWeek, (int)StartOfWeek);
330 case DateInterval.DayOfYear:
331 return DateValue.DayOfYear;
332 case DateInterval.Day:
333 return DateValue.Day;
334 case DateInterval.Hour:
335 return DateValue.Hour;
336 case DateInterval.Minute:
337 return DateValue.Minute;
338 case DateInterval.Second:
339 return DateValue.Second;
341 throw new ArgumentException();
345 private static DateInterval DateIntervalFromString(string Interval)
349 return DateInterval.Year;
351 return DateInterval.Quarter;
353 return DateInterval.Month;
355 return DateInterval.WeekOfYear;
357 return DateInterval.Weekday;
359 return DateInterval.Day;
361 return DateInterval.DayOfYear;
363 return DateInterval.Hour;
365 return DateInterval.Minute;
367 return DateInterval.Second;
369 throw new ArgumentException();
373 public static System.DateTime DateAdd (string Interval,
374 double Number, System.Object DateValue)
376 if (DateValue == null) {
377 throw new ArgumentNullException("DateValue", "Value can not be null.");
379 if (!(DateValue is DateTime)) {
380 throw new InvalidCastException();
383 return DateAdd(DateIntervalFromString(Interval), Number, (DateTime)DateValue);
386 public static System.Int64 DateDiff (string Interval,
387 System.Object Date1, System.Object Date2,
388 [Optional] [DefaultValue(FirstDayOfWeek.Sunday)]
389 FirstDayOfWeek StartOfWeek,
390 [Optional] [DefaultValue(FirstWeekOfYear.Jan1)]
391 FirstWeekOfYear StartOfYear)
394 throw new ArgumentNullException("Date1", "Value can not be null.");
397 throw new ArgumentNullException("Date2", "Value can not be null.");
399 if (!(Date1 is DateTime)) {
400 throw new InvalidCastException();
402 if (!(Date2 is DateTime)) {
403 throw new InvalidCastException();
406 return DateDiff(DateIntervalFromString(Interval), (DateTime)Date1,
407 (DateTime)Date2, StartOfWeek, StartOfYear);
411 public static System.Int32 DatePart (string Interval,
412 System.Object DateValue,
413 [Optional] [DefaultValue(FirstDayOfWeek.Sunday)]
414 FirstDayOfWeek StartOfWeek,
415 [Optional] [DefaultValue(FirstWeekOfYear.Jan1)]
416 FirstWeekOfYear StartOfYear)
418 if (DateValue == null) {
419 throw new ArgumentNullException("DateValue", "Value can not be null.");
421 if (!(DateValue is DateTime)) {
422 throw new InvalidCastException();
426 return DatePart(DateIntervalFromString(Interval),
427 (DateTime)DateValue, StartOfWeek, StartOfYear);
430 public static System.DateTime DateSerial (int Year, int Month, int Day)
435 Year = Year + DateTime.Now.Year;
436 else if (Year >= 0 && Year <= 29)
438 else if(Year >= 30 && Year <= 99)
441 date = new DateTime(Year, 1, 1);
443 date = date.AddMonths(Month - 1);
445 date = date.AddDays(Day - 1);
450 public static System.DateTime TimeSerial (int Hour, int Minute, int Second)
455 if (Minute == 0 && Hour == 0)
457 else if (Minute == 0){
467 else if(Second > 59){
480 else if (Minute > 59){
487 else if (Hour > 23) {
492 return new DateTime(1, 1, day, Hour, Minute, Second);
495 public static System.DateTime DateValue (string StringDate)
497 string[] expectedFormats = {"D", "d", "G", "g", "f" ,"F", "m", "M", "r", "R",
498 "s", "T", "t", "U", "u", "Y", "y",
499 "MMM dd, yy", "MMMM dd, yy", "MMM dd, yyyy", "MMMM dd, yyyy"};
502 return DateTime.ParseExact(StringDate, expectedFormats,
503 System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat,
504 System.Globalization.DateTimeStyles.NoCurrentDateDefault);
506 catch (FormatException exception) {
507 throw new InvalidCastException(null, exception);
511 public static System.DateTime TimeValue (string StringTime)
514 return DateTime.MinValue + DateTime.Parse(StringTime).TimeOfDay;
515 } catch (FormatException exception) {
516 throw new InvalidCastException(null, exception);
520 public static int Year (System.DateTime DateValue)
522 return DateValue.Year;
525 public static int Month (System.DateTime DateValue)
527 return DateValue.Month;
530 public static int Day (System.DateTime DateValue)
532 return DateValue.Day;
535 public static int Hour (System.DateTime TimeValue)
537 return TimeValue.Hour;
540 public static int Minute (System.DateTime TimeValue)
542 return TimeValue.Minute;
545 public static int Second (System.DateTime TimeValue)
547 return TimeValue.Second;
550 public static int Weekday (System.DateTime DateValue,
551 [Optional] [DefaultValue(FirstDayOfWeek.Sunday)]
552 FirstDayOfWeek StartOfWeek)
554 return DatePart(DateInterval.Weekday, DateValue, StartOfWeek, FirstWeekOfYear.System);
557 public static System.String MonthName (int Month,
558 [Optional] [DefaultValue(false)] bool Abbreviate)
560 if (Month < 1 || Month > 13) {
561 throw new ArgumentException();
564 return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(Month);
567 return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Month);
571 public static System.String WeekdayName (int Weekday,
572 [Optional] [DefaultValue(false)] System.Boolean Abbreviate,
573 [Optional] [DefaultValue(FirstDayOfWeek.System)]
574 FirstDayOfWeek FirstDayOfWeekValue)
576 if (Weekday < 1 || Weekday > 7) {
577 throw new ArgumentException();
579 Weekday += (int)FirstDayOfWeekValue;
584 return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedDayName((DayOfWeek)Weekday);
587 return CultureInfo.CurrentCulture.DateTimeFormat.GetDayName((DayOfWeek)Weekday);