import 'package:date_format/date_format.dart';
import 'package:intl/intl.dart';

abstract class DataTimeUtils {
  /// 转成UTC字符串
  static String convertToUTCString(DateTime date) {
    // final utcFormatter = DateFormat('yyyy-MM-ddTHH:mm:ssZ');
    // final utcString = utcFormatter.format(date.toUtc());
    // return utcString;
    return formatDate(date.toUtc(),
        [yyyy, '-', mm, '-', dd, 'T', HH, ':', nn, ':', ss, ' ', Z]);
  }

  /// UTC字符串转DateTime
  static DateTime convertFromUTCString(String utcString) {
    final utcFormatter = DateFormat('yyyy-MM-ddTHH:mm:ssZ');
    final dateTime = utcFormatter.parseUtc(utcString);
    return dateTime;
  }

  static DateTime convertFromDateString(String utcString) {
    final utcFormatter = DateFormat('yyyy-MM-dd');
    final dateTime = utcFormatter.parseUtc(utcString);
    return dateTime;
  }

  /// 计算年龄
  static int calculateAge(DateTime birthDate) {
    DateTime currentDate = DateTime.now();
    int age = currentDate.year - birthDate.year;

    if (currentDate.month < birthDate.month ||
        (currentDate.month == birthDate.month &&
            currentDate.day < birthDate.day)) {
      age--;
    }
    return age;
  }
}
