/*********************************************************************************************
 * Copyright 2005 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: DTI.cs $     $Revision: 1 $
 * $Author: Kathy $   $Date: 3/08/05 1:46p $
 *
 * $History: DTI.cs $
 * 
 * *****************  Version 1  *****************
 * User: Kathy        Date: 3/08/05    Time: 1:46p
 * Created in $/LibSource/Utils
 * Approval
 *********************************************************************************************/
using System;
using System.Text;


namespace Utils
{
	/// <summary>
	/// Summary description for DTI.
	/// </summary>
	public class DTI
	{
		public static string DateTimeInit;
		public static string SetDateTimeInit;
		public static string Initials;		// set from the user's run time.

		public static string CurDate(string delim)
		{
			DateTime dt = DateTime.Today;
			
			StringBuilder sdt = new StringBuilder(11);
			if (delim == null || delim == "") delim = "-";
			sdt.Append(dt.Month.ToString("D2"));
			sdt.Append(delim);
			sdt.Append(dt.Day.ToString("D2"));
			sdt.Append(delim);
			sdt.Append(dt.Year.ToString("D2"));
			return sdt.ToString();
		}

		public static string CurTime()
		{
			DateTime dt = DateTime.Now;
			StringBuilder sdt = new StringBuilder(11);
			sdt.Append(dt.Hour.ToString("D2"));
			sdt.Append(":");
			sdt.Append(dt.Minute.ToString("D2"));
			sdt.Append(":");
			sdt.Append(dt.Second.ToString("D2"));
			return sdt.ToString();
		}

		// ovrride == 0 default, don't do anything different
		// ovrride == 1 means simply override "super"
		// ovrride == 2 means force a reset of the DTI
		public static void UpdateDateTimeInit(int ovrride)
		{
			StringBuilder ldti = new StringBuilder(19);
			if (ovrride != 2 && (ovrride!=0 || Initials!="super"))
			{
				string tptr = CurDate(null);
				ldti = new StringBuilder(12);
				ldti.Insert(0,tptr.Substring(6,4));
				ldti.Insert(4,tptr.Substring(0,2));
				ldti.Insert(6,tptr.Substring(3,2));		
				ldti.Insert(8,CurTime().Substring(0,5));
				ldti.Insert(13,Initials.PadRight(5,' ').Substring(0,5));
			}
			else
				ldti.Insert(0,DTI.SetDateTimeInit);

			// for dbase, replace spaces with '0' in date/time part & shift any non-number
			// in first time field back to end of time field (?not sure why - this was 
			// done in 16-bit code.
			for (int i=0;i<13&&ldti[i]!=0;i++) if (ldti[i]==' ') ldti[i]='0';
			char c = ldti[10];
			if (!Char.IsDigit(c))
			{
				ldti[10]=ldti[11];
				ldti[11]=ldti[12];
				ldti[12]=c;
			}

			if (ldti.ToString().Substring(0,8).Equals("19800101"))
			{	
				ldti.Insert(0,SetDateTimeInit.Substring(0,13));
			}
			DateTimeInit = ldti.ToString();
		}

		// MakeDate is passed a date in the form of YYYYMMDD and translates it
		// into MM/DD/YY (if ShortYear is true) or MM/DD/YYYY. condense
		// removes any leading ' ' or 0 in month or day.
		public static string MakeDate(string src, bool condense, bool ShortYear)
		{
			int[] DateOffset={4,5,47,6,7,47,2,3};			// 47 = '/'
			StringBuilder datebuff = new StringBuilder(12);
			for (int i=0; i<DateOffset.Length; i++)
			{
				if (DateOffset[i]<9)
					datebuff.Append(src[DateOffset[i]]);
				else
					datebuff.Append(System.Convert.ToChar(DateOffset[i]));
			}
		
			// for Y2K the year should be four digits. Copy the first
			// four characters from the source (i.e. 4 digit year) to
			// the 'year part' of the destination.
	
			if (!ShortYear)
			{
				datebuff.Append("  ");	// add the two characters to replace in next stmt
				for (int i=0;i<4;i++) datebuff[i+6]=src[i];
			}
			// if the condense flag is set, remove leading 0 or blank
			// from day and month fields.
			if(condense)
			{
				for (int i=0;i<datebuff.Length;i++)
				{
					bool r1 = (datebuff[0]==' ')?true:false;
					bool r2 = (datebuff[3]=='0'||datebuff[3]==' ')?true:false;
					int pos = 0;

					if (!((r1 && i==0)||(r2 && i==3)))
					{
						datebuff[pos]=datebuff[i];
						pos++;
					}
				}
			}
			return(datebuff.ToString());
		
		}
	}
}
