DateTime

//时间、数值工具类//
public class _Utility:MonoBehaviour{
void Start () {
}
void Update () {
}

public static int getDstNum(int num)
{
    return (num+198012)^654321;
}
public static int getSrcNum(int num)
{
    return (num^654321)-198012;
}

//日期相关*******************************************************************************************************************//
//现在到1970的时间,精确到毫秒//
//static Stopwatch stopwatch = new Stopwatch();
public static double currentTimeMillis()
{
    TimeSpan ts=DateTime.UtcNow - DateTime.Parse("1970-1-1");
    return ts.TotalMilliseconds;
}
//获得当前时间DateTime对象//
public static System.DateTime getTime()
{
    //System.DateTime.Now//本地时间,12小时制的//
    //System.DateTime.Now.ToString("yyyyMMddHHmmss")//军事日期时间//
    //System.DateTime.UtcNow//UTC时间//
    //GUILayout.SelectionGrid(System.DateTime.Now.Month-1,Months,3);
    //GUILayout.SelectionGrid(System.DateTime.Now.Day-1,Days,10);
    //GUILayout.Box(str+dNow.Now.Year+"年"+dNow.Now.Month+"月"+dNow.Now.Day+" 日"+" "+dNow.Now.Hour+"时"+dNow.Now.Minute+"分"+dNow.Now.Second+"秒");
    System.DateTime now = System.DateTime.Now;
    return now;
}
//返回当前时间与oldTime的时间差,以秒为单位//
public static System.TimeSpan getSpanTime(System.DateTime oldTime)
{
    System.TimeSpan span = getTime() - oldTime;
    return span;
}
//返回当前时间与oldTime的时间差,以秒为单位//
public static int getSpanTimeSeconds(System.DateTime oldTime)
{
    System.TimeSpan span = getTime() - oldTime;
    return (int)span.TotalSeconds;
}
//当前时间到未来一个时间,还剩多少时间//
public static System.TimeSpan getRemainTime(System.DateTime futureTime)
{
    System.TimeSpan remain = futureTime - getTime();
    return remain;
}
//DateTime转换为19700101,单位 秒//
public static double ConvertDateTime2Int(System.DateTime time)  
{  
    double intResult = 0;  
    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));  
    intResult = (time - startTime).TotalSeconds;  
    return intResult;  
}
//19700101,单位 秒转换为DateTime//
public static System.DateTime ConvertInt2DateTime(double d)
{  
    System.DateTime time = System.DateTime.MinValue;  
    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
    time = startTime.AddSeconds(d);  
    return time;
}
//判断是不是今天//
public static bool timeIsToday(long time)
{
    //+8时区//
    double curTime = currentTimeMillis() / 1000;
    //curTime += 8*3600;

    DateTime date = ConvertInt2DateTime (time);
    DateTime curdate = ConvertInt2DateTime (curTime);

    if(date.Year == curdate.Year && date.Month == curdate.Month && date.Day == curdate.Day)
    {
        return true;
    }
    return false;
}
//判断是不是过了cd,参数cd,精度,次数,时间偏移,被比较的时间//
public static bool isCrossResetPoint(int cd,int accuracy,int count,int offset,int lastSecond)
{
        double curTime = currentTimeMillis() / 1000;
        int cur = (int)(curTime - offset);
        int last = lastSecond - offset;

    if (accuracy == 1) //精度1秒//
    {
        if((cur - last) >= cd * accuracy)
        {
            return true;
        }
    }else if(accuracy == 86400)//精度1天//
    {
        if(timeIsToday(lastSecond))
        {
            return false;
        }else
        {
            return true;
        }
    }else//精度其他//
    {
        return false;
    }
    return false;
}
//某个日期是否 > 某个日期//
public static bool IsGreaterDate(DateTime myDate, DateTime compareDate)
{
    return myDate.CompareTo(compareDate) >= 0;
}
//某个日期是否 < 某个日期//
public static bool IsSmallerDate(DateTime myDate, DateTime compareDate)
{
    return myDate.CompareTo(compareDate) <= 0;
}
### datetime 模块的基本使用 Python 的 `datetime` 模块提供了处理日期和时间的功能,能够轻松地进行日期与时间的创建、格式化、解析以及运算。以下是关于 `datetime` 模块的一些关键用法和信息。 #### 1. 获取当前日期和时间 可以通过 `datetime.now()` 方法获取当前的日期和时间。该方法返回一个 `datetime` 对象,表示当前的本地时间[^1]。 ```python from datetime import datetime now = datetime.now() # 获取当前日期和时间 print(now) # 输出示例:2015-05-18 16:28:07.198690 print(type(now)) # <class 'datetime.datetime'> ``` #### 2. 从字符串解析日期和时间 `datetime.strptime()` 是一个类方法,用于将日期和时间的字符串按照指定的格式解析为 `datetime` 对象[^2]。 ```python from datetime import datetime date_string = "2023-10-05 14:30:00" date_format = "%Y-%m-%d %H:%M:%S" parsed_date = datetime.strptime(date_string, date_format) print(parsed_date) # 输出:2023-10-05 14:30:00 ``` #### 3. 格式化日期和时间为字符串 `strftime()` 方法可以将 `datetime` 对象转换为指定格式的字符串。 ```python from datetime import datetime now = datetime.now() formatted_date = now.strftime("%Y-%m-%d %H:%M:%S") print(formatted_date) # 输出示例:2023-10-05 16:45:30 ``` #### 4. 时间差计算 `timedelta` 类型可以用于表示两个 `datetime` 对象之间的时间差。 ```python from datetime import datetime, timedelta now = datetime.now() future_date = now + timedelta(days=5, hours=3) # 当前时间加5天3小时 print(future_date) ``` #### 5. 日期和时间的属性访问 `datetime` 对象包含多个属性,可以直接访问年、月、日、时、分、秒等信息。 ```python from datetime import datetime now = datetime.now() print(now.year) # 输出年份 print(now.month) # 输出月份 print(now.day) # 输出日期 print(now.hour) # 输出小时 print(now.minute) # 输出分钟 print(now.second) # 输出秒数 ``` ### 注意事项 - 在跨时区操作时,建议使用 `pytz` 或 `zoneinfo` 模块来处理时区问题。 - 如果需要更高效的日期时间处理,可以考虑使用第三方库如 `pandas` 或 `arrow`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值