SafeDispatch/SafeMobileLIB_DLL/ExtensionMethods.cs
2024-02-22 18:43:59 +02:00

329 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SafeMobileLib
{
public static class ExtensionMethods
{
public static uint DateTo70Format(this DateTime param)
{
long nOfSeconds;
DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan span = param - dt70;
nOfSeconds = (long)span.TotalSeconds;
return ((uint)nOfSeconds);
}
public static Int32 GetSecondsFromDT(this DateTime param)
{
DateTime datetime = param;
Int32 nOfSeconds;
DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan span = datetime - dt70;
nOfSeconds = (Int32)span.TotalSeconds;
return nOfSeconds;
}
public static DateTime GetDTFromSeconds(this uint param)
{
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds((double)param);
return dateTime;
}
public static DateTime GetDTFromSeconds(this int param)
{
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds((double)param);
return dateTime;
}
public static DateTime GetDTLocalFromSeconds(this uint param)
{
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds((double)param);
dateTime = dateTime.ToLocalTime();
return dateTime;
}
public static DateTime GetDTLocalFromSeconds(this Int32 param)
{
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds((double)param);
dateTime = dateTime.ToLocalTime();
return dateTime;
}
public static uint GetSecondsLocalFromDT(this 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;
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 ((uint)nOfSeconds);
}
public static String TimeOfDayHHMMLocal(this Int32 param)
{
Int32 ParamLoc;
ParamLoc = ConvertGMTToLocal(param);
String toReturn = "";
DateTime tempObj = ((uint)ParamLoc).GetDTFromSeconds();
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 static Int32 ConvertGMTToLocal(this Int32 param)
{
DateTime time = (param).GetDTFromSeconds();
DateTime GMTTime = time.ToUniversalTime();
TimeSpan span = time - GMTTime;
long nOfSeconds = (long)span.TotalSeconds;
return (Int32)(nOfSeconds + param);
}
public static String TimeOfDayHHMMLocal(this Int32 param, Boolean AMPMtype)
{
Int32 ParamLoc;
ParamLoc = ConvertGMTToLocal(param);
String toReturn = "";
DateTime tempObj = ((uint)ParamLoc).GetDTFromSeconds();
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 static String TimeOfDayHHMMSSLocal(this Int32 param, Boolean AMPMtype)
{
Int32 ParamLoc;
ParamLoc = ConvertGMTToLocal(param);
String toReturn = "";
DateTime tempObj = ((uint)ParamLoc).GetDTFromSeconds();
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 static String DateTimeOfDayHHMMLocal(this Int32 param)
{
Int32 ParamLoc;
ParamLoc = ConvertGMTToLocal(param);
String toReturn = "";
DateTime tempObj = ((uint)ParamLoc).GetDTFromSeconds();
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 = Convert.ToString(tempObj);
return toReturn;
}
public static String GetRegionalFormat(this Int32 ValTime, Boolean is24hours, Boolean DayFirst)
{
String toreturn = "";
if (is24hours)
{
if (DayFirst)
toreturn = ((uint)ValTime).GetDTLocalFromSeconds().ToString("dd/MM/yyyy HH:mm:ss");
else
toreturn = ((uint)ValTime).GetDTLocalFromSeconds().ToString("MM/dd/yyyy HH:mm:ss");
}
else
{
if (DayFirst)
toreturn = ((uint)ValTime).GetDTLocalFromSeconds().ToString("dd/MM/yyyy hh:mm:ss tt");
else
toreturn = ((uint)ValTime).GetDTLocalFromSeconds().ToString("MM/dd/yyyy hh:mm:ss tt");
}
return toreturn;
}
/// <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 static Int32 ConvertLocalToGMT(this Int32 param)
{
DateTime time = ((uint)param).GetDTFromSeconds();
DateTime GMTTime = time.ToUniversalTime();
TimeSpan span = GMTTime - time;
Int32 nOfSeconds = (Int32)span.TotalSeconds;
return (Int32)(nOfSeconds + param);
}
public static string FirstLetterToUpper(this string str)
{
if (str == null)
return null;
if (str.Length > 1)
return char.ToUpper(str[0]) + str.Substring(1);
return str.ToUpper();
}
public static string FromUnicode(this string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
sb.Append(GetEscapeSequence(c));
return sb.ToString();
//string inputString = new string(new char[] { '\u70B9', '\u83DC' }); // 点菜
}
public static string ToUnicode(this string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
sb.Append(GetEscapeSequence(c));
return sb.ToString();
}
private static string GetEscapeSequence(char c)
{
return $"\\u{((int)c).ToString("X4")}";
}
public static string EncodeNonAsciiCharacters(this string value)
{
StringBuilder sb = new StringBuilder();
foreach (char c in value)
{
if (c > 127)
{
// This character is too big for ASCII
string encodedValue = "\\u" + ((int)c).ToString("x4");
sb.Append(encodedValue);
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
public static string DecodeEncodedNonAsciiCharacters(this string value)
{
return Regex.Replace(
value,
@"\\u(?<Value>[a-zA-Z0-9]{4})",
m => {
return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();
});
}
public static bool IsIpAddress(this string str)
{
//create our match pattern
string pattern = @"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$";
//create our Regular Expression object
Regex check = new Regex(pattern);
//boolean variable to hold the status
bool valid = false;
//check to make sure an ip address was provided
if (str == "")
{
//no address provided so return false
valid = false;
}
else
{
//address provided so use the IsMatch Method
//of the Regular Expression object
valid = check.IsMatch(str, 0);
}
//return the results
return valid;
}
}
}