df-helper 0.2.26

df helper tools db cache
Documentation
use chrono::prelude::*;

/// 年月日 时分秒
pub fn datetime(utc: bool) -> String {
    let fmt = "%Y-%m-%d %H:%M:%S";
    return {
        if utc {
            Utc::now().format(fmt).to_string()
        } else {
            Local::now().format(fmt).to_string()
        }
    };
}


/// 年月日
/// 年月日小时 转 年月日
pub fn to_date(datetime: &str) -> String {
    let fmt = "%Y-%m-%d %H:%M:%S";
    let datetime = NaiveDateTime::parse_from_str(datetime, fmt).unwrap();
    let fmt = "%Y-%m-%d";
    datetime.format(fmt).to_string()
}

/// 年月日小时 转 时分秒
pub fn to_time(datetime: &str) -> String {
    let fmt = "%Y-%m-%d %H:%M:%S";
    let datetime = NaiveDateTime::parse_from_str(datetime, fmt).unwrap();
    let fmt = "%H:%M:%S";
    datetime.format(fmt).to_string()
}

/// 年月日小时转时间戳
/// unit 单位 ms 毫秒 s 秒
/// utc 是否世界时间
pub fn to_timestamp(datetime: &str, unit: &str) -> i64 {
    let fmt = "%Y-%m-%d %H:%M:%S";
    let timestamp = NaiveDateTime::parse_from_str(datetime, fmt).unwrap();
    let local = Local.timestamp(0, 0).offset().utc_minus_local() as i64;
    match unit {
        "ms" => {
            timestamp.timestamp_millis() + local * 1000
        }
        _ => {
            timestamp.timestamp() + local
        }
    }
}
/// 业务单号生成
/// utc 是否世界时间
pub fn to_sn(utc: bool) -> String {
    let fmt = "%Y%m%d%H%M%S";
    return {
        if utc {
            Utc::now().format(fmt).to_string()
        } else {
            Local::now().format(fmt).to_string()
        }
    };
}

/// 格林尼治时间格式
pub fn to_gmt() -> String {
    let fmt = "%a %d %b %Y %H:%M:%S GMT";
    Utc::now().format(fmt).to_string()
}