Article 4S5JN Coded Smorgasbord: Driven to Substraction

Coded Smorgasbord: Driven to Substraction

by
Remy Porter
from The Daily WTF on (#4S5JN)

Deon (previously) has some good news. His contract at Initrode is over, and he's on his way out the door. But before he goes, he wants to share more of his pain with us.

You may remember that the StringManager class had a bunch of data type conversions to numbers and dates. Well guess what, there's also a DateManager class, which is another 1600 lines of methods to handle dates.

As you might expect, there are a pile of re-invented conversion and parsing methods which do the same thing as the built-in methods. But there's also utility methods to help us handle date-related operations.

public static int subStractFromCurrentDate(System.DateTime dateTimeParm) {//get nowSystem.DateTime now = System.DateTime.Now;//now compare daysint daysDifference = now.Day - dateTimeParm.Day;return daysDifference ;}

Fun fact: the Day property returns the day of the month. So this method might "subStract", but if these two dates fall in different months, we're going to get unexpected results.

One of the smaller string formatters included is this one:

public static string formatEnglishDate (System.DateTime inputDateTime) {Hashtable _monthsInEnglishByMonthNumber = new Hashtable();_monthsInEnglishByMonthNumber[1] = "January";_monthsInEnglishByMonthNumber[2] = "February";_monthsInEnglishByMonthNumber[3] = "March";_monthsInEnglishByMonthNumber[4] = "April";_monthsInEnglishByMonthNumber[5] = "May";_monthsInEnglishByMonthNumber[6] = "June";_monthsInEnglishByMonthNumber[7] = "July";_monthsInEnglishByMonthNumber[8] = "August";_monthsInEnglishByMonthNumber[9] = "September";_monthsInEnglishByMonthNumber[10] = "October";_monthsInEnglishByMonthNumber[11] = "November";_monthsInEnglishByMonthNumber[12] = "December";StringBuilder _dateBldr = new StringBuilder();_dateBldr.Append(_monthsInEnglishByMonthNumber[inputDateTime.Month].ToString());_dateBldr.Append(" ");_dateBldr.Append(inputDateTime.Day.ToString());_dateBldr.Append(", ");_dateBldr.Append(inputDateTime.Year.ToString());return _dateBldr.ToString();}

Among all the bad things implied here, I really like that they used a Hashtable as an array.

 public static bool currentDateIsFirstBusinessDateOfTheMonth ( Hashtable inputHolidayHash ) { /* * If current date is not a business date, then it cannot * be the first business date of the month. */ DateTime _currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); _currentDate = new DateTime(2010, 5, 6); if ( _currentDate.DayOfWeek == DayOfWeek.Saturday || _currentDate.DayOfWeek == DayOfWeek.Sunday || inputHolidayHash[_currentDate] != null ) return false; /* * If current date is a business date, and if it is also * the first calendar date of the month, then the * current date is the first business date of the month. */ DateTime _firstDayOfTheMonth = _currentDate.AddDays(1 - _currentDate.Day); if (_firstDayOfTheMonth == _currentDate) return true; /* * If current date is a business date, but is not the 1st calendar * date of the month, and, if, in stepping back day by day * from the current date, we encounter a business day before * encountering the last calendar day of the preceding month, then the * current date is NOT the first business date of the month. */ DateTime _tempDate = _currentDate.AddDays(-1); while (_tempDate >= _firstDayOfTheMonth) { if ( _tempDate.DayOfWeek != DayOfWeek.Saturday && _tempDate.DayOfWeek != DayOfWeek.Sunday && inputHolidayHash[_tempDate] == null ) return false; _tempDate = _tempDate.AddDays(-1); } /* * * If current date is a business date, but is not the 1st calendar * date,and, if, in stepping back day by day from the current date, * we encounter no business day before encountering the * 1st calendar day of the month, then the current date * IS the first business date of the month. */ return true; }

This one has loads of comments, and honestly, I still have no idea what it's doing. If it's checking the current day, why does it need to cycle through other days? Why even ask that question, because clearly while debugging they hard-coded a testing date (new DateTime(2010, 5, 6)) and just left that in there.

I'm not the only one getting confused. Check out this comment:

 //@?? public static DateTime givenPeriodEndDateFindLastBusinessDateInPeriod ( DateTime inputPeriodEndDate , Hashtable inputHolidayHash ) { ... }

And if you're missing good old StringManager, don't worry, we use it here:

 /** * @description format date * */public static string formatYYYYMMDD (System.DateTime inputDateTime) {StringBuilder _bldr = new StringBuilder();_bldr.Append(inputDateTime.Year.ToString());_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Month.ToString(),"0", // Zero-Filltrue, // Fill from left2)); // String length_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Day.ToString(),"0", // Zero-Filltrue, // Fill from left2)); // String lengthreturn _bldr.ToString();}

And all of this is from just about the first third of the code. I'm trying to keep to shorter methods before posting the whole blob of ugly. So with that in mind, what if you wanted to compare dates?

public static DateComparison date1ComparedToDate2(DateTime inputDate1, DateTime inputDate2){if (inputDate1.Year > inputDate2.Year) return DateComparison.gt;if (inputDate1.Year < inputDate2.Year) return DateComparison.lt;if (inputDate1.DayOfYear > inputDate2.DayOfYear) return DateComparison.gt;if (inputDate1.DayOfYear < inputDate2.DayOfYear) return DateComparison.lt;return DateComparison.eq;}

Oh yeah, not only do we break the dates up into parts to compare them, we also have a custom enumerated type to represent the result of the comparison. And it's not just dates, we do it with times, too.

public static DateComparison timestamp1ComparedToTimestamp2(DateTime inputTimestamp1, DateTime inputTimestamp2){if (inputTimestamp1.Year > inputTimestamp2.Year) return DateComparison.gt;if (inputTimestamp1.Year < inputTimestamp2.Year) return DateComparison.lt;if (inputTimestamp1.DayOfYear > inputTimestamp2.DayOfYear) return DateComparison.gt;if (inputTimestamp1.DayOfYear < inputTimestamp2.DayOfYear) return DateComparison.lt;if (inputTimestamp1.Hour > inputTimestamp2.Hour) return DateComparison.gt;if (inputTimestamp1.Hour < inputTimestamp2.Hour) return DateComparison.lt;if (inputTimestamp1.Minute > inputTimestamp2.Minute) return DateComparison.gt;if (inputTimestamp1.Minute < inputTimestamp2.Minute) return DateComparison.lt;if (inputTimestamp1.Second > inputTimestamp2.Second) return DateComparison.gt;if (inputTimestamp1.Second < inputTimestamp2.Second) return DateComparison.lt;if (inputTimestamp1.Millisecond > inputTimestamp2.Millisecond) return DateComparison.gt;if (inputTimestamp1.Millisecond < inputTimestamp2.Millisecond) return DateComparison.lt;return DateComparison.eq;}

Initrode has a bright future with this product. Deon adds:

The contractor who is replacing me has rolled his own piece of software to try and replace Entity Framework because his version is "better" despite being written around a decade ago, so I'm sure he'll fit right in.

The future's so bright I've gotta wear shades.

Here's the full block, if you want to suffer through that:

/* Changes Log: @01 - 01/23/2009 - {some initials were here} - Improve performance of approval screens.*/using System;using System.Collections;using System.Globalization; using System.Text;namespace initrode.utilities{/// <summary>/// Summary description for DateManager./// </summary>public class DateManager{public enumDateComparison {gt = 1, eq = 0, lt = -1} public enum DateTimeParts { dateOnly , dateAndTime , dateTimeAndAMOrPM }/** @description return the days difference from today date* @parm int amount of days in the past* @return int the amount of days difference* **/public static int subStractFromCurrentDate(System.DateTime dateTimeParm) {//get nowSystem.DateTime now = System.DateTime.Now;//now compare daysint daysDifference = now.Day - dateTimeParm.Day;return daysDifference ;}/** * @description format date * */public static string format (System.DateTime dateTime, string format) {string dateFormat;dateFormat = dateTime.ToString(format,DateTimeFormatInfo.InvariantInfo);return dateFormat;} public static DateTime convertDateStringInSlashedFormatToDateTime ( string inputDateStringInSlashedFormat ) { inputDateStringInSlashedFormat = initrode.utilities.StringManager.StripWhitespace ( inputDateStringInSlashedFormat ); ArrayList _dateParts = initrode.utilities.StringManager.splitIntoArrayList ( inputDateStringInSlashedFormat ,@"/" ); if (_dateParts.Count != 3) return new DateTime(1900, 1, 1); string _monthString = initrode.utilities.StringManager.StripWhitespace ( _dateParts[0].ToString() ); if ( initrode.utilities.StringManager.IsValidNumber ( _monthString ) == false ) new DateTime(1900, 1, 1); string _dayString = initrode.utilities.StringManager.StripWhitespace ( _dateParts[1].ToString() ); if ( initrode.utilities.StringManager.IsValidNumber ( _dayString ) == false ) new DateTime(1900, 1, 1); string _yearString = initrode.utilities.StringManager.StripWhitespace ( _dateParts[2].ToString() ); if ( initrode.utilities.StringManager.IsValidNumber ( _yearString ) == false ) new DateTime(1900, 1, 1); return new DateTime ( Convert.ToInt32 ( _yearString ) , Convert.ToInt32 ( _monthString ) , Convert.ToInt32 ( _dayString ) ); } /** * @description format date * */public static string formatEnglishDate (System.DateTime inputDateTime) {Hashtable _monthsInEnglishByMonthNumber = new Hashtable();_monthsInEnglishByMonthNumber[1] = "January";_monthsInEnglishByMonthNumber[2] = "February";_monthsInEnglishByMonthNumber[3] = "March";_monthsInEnglishByMonthNumber[4] = "April";_monthsInEnglishByMonthNumber[5] = "May";_monthsInEnglishByMonthNumber[6] = "June";_monthsInEnglishByMonthNumber[7] = "July";_monthsInEnglishByMonthNumber[8] = "August";_monthsInEnglishByMonthNumber[9] = "September";_monthsInEnglishByMonthNumber[10] = "October";_monthsInEnglishByMonthNumber[11] = "November";_monthsInEnglishByMonthNumber[12] = "December";StringBuilder _dateBldr = new StringBuilder();_dateBldr.Append(_monthsInEnglishByMonthNumber[inputDateTime.Month].ToString());_dateBldr.Append(" ");_dateBldr.Append(inputDateTime.Day.ToString());_dateBldr.Append(", ");_dateBldr.Append(inputDateTime.Year.ToString());return _dateBldr.ToString();} public static bool currentDateIsFirstSaturdayOfTheMonth() { /* * If current date is not a business date, then it cannot * be the first business date of the month. */ DateTime _currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); if ( _currentDate.DayOfWeek == DayOfWeek.Saturday && _currentDate.Day <= 7 ) return true; return false; } public static bool currentDateIsFirstBusinessDateOfTheMonth ( Hashtable inputHolidayHash ) { /* * If current date is not a business date, then it cannot * be the first business date of the month. */ DateTime _currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); _currentDate = new DateTime(2010, 5, 6); if ( _currentDate.DayOfWeek == DayOfWeek.Saturday || _currentDate.DayOfWeek == DayOfWeek.Sunday || inputHolidayHash[_currentDate] != null ) return false; /* * If current date is a business date, and if it is also * the first calendar date of the month, then the * current date is the first business date of the month. */ DateTime _firstDayOfTheMonth = _currentDate.AddDays(1 - _currentDate.Day); if (_firstDayOfTheMonth == _currentDate) return true; /* * If current date is a business date, but is not the 1st calendar * date of the month, and, if, in stepping back day by day * from the current date, we encounter a business day before * encountering the last calendar day of the preceding month, then the * current date is NOT the first business date of the month. */ DateTime _tempDate = _currentDate.AddDays(-1); while (_tempDate >= _firstDayOfTheMonth) { if ( _tempDate.DayOfWeek != DayOfWeek.Saturday && _tempDate.DayOfWeek != DayOfWeek.Sunday && inputHolidayHash[_tempDate] == null ) return false; _tempDate = _tempDate.AddDays(-1); } /* * * If current date is a business date, but is not the 1st calendar * date,and, if, in stepping back day by day from the current date, * we encounter no business day before encountering the * 1st calendar day of the month, then the current date * IS the first business date of the month. */ return true; } //@?? public static DateTime givenPeriodEndDateFindLastBusinessDateInPeriod ( DateTime inputPeriodEndDate , Hashtable inputHolidayHash ) { if (inputHolidayHash[inputPeriodEndDate] == null) return inputPeriodEndDate; DateTime _tempDate = inputPeriodEndDate.AddDays(-1); while ( ( _tempDate.DayOfWeek == DayOfWeek.Saturday || _tempDate.DayOfWeek == DayOfWeek.Sunday ) || inputHolidayHash[_tempDate] != null ) { _tempDate = _tempDate.AddDays(-1); } return _tempDate; }/** * @description format date * */ public static string convertDateTimeToSQLDate ( DateTime inputDateTime ) { StringBuilder _sqlDateBldr = new StringBuilder(); _sqlDateBldr.AppendFormat ( "{0}/{1}/{2}" ,inputDateTime.Month.ToString() ,inputDateTime.Day.ToString() ,inputDateTime.Year.ToString() ); return _sqlDateBldr.ToString(); } /** * @description format date * */ public static string convertDateTimeToDB2Timestamp ( DateTime inputDateTime ) { StringBuilder _sqlDateBldr = new StringBuilder(); _sqlDateBldr.AppendFormat ( "{0}-{1}-{2}.{3}:{4}:{5}.{6}" , inputDateTime.Year.ToString() , initrode.utilities.StringManager.Fill ( inputDateTime.Month.ToString() ,"0" ,true //boolFromLeft ,2 ) , initrode.utilities.StringManager.Fill ( inputDateTime.Day.ToString() ,"0" ,true //boolFromLeft ,2 ) , initrode.utilities.StringManager.Fill ( inputDateTime.Hour.ToString() ,"0" ,true //boolFromLeft ,2 ) , initrode.utilities.StringManager.Fill ( inputDateTime.Minute.ToString() ,"0" ,true //boolFromLeft ,2 ) , initrode.utilities.StringManager.Fill ( inputDateTime.Second.ToString() , "0" , true //boolFromLeft , 2 ) , initrode.utilities.StringManager.Fill ( inputDateTime.Millisecond.ToString() , "0" , true //boolFromLeft , 2 ) ); return _sqlDateBldr.ToString(); }/** * @description format date * */public static string formatYYYYMMDD (System.DateTime inputDateTime) {StringBuilder _bldr = new StringBuilder();_bldr.Append(inputDateTime.Year.ToString());_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Month.ToString(),"0", // Zero-Filltrue, // Fill from left2)); // String length_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Day.ToString(),"0", // Zero-Filltrue, // Fill from left2)); // String lengthreturn _bldr.ToString();}//@01public static DateTime givenDateGetPeriodStartDate(DateTime inputDate1){if (inputDate1.Day > 15) return new DateTime(inputDate1.Year,inputDate1.Month,16);return new DateTime(inputDate1.Year,inputDate1.Month,1);}//@01public static DateTime givenDateGetPeriodEndDate(DateTime inputDate1){if (inputDate1.Day < 16) return new DateTime(inputDate1.Year,inputDate1.Month,15);inputDate1 = inputDate1.AddMonths(1);inputDate1 = new DateTime(inputDate1.Year,inputDate1.Month,1).AddDays(-1);return inputDate1;}/** * @description add days to a date * */public static DateTime addDays (DateTime dateTime, int days) {DateTime newDate = dateTime.AddDays(days);return newDate;}/** * @description get first day of the month from mm-dd-yyyy formatted string * **/public static DateTime getFirstDayofTheMonthFromMM_DD_YYYYFormattedString(string inputDateTimeInMM_DD_YYYYFormatString) {if (initrode.utilities.StringManager.IsValidDateInMM_DD_YYYYFormat(inputDateTimeInMM_DD_YYYYFormatString) == false){return initrode.utilities.DateManager.getFirstDayofTheCurrentMonth();}return initrode.utilities.DateManager.getFirstDayofTheMonth(Convert.ToDateTime(inputDateTimeInMM_DD_YYYYFormatString));}/** * @description get first day of the month * **/public static DateTime getFirstDayofTheMonth(System.DateTime inputDateTime) {return new DateTime(inputDateTime.Year,inputDateTime.Month,1);} public static DateTime convertTimestampOrDateInAnyStringFormatToDateTime ( string inputTimestampOrDateInAnyStringFormat ) { DateTime _returnDateTime = new DateTime(1900, 1, 1); ArrayList _splitDateTimeParts = new ArrayList(); inputTimestampOrDateInAnyStringFormat = initrode.utilities.StringManager.StripWhitespaceFromEnds ( inputTimestampOrDateInAnyStringFormat ); DateTimeParts _myDateTimeParts = DateTimeParts.dateOnly; string _timeParts = ""; string _amOrPMParts = ""; if (inputTimestampOrDateInAnyStringFormat.Contains(" ")) { _splitDateTimeParts = initrode.utilities.StringManager.splitIntoArrayList ( inputTimestampOrDateInAnyStringFormat , " " ); } else { _splitDateTimeParts.Add ( inputTimestampOrDateInAnyStringFormat ); } DateTime _dateOnly = new DateTime(1900, 1, 1); switch (_splitDateTimeParts.Count) { case 1: _myDateTimeParts = DateTimeParts.dateOnly; _dateOnly = initrode.utilities.DateManager.convertDateInAnyStringFormatIntoDateTime ( inputTimestampOrDateInAnyStringFormat ); break; case 2: _myDateTimeParts = DateTimeParts.dateAndTime; _dateOnly = initrode.utilities.DateManager.convertDateInAnyStringFormatIntoDateTime ( _splitDateTimeParts[0].ToString() ); _timeParts = initrode.utilities.StringManager.StripWhitespace ( _splitDateTimeParts[1].ToString() ); break; case 3: _myDateTimeParts = DateTimeParts.dateTimeAndAMOrPM; _dateOnly = initrode.utilities.DateManager.convertDateInAnyStringFormatIntoDateTime ( _splitDateTimeParts[0].ToString() ); _timeParts = initrode.utilities.StringManager.StripWhitespace ( _splitDateTimeParts[1].ToString() ); _amOrPMParts = initrode.utilities.StringManager.StripWhitespace ( _splitDateTimeParts[2].ToString() ).ToUpper(); break; default: return _returnDateTime; } if (_myDateTimeParts == DateTimeParts.dateOnly) return _dateOnly; if (_dateOnly == new DateTime(1900, 1, 1)) return _returnDateTime; if ( _myDateTimeParts == DateTimeParts.dateTimeAndAMOrPM && _amOrPMParts.CompareTo("AM") != 0 && _amOrPMParts.CompareTo("PM") != 0 ) return _returnDateTime; switch (_myDateTimeParts) { case DateTimeParts.dateAndTime: return initrode.utilities.DateManager.convertTimeInStringFormatAlongWithDateOnlyDateTimeIntoDateTime ( _timeParts //string inputStrTime , false //bool inputAMOrPMFormat , "" //string inputAMOrPM , _dateOnly //DateTime inputDateOnlyDateTime ); case DateTimeParts.dateTimeAndAMOrPM: return initrode.utilities.DateManager.convertTimeInStringFormatAlongWithDateOnlyDateTimeIntoDateTime ( _timeParts //string inputStrTime , true //bool inputAMOrPMFormat , _amOrPMParts //string inputAMOrPM , _dateOnly //DateTime inputDateOnlyDateTime ); } return _returnDateTime; } public static DateTime convertTimeInStringFormatAlongWithDateOnlyDateTimeIntoDateTime ( string inputStrTime ,bool inputAMOrPMFormat ,string inputAMOrPM ,DateTime inputDateOnlyDateTime ) { DateTime _returnDateTime = inputDateOnlyDateTime; if (inputStrTime.Contains(":") == false) return _returnDateTime; int _intMillisecondsPart = 0; if (inputStrTime.Contains(".") == true) { ArrayList _millisecondsAndTimeParts = initrode.utilities.StringManager.splitIntoArrayList ( inputStrTime ,@"." ); if (_millisecondsAndTimeParts.Count != 2) return _returnDateTime; string _strMillisecondsPart = initrode.utilities.StringManager.StripWhitespace ( _millisecondsAndTimeParts[1].ToString() ); if (initrode.utilities.StringManager.IsValidNumber(_strMillisecondsPart) == true) _intMillisecondsPart = Convert.ToInt32 ( _strMillisecondsPart ); inputStrTime = initrode.utilities.StringManager.StripWhitespace ( _millisecondsAndTimeParts[0].ToString() ); } ArrayList _timeParts = initrode.utilities.StringManager.splitIntoArrayList ( inputStrTime ,":" ); if (_timeParts.Count != 3) return _returnDateTime; string _strHoursPart = initrode.utilities.StringManager.StripWhitespace ( _timeParts[0].ToString() ); string _strMinutesPart = initrode.utilities.StringManager.StripWhitespace ( _timeParts[1].ToString() ); string _strSecondsPart = initrode.utilities.StringManager.StripWhitespace ( _timeParts[2].ToString() ); if ( initrode.utilities.StringManager.IsValidNumber ( _strHoursPart ) == false || initrode.utilities.StringManager.IsValidNumber ( _strMinutesPart ) == false || initrode.utilities.StringManager.IsValidNumber ( _strSecondsPart ) == false ) return _returnDateTime; int _intHoursPart = Convert.ToInt32 ( _strHoursPart ); int _intMinutesPart = Convert.ToInt32 ( _strMinutesPart ); int _intSecondsPart = Convert.ToInt32 ( _strSecondsPart ); if (_intHoursPart > 23) return _returnDateTime; if (inputAMOrPMFormat == true) { if (_intHoursPart > 12) return _returnDateTime; } if (_intMinutesPart > 59) return _returnDateTime; if (_intSecondsPart > 59) return _returnDateTime; if (inputAMOrPMFormat == true) { if (inputAMOrPM.CompareTo("PM") == 0) { _intHoursPart += 12; } else if ( inputAMOrPM.CompareTo("AM") == 0 && _intHoursPart == 12 && _intMinutesPart == 0 && _intSecondsPart == 0 && _intMillisecondsPart == 0 ) { return new DateTime ( inputDateOnlyDateTime.Year , inputDateOnlyDateTime.Month , inputDateOnlyDateTime.Day ); } } _returnDateTime = new DateTime ( inputDateOnlyDateTime.Year , inputDateOnlyDateTime.Month , inputDateOnlyDateTime.Day , _intHoursPart , _intMinutesPart , _intSecondsPart , _intMillisecondsPart ); return _returnDateTime; } public static DateTime convertDateInAnyStringFormatIntoDateTime ( string inputDateInAnyStringFormat ) { DateTime _returnDateTime = new DateTime(1900, 1, 1); inputDateInAnyStringFormat = initrode.utilities.StringManager.StripWhitespace ( inputDateInAnyStringFormat ); ArrayList _dateParts = new ArrayList(); string _strMonth = ""; string _strDay = ""; string _strYear = ""; if (inputDateInAnyStringFormat.Contains("/") == true) { _dateParts = initrode.utilities.StringManager.splitIntoArrayList ( inputDateInAnyStringFormat ,@"/" ); if (_dateParts.Count != 3) return _returnDateTime; _strMonth = initrode.utilities.StringManager.StripWhitespace ( _dateParts[0].ToString() ); _strDay = initrode.utilities.StringManager.StripWhitespace ( _dateParts[1].ToString() ); _strYear = initrode.utilities.StringManager.StripWhitespace ( _dateParts[2].ToString() ); return initrode.utilities.DateManager.convertDateTimeStringPartsIntoDateTime ( _strMonth ,_strDay ,_strYear ); } if (inputDateInAnyStringFormat.Contains("-") == true) { _dateParts = initrode.utilities.StringManager.splitIntoArrayList ( inputDateInAnyStringFormat , @"-" ); if (_dateParts.Count != 3) return _returnDateTime; _strYear = initrode.utilities.StringManager.StripWhitespace ( _dateParts[0].ToString() ); _strMonth = initrode.utilities.StringManager.StripWhitespace ( _dateParts[1].ToString() ); _strDay = initrode.utilities.StringManager.StripWhitespace ( _dateParts[2].ToString() ); return initrode.utilities.DateManager.convertDateTimeStringPartsIntoDateTime ( _strMonth , _strDay , _strYear ); } if (inputDateInAnyStringFormat.Length == 8) { _strYear = inputDateInAnyStringFormat.Substring(0, 4); _strMonth = inputDateInAnyStringFormat.Substring(4, 2); _strDay = inputDateInAnyStringFormat.Substring(6, 2); return initrode.utilities.DateManager.convertDateTimeStringPartsIntoDateTime ( _strMonth , _strDay , _strYear ); } if (inputDateInAnyStringFormat.Length == 6) { _strYear = inputDateInAnyStringFormat.Substring(0, 2); _strMonth = inputDateInAnyStringFormat.Substring(2, 2); _strDay = inputDateInAnyStringFormat.Substring(4, 2); return initrode.utilities.DateManager.convertDateTimeStringPartsIntoDateTime ( _strMonth , _strDay , _strYear ); } return _returnDateTime; } public static DateTime convertDateTimeStringPartsIntoDateTime ( string inputStrMonth , string inputStrDay , string inputStrYear ) { DateTime _returnDateTime = new DateTime(1900, 1, 1); if ( initrode.utilities.StringManager.IsValidNumber ( inputStrMonth ) == false || initrode.utilities.StringManager.IsValidNumber ( inputStrDay ) == false || initrode.utilities.StringManager.IsValidNumber ( inputStrYear ) == false ) return _returnDateTime; int _intYear = Convert.ToInt32 ( inputStrYear ); if (_intYear <= 100) { if (_intYear >= 90) { _intYear += 1900; } else { _intYear += 2000; } } inputStrYear = _intYear.ToString(); inputStrMonth = initrode.utilities.StringManager.Fill ( inputStrMonth ,"0" ,true //fromLeft ,2 ); inputStrDay = initrode.utilities.StringManager.Fill ( inputStrDay ,"0" ,true //fromLeft ,2 ); if ( initrode.utilities.StringManager.IsValidDate ( inputStrMonth ,inputStrDay ,inputStrYear ) == false ) return _returnDateTime; _returnDateTime = new DateTime ( Convert.ToInt32 ( inputStrYear ) , Convert.ToInt32 ( inputStrMonth ) , Convert.ToInt32 ( inputStrDay ) ); return _returnDateTime; } public static DateTime convertDateIn_MM_Slash_DD_Slash_YYYY_FormatToDateTime ( string inputDateIn_MM_Slash_DD_Slash_YYYY_Format ) { if (initrode.utilities.StringManager.IsValidDateInMM_DD_YYYYFormat(inputDateIn_MM_Slash_DD_Slash_YYYY_Format) == false) return Convert.ToDateTime("1/1/1900"); ArrayList _dateParts = initrode.utilities.StringManager.splitIntoArrayList ( inputDateIn_MM_Slash_DD_Slash_YYYY_Format ,"/" ); string _mm = _dateParts[0].ToString(); if (_mm.Substring(0, 1).CompareTo("0") == 0) _mm = _mm.Substring(1, 1); string _dd = _dateParts[1].ToString(); if (_dd.Substring(0, 1).CompareTo("0") == 0) _dd = _dd.Substring(1, 1); string _yyyy = _dateParts[2].ToString(); return new DateTime ( Convert.ToInt32 ( _yyyy ) , Convert.ToInt32 ( _mm ) , Convert.ToInt32 ( _dd ) ); } public static bool isInputtedDateTheLastBusinessDateOfTheMonth ( DateTime inputDateTime , Hashtable inputHolidayHash ) { inputDateTime = new DateTime ( inputDateTime.Year , inputDateTime.Month , inputDateTime.Day ); DateTime _lastBusinessDate = initrode.utilities.DateManager.getLastBusinessDateOfMonthForInputtedDate ( inputDateTime ,inputHolidayHash ); if ( inputDateTime.Year == _lastBusinessDate.Year && inputDateTime.Month == _lastBusinessDate.Month && inputDateTime.Day == _lastBusinessDate.Day ) return true; return false; } public static DateTime getLastBusinessDateOfMonthForInputtedDate ( DateTime inputDateTime , Hashtable inputHolidayHash ) { inputDateTime = new DateTime ( inputDateTime.Year ,inputDateTime.Month ,inputDateTime.Day ); DateTime _lastBusinessDate; if ( initrode.utilities.DateManager.isInputtedDateABusinessDate ( inputDateTime , inputHolidayHash ) == true ) _lastBusinessDate = inputDateTime; else _lastBusinessDate = initrode.utilities.DateManager.getNextBusinessDateFromInputtedDate ( inputDateTime , inputHolidayHash ); if (_lastBusinessDate.Month != inputDateTime.Month) { if ( initrode.utilities.DateManager.isInputtedDateABusinessDate ( inputDateTime , inputHolidayHash ) == true ) return inputDateTime; else return initrode.utilities.DateManager.getPreviousBusinessDateFromInputtedDate ( inputDateTime , inputHolidayHash ); } DateTime _nextBusinessDate = initrode.utilities.DateManager.getNextBusinessDateFromInputtedDate ( _lastBusinessDate , inputHolidayHash ); while (_nextBusinessDate.Month == inputDateTime.Month) { _lastBusinessDate = _nextBusinessDate; _nextBusinessDate = initrode.utilities.DateManager.getNextBusinessDateFromInputtedDate ( _lastBusinessDate , inputHolidayHash ); } return _lastBusinessDate; } public static DateTime getPreviousBusinessDateFromInputtedDate ( DateTime inputDateTime , Hashtable inputHolidayHash ) { DateTime _dateWithTimeOmitted = new DateTime ( inputDateTime.Year , inputDateTime.Month , inputDateTime.Day ); _dateWithTimeOmitted = _dateWithTimeOmitted.AddDays(-1); while ( initrode.utilities.DateManager.isInputtedDateABusinessDate ( _dateWithTimeOmitted , inputHolidayHash ) == false ) { _dateWithTimeOmitted = _dateWithTimeOmitted.AddDays(-1); } return _dateWithTimeOmitted; } public static DateTime getNextBusinessDateFromInputtedDate ( DateTime inputDateTime , Hashtable inputHolidayHash ) { DateTime _dateWithTimeOmitted = new DateTime ( inputDateTime.Year , inputDateTime.Month , inputDateTime.Day ); _dateWithTimeOmitted.AddDays(1); while ( initrode.utilities.DateManager.isInputtedDateABusinessDate ( _dateWithTimeOmitted ,inputHolidayHash ) == false ) { _dateWithTimeOmitted.AddDays(1); } return _dateWithTimeOmitted; } public static bool isInputtedDateABusinessDate ( DateTime inputDateTime ,Hashtable inputHolidayHash ) { DateTime _dateWithTimeOmitted = new DateTime ( inputDateTime.Year ,inputDateTime.Month ,inputDateTime.Day ); if (_dateWithTimeOmitted.DayOfWeek == DayOfWeek.Saturday || _dateWithTimeOmitted.DayOfWeek == DayOfWeek.Sunday) return false; foreach (DateTime _holidayDate in inputHolidayHash.Keys) { if ( _holidayDate.Year == inputDateTime.Year && _holidayDate.Month == inputDateTime.Month && _holidayDate.Day == inputDateTime.Day ) { return false; } } return true; } public static string convertDateTimeToMMDDYYYY_WithoutSlashesOrDashes ( DateTime inputDateTime ) { StringBuilder _dateBldr = new StringBuilder(); _dateBldr.AppendFormat ( "{0}{1}{2}" , initrode.utilities.StringManager.Fill ( inputDateTime.Month.ToString() , "0" , true //from left , 2 ) ,initrode.utilities.StringManager.Fill ( inputDateTime.Day.ToString() , "0" , true //from left , 2 ) ,inputDateTime.Year.ToString() ); return _dateBldr.ToString(); } public static DateTime convertMMDDYYYY_WithoutSlashesOrDashesToDateTime ( string inputMMDDYYYY ) { inputMMDDYYYY = initrode.utilities.StringManager.StripWhitespace ( inputMMDDYYYY ); StringBuilder _mmSlashddSlashyyyyBldr = new StringBuilder(); _mmSlashddSlashyyyyBldr.AppendFormat ( "{0}/{1}/{2}" ,inputMMDDYYYY.Substring(0,2) ,inputMMDDYYYY.Substring(2,2) ,inputMMDDYYYY.Substring(4,4) ); if ( initrode.utilities.StringManager.IsValidDateInMM_DD_YYYYFormat ( _mmSlashddSlashyyyyBldr.ToString() ) == false ) return new DateTime(1900, 1, 1); DateTime _returnDateTime = new DateTime ( Convert.ToInt32 ( inputMMDDYYYY.Substring(4, 4) ) , Convert.ToInt32 ( inputMMDDYYYY.Substring(0, 2) ) , Convert.ToInt32 ( inputMMDDYYYY.Substring(2, 2) ) ); return _returnDateTime; }public static DateTimeconvertDateInYYYYMMDDFormatToDateTime(string inputDateInYYYYMMDDFormat){if (initrode.utilities.StringManager.IsValidDateInYYYYMMDDFormat(inputDateInYYYYMMDDFormat) == false) return Convert.ToDateTime("1/1/1900");return newDateTime(Convert.ToInt32(inputDateInYYYYMMDDFormat.Substring(0,4)),Convert.ToInt32(inputDateInYYYYMMDDFormat.Substring(4,2)),Convert.ToInt32(inputDateInYYYYMMDDFormat.Substring(6,2)));}public static DateTimegetNextPeriodStartDateFromGivenDate(DateTime inputDate){if (inputDate.Day == 1) return inputDate;if (inputDate.Day == 16) return inputDate;if (inputDate.Day <= 15) return inputDate.AddDays(16 - inputDate.Day);return inputDate.AddMonths(1).AddDays(1 - inputDate.Day);}public static DateTimegetNextPeriodEndDateFromGivenPeriodStartDate(DateTime inputPeriodStartDate){if (inputPeriodStartDate.Day == 1) return inputPeriodStartDate.AddDays(15 - inputPeriodStartDate.Day);return inputPeriodStartDate.AddMonths(1).AddDays(0 - inputPeriodStartDate.Day);}public static DateTimeconvertDateInYYYYMMDDFormatAndTimeInHHColonMIColonSSFormatToDateTime(string inputDateInYYYYMMDDFormat,string inputTimeInHHColonMIColonSSFormat){inputDateInYYYYMMDDFormat = initrode.utilities.StringManager.StripWhitespaceFromEnds(inputDateInYYYYMMDDFormat);inputTimeInHHColonMIColonSSFormat = initrode.utilities.StringManager.StripWhitespaceFromEnds(inputTimeInHHColonMIColonSSFormat);if (inputDateInYYYYMMDDFormat.Length != 8 ||initrode.utilities.StringManager.IsValidDateInYYYYMMDDFormat(inputDateInYYYYMMDDFormat) == false) return new DateTime(1900,1,1);if (inputTimeInHHColonMIColonSSFormat.Length != 8 ||initrode.utilities.StringManager.IsValidTimeInHHColonMIColonSSFormat(inputTimeInHHColonMIColonSSFormat) == false)return newDateTime (Convert.ToInt32(inputDateInYYYYMMDDFormat.Substring(0,4)),Convert.ToInt32(inputDateInYYYYMMDDFormat.Substring(4,2)),Convert.ToInt32(inputDateInYYYYMMDDFormat.Substring(6,2)));return newDateTime ( Convert.ToInt32(inputDateInYYYYMMDDFormat.Substring(0,4)),Convert.ToInt32(inputDateInYYYYMMDDFormat.Substring(4,2)),Convert.ToInt32(inputDateInYYYYMMDDFormat.Substring(6,2)),Convert.ToInt32(inputTimeInHHColonMIColonSSFormat.Substring(0,2)),Convert.ToInt32(inputTimeInHHColonMIColonSSFormat.Substring(3,2)),Convert.ToInt32(inputTimeInHHColonMIColonSSFormat.Substring(6,2)));} public static bool validateTimestampInODBCCanonicalFormat ( string inputTimestampInODBCCanonicalFormat ) { if (inputTimestampInODBCCanonicalFormat.Length != 23) return false; if (initrode.utilities.StringManager.IsValidNumber(inputTimestampInODBCCanonicalFormat.Substring(0, 4)) == false || initrode.utilities.StringManager.IsValidNumber(inputTimestampInODBCCanonicalFormat.Substring(5, 2)) == false || initrode.utilities.StringManager.IsValidNumber(inputTimestampInODBCCanonicalFormat.Substring(8, 2)) == false || initrode.utilities.StringManager.IsValidNumber(inputTimestampInODBCCanonicalFormat.Substring(11, 2)) == false || initrode.utilities.StringManager.IsValidNumber(inputTimestampInODBCCanonicalFormat.Substring(14, 2)) == false || initrode.utilities.StringManager.IsValidNumber(inputTimestampInODBCCanonicalFormat.Substring(17, 2)) == false || initrode.utilities.StringManager.IsValidNumber(inputTimestampInODBCCanonicalFormat.Substring(20, 3)) == false) return false; string _yyyy = inputTimestampInODBCCanonicalFormat.Substring(0, 4); string _mm = inputTimestampInODBCCanonicalFormat.Substring(5, 2); string _dd = inputTimestampInODBCCanonicalFormat.Substring(8, 2); if (initrode.utilities.StringManager.IsValidDate ( _mm ,_dd ,_yyyy ) == false) return false; StringBuilder _timeBldr = new StringBuilder(); _timeBldr.Append ( inputTimestampInODBCCanonicalFormat.Substring(11, 2) ); _timeBldr.Append ( ":" ); _timeBldr.Append ( inputTimestampInODBCCanonicalFormat.Substring(14, 2) ); _timeBldr.Append ( ":" ); _timeBldr.Append ( inputTimestampInODBCCanonicalFormat.Substring(17, 2) ); if (initrode.utilities.StringManager.IsValidTimeInHHColonMIColonSSFormat ( _timeBldr.ToString() ) == false) return false; return true; } public static DateTime convertTimestampInODBCCanonicalFormatToDateTime ( string inputTimestampInODBCCanonicalFormat ) { if (validateTimestampInODBCCanonicalFormat ( inputTimestampInODBCCanonicalFormat ) == false) return new DateTime(1900, 1, 1); int _yyyy = Convert.ToInt32(inputTimestampInODBCCanonicalFormat.Substring(0,4)); int _mm = Convert.ToInt32(inputTimestampInODBCCanonicalFormat.Substring(5,2)); int _dd = Convert.ToInt32(inputTimestampInODBCCanonicalFormat.Substring(8,2)); int _hh = Convert.ToInt32(inputTimestampInODBCCanonicalFormat.Substring(11, 2)); int _mi = Convert.ToInt32(inputTimestampInODBCCanonicalFormat.Substring(14, 2)); int _ss = Convert.ToInt32(inputTimestampInODBCCanonicalFormat.Substring(17, 2)); int _ms = Convert.ToInt32(inputTimestampInODBCCanonicalFormat.Substring(20, 3)); return new DateTime ( _yyyy , _mm , _dd , _hh , _mi , _ss , _ms ); }/** * * @description get first day of the current month * **/public static DateTime getFirstDayofTheCurrentMonth() {return initrode.utilities.DateManager.getFirstDayofTheMonth(System.DateTime.Now);} public static DateTime convertDateTimeToDate ( DateTime inputTimestamp ) { DateTime _returnDate = new DateTime ( inputTimestamp.Year ,inputTimestamp.Month ,inputTimestamp.Day ); return _returnDate; }/** * @description get the last day of the month * */ public static DateTime getLastDayOfTheMonth( System.DateTime inputDateTime) {return initrode.utilities.DateManager.getFirstDayofNextMonth(inputDateTime).AddDays(-1);}/** * @description get last day of the current month * **/public static DateTime getLastDayofTheCurrentMonth() {return initrode.utilities.DateManager.getLastDayOfTheMonth(DateTime.Now);}/** * Convert the DateTime value to YYYYMMDD format * **/public static string convertDateTimeToYYYYMMDDFormat(DateTime inputDateTime){StringBuilder _dateBldr = new StringBuilder();_dateBldr.Append(inputDateTime.Year.ToString());_dateBldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Month.ToString(),"0",true,2));_dateBldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Day.ToString(),"0",true,2));return _dateBldr.ToString();}/** * Convert the DateTime value to MM, DD, YYYY character parts * **/public static void convertDateTimeToMM_DD_YYYYStringParts(DateTime inputDateTime,out string outputMM,out string outputDD,out string outputYYYY){string _date_in_MM_DD_YYYY_Format = convertDateTimeToMM_DD_YYYYFormat(inputDateTime);outputMM = "";outputDD = "";outputYYYY = "";outputMM = _date_in_MM_DD_YYYY_Format.Substring(0,2);outputDD = _date_in_MM_DD_YYYY_Format.Substring(3,2);outputYYYY = _date_in_MM_DD_YYYY_Format.Substring(6,4);}/** * Convert the DateTime value to MM_DD_YYYY format. * **/public static DateTime convertMM_DD_YYYYFormatToDateTime(string inputDateInMM_DD_YYYYFormat){inputDateInMM_DD_YYYYFormat = initrode.utilities.StringManager.StripWhitespaceFromEnds(inputDateInMM_DD_YYYYFormat);if (initrode.utilities.StringManager.IsValidDateInMM_DD_YYYYFormat(inputDateInMM_DD_YYYYFormat) == false) return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);int _intMM = Convert.ToInt32(inputDateInMM_DD_YYYYFormat.Substring(0,2));int _intDD = Convert.ToInt32(inputDateInMM_DD_YYYYFormat.Substring(3,2));int _intYYYY = Convert.ToInt32(inputDateInMM_DD_YYYYFormat.Substring(6,4));return new DateTime(_intYYYY,_intMM, _intDD);} public static int calculateMonthsDifferenceBetweenTwoDates ( DateTime inputOlderDate , DateTime inputNewerDate ) { DateTime _tempDate = inputOlderDate; int _numberOfMonthsDifference = 0; while (_tempDate < inputNewerDate) { _tempDate = _tempDate.AddMonths(1); if (_tempDate < inputNewerDate) _numberOfMonthsDifference++; } return _numberOfMonthsDifference; }/** * Convert the DateTime value to MM_DD_YYYY format. * **/public static string convertTimestampToStringFormat(DateTime inputDateTime){StringBuilder _bldr = new StringBuilder();_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Month.ToString(),"0",true, //Fill from left2));_bldr.Append("/");_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Day.ToString(),"0",true, //Fill from left2));_bldr.Append("/");_bldr.Append(inputDateTime.Year.ToString());_bldr.Append(" ");_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Hour.ToString(),"0",true, //Fill from left2));_bldr.Append(":");_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Minute.ToString(),"0",true, //Fill from left2));_bldr.Append(":");_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Second.ToString(),"0",true, //Fill from left2));_bldr.Append(".");_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Millisecond.ToString(),"0",true, //Fill from left3));return _bldr.ToString();}/** * Convert the DateTime value to MM_DD_YYYY format. * **/public static string convertDateTimeToMM_DD_YYYYFormat(DateTime inputDateTime){StringBuilder _bldr = new StringBuilder();_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Month.ToString(),"0",true, //Fill from left2));_bldr.Append("/");_bldr.Append(initrode.utilities.StringManager.Fill(inputDateTime.Day.ToString(),"0",true, //Fill from left2));_bldr.Append("/");_bldr.Append(inputDateTime.Year.ToString());return _bldr.ToString();}/** * @description get first day of the next month * **/public static DateTime getFirstDayofNextMonth(DateTime inputDateTime) {return initrode.utilities.DateManager.getFirstDayofTheMonth(inputDateTime).AddMonths(1);}/** * @description get first day of the next month * **/public static DateTime getFirstDayofNextMonth() {return getFirstDayofNextMonth(DateTime.Now); }/** * @description add days to a date * **/public static DateTime daysFromTheFirst(int days, System.DateTime date){DateTime nextDate = date.AddDays(days); //calculate 1 day of next monthreturn nextDate;}/** * * @description get first day of the current year * **/public static DateTime getFirstDayofTheCurrentYear() {return initrode.utilities.DateManager.getFirstDayofTheInputtedDatesYear(System.DateTime.Now);}/** * * @description get last day of the current year * **/public static DateTime getLastDayofTheCurrentYear() {return initrode.utilities.DateManager.getFirstDayofTheCurrentYear().AddYears(1).AddDays(-1);}/** * * @description get first day of the inputted year * **/public static DateTime getFirstDayofTheInputtedDatesYear(System.DateTime inputDateTime) {return new DateTime(inputDateTime.Year,1,1);}/** * * @description get last day of the inputted year * **/public static DateTime getLastDayofTheInputtedDatesYear(System.DateTime inputDateTime) {return new DateTime(inputDateTime.Year,12,31);}public static DateComparison timestamp1ComparedToTimestamp2(DateTime inputTimestamp1, DateTime inputTimestamp2){if (inputTimestamp1.Year > inputTimestamp2.Year) return DateComparison.gt;if (inputTimestamp1.Year < inputTimestamp2.Year) return DateComparison.lt;if (inputTimestamp1.DayOfYear > inputTimestamp2.DayOfYear) return DateComparison.gt;if (inputTimestamp1.DayOfYear < inputTimestamp2.DayOfYear) return DateComparison.lt;if (inputTimestamp1.Hour > inputTimestamp2.Hour) return DateComparison.gt;if (inputTimestamp1.Hour < inputTimestamp2.Hour) return DateComparison.lt;if (inputTimestamp1.Minute > inputTimestamp2.Minute) return DateComparison.gt;if (inputTimestamp1.Minute < inputTimestamp2.Minute) return DateComparison.lt;if (inputTimestamp1.Second > inputTimestamp2.Second) return DateComparison.gt;if (inputTimestamp1.Second < inputTimestamp2.Second) return DateComparison.lt;if (inputTimestamp1.Millisecond > inputTimestamp2.Millisecond) return DateComparison.gt;if (inputTimestamp1.Millisecond < inputTimestamp2.Millisecond) return DateComparison.lt;return DateComparison.eq;}public static DateComparison date1ComparedToDate2(DateTime inputDate1, DateTime inputDate2){if (inputDate1.Year > inputDate2.Year) return DateComparison.gt;if (inputDate1.Year < inputDate2.Year) return DateComparison.lt;if (inputDate1.DayOfYear > inputDate2.DayOfYear) return DateComparison.gt;if (inputDate1.DayOfYear < inputDate2.DayOfYear) return DateComparison.lt;return DateComparison.eq;}/** * * @description get first day of the first Future Period * **/public static DateTime getTheDateBeforeTheFirstFuturePeriod() {// If date is less than the 16th, the 15th is the date.DateTime _date = DateTime.Now;if (_date.Day < 16){return _date.AddDays(15 - _date.Day);}// If date is greater than the 16th, the 1st of the following month is// the first date of the first future period.return _date.AddDays(1 - _date.Day).AddMonths(1).AddDays(-1);}}}
buildmaster-icon.png [Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how! TheDailyWtf?d=yIl2AUoC8zAiuIXXz86C28
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments