/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-07-13 16:59:53
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-07-13 17:00:56
 * @FilePath: \marking_app\lib\utils\common_utils.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';

class CommonUtils {
  // md5 加密
  static String generateMD5(String data) {
    var content = new Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    // 这里其实就是 digest.toString()
    return hex.encode(digest.bytes);
  }

  ///补零
  static String zeroFill(int i) {
    return i >= 10 ? "$i" : "0$i";
  }
  /// 秒转时分秒
  static String second2HMS(int sec, {bool isEasy = false}) {
    String hms = "0";
    if (!isEasy) hms = "0秒";
    if (sec > 0) {
      int h = sec ~/ 3600;
      int m = (sec % 3600) ~/ 60;
      int s = sec % 60;
     /* hms = "${zeroFill(h)}:${zeroFill(m)}:${zeroFill(s)}";
      if (!isEasy) hms = "${zeroFill(h)}时${zeroFill(m)}分${zeroFill(s)}秒";*/
      if(h>0){
        hms = "$h时$m分$s秒";
      }else{
        if(m>0){
          hms = "$m分$s秒";
        }else{
          hms = "$s秒";
        }
      }
    }
    return hms;
  }

  static DateTime getWeekStartDate() {
    DateTime now = DateTime.now();
    int dayOfWeek = now.weekday; // 获取今天是周几（1代表周一，7代表周日）
    int diff = dayOfWeek - 1; // 计算今天距离周一的天数差
    if (diff < 0) {
      diff += 7; // 如果是周日，则需要加上一周的天数
    }
    return now.subtract(Duration(days: diff)); // 减去天数差，得到本周一的时间
  }

  static DateTime getWeekEndDate() {
    DateTime now = DateTime.now();
    int dayOfWeek = now.weekday; // 获取今天是周几
    int diff = 7 - dayOfWeek; // 计算今天距离周日的天数差
    if (diff == 0) {
      diff = 7; // 如果是周日，则加上一周的天数
    }
    return now.add(Duration(days: diff)); // 加上天数差减一，得到本周日的时间
  }


}
