toNepaliDateTime method
Converts the DateTime to NepaliDateTime.
Implementation
NepaliDateTime toNepaliDateTime() {
const nepalTzOffset = Duration(hours: 5, minutes: 45);
final now = toUtc().add(nepalTzOffset);
// Setting nepali reference to 1970/1/1 with english date 1913/4/13
var nepaliYear = 1970;
var nepaliMonth = 1;
var nepaliDay = 1;
// Time was causing error while differencing dates.
final date = DateTime(now.year, now.month, now.day);
var difference = date.difference(DateTime(1913, 4, 13)).inDays;
// 1986-1-1's duration is only 23 hours 45 minutes in Dart for Nepal Time.
// This can be tested using
// `print(DateTime(1986,1,2).difference(DateTime(1986,1,1)))`;
// So, in order to compensate it one extra day is added from this date.
if (date.timeZoneOffset == nepalTzOffset && date.isAfter(DateTime(1986))) {
difference += 1;
}
// Getting nepali year until the difference remains less than 365
var daysInYear = _nepaliYears[nepaliYear]!.first;
while (difference >= daysInYear) {
nepaliYear += 1;
difference -= daysInYear;
daysInYear = _nepaliYears[nepaliYear]!.first;
}
// Getting nepali month until the difference remains less than 31
var daysInMonth = _nepaliYears[nepaliYear]![nepaliMonth];
while (difference >= daysInMonth) {
difference -= daysInMonth;
nepaliMonth += 1;
daysInMonth = _nepaliYears[nepaliYear]![nepaliMonth];
}
// Remaining days is the actual day;
nepaliDay += difference;
return NepaliDateTime(
nepaliYear,
nepaliMonth,
nepaliDay,
now.hour,
now.minute,
now.second,
now.millisecond,
now.microsecond,
);
}