namespace customTypes
{
///
/// gRPC送信用Date
/// SqlServerのDate型対応
///
public partial class Date
{
public static readonly int MinValue = 10000101;
public static readonly DateTime MinDate = DateTime.Parse("1000/01/01");
public static readonly Date Default = new Date(0);
///
/// 利用情報のプライマリーキー:1901/01/01
///
public static readonly Date MinUseDate = new Date(19010101);
public Date(int val)
{
Value = val;
}
public Date(string val)
{
Value = int.Parse(val.Replace("/", string.Empty).Replace("-", string.Empty));
}
///
/// 暗然変換
/// DateTime = Date
///
///
public static implicit operator DateTime(Date grpcDate)
{
try
{
if (grpcDate.Value <= MinValue) { return MinDate; }
string date = grpcDate.Value.ToString();
int year = System.Convert.ToInt32(date.Substring(0, 4));
int mon = System.Convert.ToInt32(date.Substring(4, 2));
int day = System.Convert.ToInt32(date.Substring(6, 2));
return new DateTime(year, mon, day);
}
catch
{
return MinDate;
}
}
///
/// 暗然変換
/// Date = DateTime
///
///
public static implicit operator Date(DateTime value)
{
return new Date(int.Parse(value.ToString("yyyyMMdd")));
}
///
/// 比較
///
///
///
public int CompareTo(Date val)
{
DateTime self = this;
DateTime inVal = val;
return self.CompareTo(inVal);
}
///
/// c#基本型の値を返す
///
///
public DateTime ToDateTime()
{
DateTime self = this;
return self;
}
///
/// 文字列
///
///
public string ToText(string format = "yyyy/MM/dd")
{
DateTime self = this;
if (format.Length > 0) { return self.ToString(format); }
return self.ToString();
}
///
/// SQL文の値セット
///
///
public string ToSqlValue()
{
DateTime self = this;
return Value <= MinValue || self.CompareTo(MinDate) <= 0 ? "NULL" : $"'{self.ToString("yyyy/MM/dd")}'";
}
///
/// 日付の±
///
///
///
public Date Add(int day)
{
DateTime self = this;
Date date = self.AddDays((double)day);
return date;
}
///
/// 日付引く
///
///
///
public int Subtract(Date beginDate)
{
DateTime end = this;
DateTime begin = beginDate;
TimeSpan ts = end - begin;
return ts.Days;
}
}
}