228 lines
6.5 KiB
C#
228 lines
6.5 KiB
C#
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
|
||
namespace Zxd.Core.Shared.Helpers
|
||
{
|
||
public class ConvertHelper
|
||
{
|
||
public static bool? GetBoolean(string input)
|
||
{
|
||
return GetBoolean(input, null);
|
||
}
|
||
|
||
public static bool? GetBoolean(string input, bool? defaultValue)
|
||
{
|
||
if (bool.TryParse(input, out bool temp))
|
||
{
|
||
return temp;
|
||
}
|
||
|
||
return defaultValue;
|
||
}
|
||
|
||
public static int? GetInt(string input)
|
||
{
|
||
return GetInt(input, null);
|
||
}
|
||
|
||
public static int? GetInt(string input, int? defaultValue)
|
||
{
|
||
if (!string.IsNullOrEmpty(input))
|
||
{
|
||
if (int.TryParse(input, out int temp))
|
||
{
|
||
return temp;
|
||
}
|
||
}
|
||
|
||
return defaultValue;
|
||
}
|
||
|
||
public static long? GetLong(string input)
|
||
{
|
||
return GetLong(input, null);
|
||
}
|
||
|
||
public static long? GetLong(string input, long? defaultValue)
|
||
{
|
||
if (!string.IsNullOrEmpty(input))
|
||
{
|
||
if (long.TryParse(input, out long temp))
|
||
{
|
||
return temp;
|
||
}
|
||
}
|
||
|
||
return defaultValue;
|
||
}
|
||
|
||
public static decimal? GetDecimal(string input)
|
||
{
|
||
return GetDecimal(input, null);
|
||
}
|
||
|
||
public static decimal? GetDecimal(string input, decimal? defaultValue)
|
||
{
|
||
if (!string.IsNullOrEmpty(input))
|
||
{
|
||
if (decimal.TryParse(input, out decimal temp))
|
||
{
|
||
return temp;
|
||
}
|
||
}
|
||
|
||
return defaultValue;
|
||
}
|
||
|
||
public static DateTime? GetDateTime(string input)
|
||
{
|
||
return GetDateTime(input, null);
|
||
}
|
||
|
||
public static DateTime? GetDateTime(string input, DateTime? defaultValue)
|
||
{
|
||
if (DateTime.TryParse(input, out DateTime temp))
|
||
{
|
||
return temp;
|
||
}
|
||
|
||
return defaultValue;
|
||
}
|
||
|
||
public static DateTime? GetDateTimeExact(string input, string format)
|
||
{
|
||
return GetDateTimeExact(input, format, null);
|
||
}
|
||
|
||
public static DateTime? GetDateTimeExact(string input, string format, DateTime? defaultValue)
|
||
{
|
||
if (DateTime.TryParseExact(input, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime temp))
|
||
{
|
||
return temp;
|
||
}
|
||
|
||
return defaultValue;
|
||
}
|
||
|
||
public static decimal? ParseNumberFromString(string input)
|
||
{
|
||
return ParseNumberFromString(input, false);
|
||
}
|
||
|
||
public static decimal? ParseNumberFromString(string input, bool exact_mode)
|
||
{
|
||
var exact_input = "";
|
||
if (exact_mode)
|
||
{
|
||
var matches = Regex.Matches(input, "([0-9]+[.]?[0-9]+)");
|
||
if (matches.Count > 0)
|
||
{
|
||
if (matches[0].Groups.Count > 1)
|
||
{
|
||
exact_input = matches[0].Groups[1].Value;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
var sb = new StringBuilder();
|
||
foreach (var ch in input)
|
||
{
|
||
if (!char.IsNumber(ch) && ch != '.')
|
||
{
|
||
continue;
|
||
}
|
||
|
||
sb.Append(ch);
|
||
}
|
||
exact_input = sb.ToString();
|
||
}
|
||
|
||
return GetDecimal(exact_input);
|
||
}
|
||
|
||
public static object GetVal(Type ttype, string val)
|
||
{
|
||
if (ttype == typeof(int) || ttype == typeof(int?))
|
||
{
|
||
return GetInt(val).GetValueOrDefault();
|
||
}
|
||
else if (ttype == typeof(long) || ttype == typeof(long?))
|
||
{
|
||
return GetLong(val).GetValueOrDefault();
|
||
}
|
||
else if (ttype == typeof(decimal) || ttype == typeof(decimal?))
|
||
{
|
||
return GetDecimal(val).GetValueOrDefault();
|
||
}
|
||
else if (ttype == typeof(bool) || ttype == typeof(bool?))
|
||
{
|
||
return ParseBooleanFromString(val).GetValueOrDefault();
|
||
}
|
||
else if (ttype == typeof(DateTime) || ttype == typeof(DateTime?))
|
||
{
|
||
return GetDateTime(val).GetValueOrDefault();
|
||
}
|
||
else if (ttype == typeof(string))
|
||
{
|
||
return val;
|
||
}
|
||
else
|
||
{
|
||
throw new ArgumentException($"不支持的类型:{ttype.Name}");
|
||
}
|
||
}
|
||
|
||
public static bool? ParseGenderFromString(string input)
|
||
{
|
||
if (!string.IsNullOrEmpty(input))
|
||
{
|
||
var males = new string[] { "1", "男", "true", "m", "male" };
|
||
if (males.Any(a => input.ToLower().Contains(a)))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
var females = new string[] { "0", "女", "false", "f", "female" };
|
||
if (females.Any(a => input.ToLower().Contains(a)))
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public static bool? ParseBooleanFromString(string input)
|
||
{
|
||
if (!string.IsNullOrEmpty(input))
|
||
{
|
||
var yes = new string[] { "1", "真", "true", "yes" };
|
||
if (yes.Any(a => input.ToLower().Contains(a)))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
var no = new string[] { "0", "假", "false", "no" };
|
||
if (no.Any(a => input.ToLower().Contains(a)))
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// </summary>
|
||
/// <param name="timeJavaLong">java长整型日期,毫秒为单位</param>
|
||
/// <returns></returns>
|
||
public static DateTime JavaLongToDateTime(long timeJavaLong)
|
||
{
|
||
var dt1970 = new DateTime(1970, 1, 1, 0, 0, 0);
|
||
var tricks1970 = dt1970.Ticks;//1970年1月1日刻度
|
||
var timeTricks = tricks1970 + timeJavaLong * 10000;//日志日期刻度
|
||
return new DateTime(timeTricks).AddHours(8);//转化为DateTime
|
||
}
|
||
}
|
||
} |