【发布时间】:2021-07-07 13:11:32
【问题描述】:
我们如何将当前日期时间转换为颤振/飞镖中的时间?已经尝试了不同的公式和计算,没有奏效。 http://www.datetimetoticks-converter.com/
【问题讨论】:
我们如何将当前日期时间转换为颤振/飞镖中的时间?已经尝试了不同的公式和计算,没有奏效。 http://www.datetimetoticks-converter.com/
【问题讨论】:
我只是为了约会。
您可以将 C# 中的实现复制到 dart 中。
void main() {
final dateTime = DateTime.now();
print(DateTimeUtils.dateToTicks(
dateTime.year,
dateTime.month,
dateTime.day,
));
}
class DateTimeUtils {
static const int TicksPerMillisecond = 10000;
static const int TicksPerSecond = TicksPerMillisecond * 1000;
static const int TicksPerMinute = TicksPerSecond * 60;
static const int TicksPerHour = TicksPerMinute * 60;
static const int TicksPerDay = TicksPerHour * 24;
static List<int> daysToMonth365 = [
0,
31,
59,
90,
120,
151,
181,
212,
243,
273,
304,
334,
365,
];
static List<int> daysToMonth366 = [
0,
31,
60,
91,
121,
152,
182,
213,
244,
274,
305,
335,
366,
];
static int dateToTicks(int year, int month, int day) {
if (year >= 1 && year <= 9999 && month >= 1 && month <= 12) {
List<int> days = isLeapYear(year) ? daysToMonth366 : daysToMonth365;
if (day >= 1 && day <= days[month] - days[month - 1]) {
int y = year - 1;
double n = y * 365 + y / 4 - y / 100 + y / 400 + days[month - 1] + day - 1;
return n * TicksPerDay as int;
}
}
throw new ArgumentError();
}
static bool isLeapYear(int year) {
if (year < 1 || year > 9999) {
throw new ArgumentError();
}
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
}
【讨论】: