SafeDispatch/Safedispatch_4_0/Convertdate.cs
2024-02-22 18:43:59 +02:00

321 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace Safedispatch_4_0
{
public class ConvertDT
{
private System.DateTime dateTime;
/// <summary>
/// This is the class constructor
/// </summary>
public ConvertDT()
{
}
/// <summary>
/// This method makes the conversion from Unix time to a DateTime object
/// and returns a DateTime object
/// </summary>
/// <param name="param">the Unix time</param>
/// <returns>The method returns a DateTime object</returns>
public System.DateTime GetDTFromSeconds(Int32 param)
{
dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds((double)param);
return dateTime;
}
/// <summary>
/// This method converts a DateTime object into the corresponding
/// Unix time (number of seconds from 1970/01/01) - attention, does not make
/// any conversion to GMT time
/// </summary>
/// <param name="param">the DateTime object to be converted</param>
/// <returns>the corresponding Unix time, as a uint number</returns>
public Int32 GetSecondsFromDT(DateTime param)
{
System.DateTime datetime = param;
Int32 nOfSeconds;
System.DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan span = datetime - dt70;
nOfSeconds = (Int32)span.TotalSeconds;
return nOfSeconds;
}
public Int32 GetSecondsLocalFromDT(DateTime param)
{
System.DateTime datetime = param;
long nOfSeconds;
System.DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
System.DateTime dttmp1 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
System.DateTime dttmp2;
dttmp2 = dttmp1.ToLocalTime();
TimeSpan span2 = dttmp2 - dttmp1;
TimeSpan span = datetime - dt70;
//nOfSeconds = (long)span.TotalSeconds - (long)span2.TotalSeconds -3600; //mai scot o ora - 3600
if (System.TimeZone.CurrentTimeZone.IsDaylightSavingTime(param)) nOfSeconds = (long)span.TotalSeconds - (long)span2.TotalSeconds - 3600; //mai scot o ora - 3600
else nOfSeconds = (long)span.TotalSeconds - (long)span2.TotalSeconds;
return ((Int32)nOfSeconds);
}
/// <summary>
/// This method makes the conversion from GMT Unix time to a DateTime object
/// and returns local time as a DateTime object
/// </summary>
/// <param name="param">the Unix time</param>
/// <returns>The method returns a DateTime object</returns>
public System.DateTime GetDTLocalFromSeconds(Int32 param)
{
dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds((double)param);
dateTime = dateTime.ToLocalTime();
return dateTime;
}
/// <summary>
/// This method makes the conversion from local Unix time to a DateTime object
/// and returns GMT time as a DateTime object
/// </summary>
/// <param name="param">the Unix time</param>
/// <returns>The method returns a DateTime object</returns>
public System.DateTime GetDTGMTFromSeconds(Int32 param)
{
dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds((double)param);
dateTime = dateTime.ToUniversalTime();
return dateTime;
}
/// <summary>
/// This method converts from local time to GMT
/// </summary>
/// <param name="param">local time, as UNIX time</param>
/// <returns>GMT time, as UNIX time</returns>
public Int32 ConvertLocalToGMT(Int32 param)
{
DateTime time = this.GetDTFromSeconds(param);
DateTime GMTTime = time.ToUniversalTime();
TimeSpan span = GMTTime - time;
Int32 nOfSeconds = (Int32)span.TotalSeconds;
return (Int32)(nOfSeconds + param);
}
/// <summary>
/// This method converts from GMT to local time
/// </summary>
/// <param name="param">GMT as UNIX time</param>
/// <returns>local time, as UNIX time</returns>
public Int32 ConvertGMTToLocal(Int32 param)
{
DateTime time = this.GetDTFromSeconds(param);
DateTime GMTTime = time.ToUniversalTime();
TimeSpan span = time - GMTTime;
long nOfSeconds = (long)span.TotalSeconds;
return (Int32)(nOfSeconds + param);
}
/// <summary>
/// This method converts an integer, representing the number of seconds
/// from 00:00 to the current time of the day, to the corresponding String
/// </summary>
/// <param name="param">the number of seconds</param>
/// <returns>the String in hh:mm format</returns>
public String GetTimeFromSeconds(Int32 param)
{
String hours, minutes;
Int32 hoursInt, minutesInt;
hoursInt = param / 3600;
minutesInt = (param - (hoursInt * 3600)) / 60;
hours = Convert.ToString(hoursInt);
minutes = Convert.ToString(minutesInt);
if (hours.Length == 1)
hours = "0" + hours;
if (minutes.Length == 1)
minutes = "0" + minutes;
return (Convert.ToString(hours) + ":" + Convert.ToString(minutes));
}
/// <summary>
/// Returns just the time of day from the parameter, removing all other info
/// </summary>
/// <param name="param">unix time</param>
/// <returns>Time of day </returns>
public String TimeOfDay(Int32 param)
{
String toReturn = "";
DateTime tempObj = this.GetDTFromSeconds(param);
if (tempObj.Hour >= 10)
toReturn = toReturn + tempObj.Hour.ToString() + ":";
else
toReturn = toReturn + "0" + tempObj.Hour.ToString() + ":";
if (tempObj.Minute >= 10)
toReturn = toReturn + tempObj.Minute.ToString() + ":";
else
toReturn = toReturn + "0" + tempObj.Minute.ToString() + ":";
if (tempObj.Second >= 10)
toReturn = toReturn + tempObj.Second.ToString() + ":";
else
toReturn = toReturn + "0" + tempObj.Second.ToString();
return toReturn;
}
public String TimeOfDayHHMMLocal(Int32 param)
{
Int32 ParamLoc;
ParamLoc = ConvertGMTToLocal(param);
String toReturn = "";
DateTime tempObj = this.GetDTFromSeconds(ParamLoc);
if (tempObj.Hour >= 10)
toReturn = toReturn + tempObj.Hour.ToString() + ":";
else
toReturn = toReturn + "0" + tempObj.Hour.ToString() + ":";
if (tempObj.Minute >= 10)
toReturn = toReturn + tempObj.Minute.ToString();
else
toReturn = toReturn + "0" + tempObj.Minute.ToString();
return toReturn;
}
public String TimeOfDayHHMMLocal(Int32 param, Boolean AMPMtype)
{
Int32 ParamLoc;
ParamLoc = ConvertGMTToLocal(param);
String toReturn = "";
DateTime tempObj = this.GetDTFromSeconds(ParamLoc);
Int32 hourX = tempObj.Hour;
String Endis = "AM";
if (AMPMtype)
{
if (hourX>12)
{
hourX=hourX-12;
if (hourX<0) hourX =12;
Endis = "PM";
}
}
if (hourX >= 10)
toReturn = toReturn + hourX.ToString() + ":";
else
toReturn = toReturn + "0" + hourX.ToString() + ":";
if (tempObj.Minute >= 10)
toReturn = toReturn + tempObj.Minute.ToString();
else
toReturn = toReturn + "0" + tempObj.Minute.ToString();
if (AMPMtype) toReturn = toReturn + " " + Endis;
return toReturn;
}
public String TimeOfDayHHMMSSLocal(Int32 param, Boolean AMPMtype)
{
Int32 ParamLoc;
ParamLoc = ConvertGMTToLocal(param);
String toReturn = "";
DateTime tempObj = this.GetDTFromSeconds(ParamLoc);
Int32 hourX = tempObj.Hour;
String Endis = "AM";
if (AMPMtype)
{
if (hourX > 12)
{
hourX = hourX - 12;
if (hourX < 0) hourX = 12;
Endis = "PM";
}
}
if (hourX >= 10)
toReturn = toReturn + hourX.ToString() + ":";
else
toReturn = toReturn + "0" + hourX.ToString() + ":";
if (tempObj.Minute >= 10)
toReturn = toReturn + tempObj.Minute.ToString() + ":";
else
toReturn = toReturn + "0" + tempObj.Minute.ToString() + ":";
if (tempObj.Second >= 10)
toReturn = toReturn + tempObj.Second.ToString();
else
toReturn = toReturn + "0" + tempObj.Second.ToString();
if (AMPMtype) toReturn = toReturn + " " + Endis;
return toReturn;
}
public String DateTimeOfDayHHMMLocal(Int32 param)
{
Int32 ParamLoc;
ParamLoc = ConvertGMTToLocal(param);
String toReturn = "";
DateTime tempObj = this.GetDTFromSeconds(ParamLoc);
if (tempObj.Hour >= 10)
toReturn = toReturn + tempObj.Hour.ToString() + ":";
else
toReturn = toReturn + "0" + tempObj.Hour.ToString() + ":";
if (tempObj.Minute >= 10)
toReturn = toReturn + tempObj.Minute.ToString();
else
toReturn = toReturn + "0" + tempObj.Minute.ToString();
//toReturn = tempObj.Month +"//"+tempObj.Day +"//"+tempObj.Year+" "+toReturn;
toReturn = Convert.ToString(tempObj);
//toReturn = "fane " + toReturn;
return toReturn;
}
public String TimeOfDayHHMMDateTime(TimeSpan param)
{
String toReturn = "";
if (param.Hours >= 10)
toReturn = toReturn + param.Hours.ToString() + ":";
else
toReturn = toReturn + "0" + param.Hours.ToString() + ":";
if (param.Minutes >= 10)
toReturn = toReturn + param.Minutes.ToString();
else
toReturn = toReturn + "0" + param.Minutes.ToString();
return toReturn;
}
public String DatePickerToDateHHMM(DateTime Data)
{
String toReturn = "";
toReturn = toReturn + System.Convert.ToString(Data.Date.ToShortDateString()) + " ";
if (Data.TimeOfDay.Hours >= 10)
toReturn = toReturn + Data.TimeOfDay.Hours.ToString() + ":";
else
toReturn = toReturn + "0" + Data.TimeOfDay.Hours.ToString() + ":";
if (Data.TimeOfDay.Minutes >= 10)
toReturn = toReturn + Data.TimeOfDay.Minutes.ToString();
else
toReturn = toReturn + "0" + Data.TimeOfDay.Minutes.ToString();
return toReturn;
}
}
}