how to solve using dart .Given a year, return the century it is in - flutter

Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.
Example
For year = 1905, the output should be
solution(year) = 20;
For year = 1700, the output should be
solution(year) = 17.
Input/Output
[execution time limit] 4 seconds (dart)
[input] integer year
A positive integer, designating the year.
Guaranteed constraints:
1 ≤ year ≤ 2005.
[output] integer
The number of the century the year is in.

int getCentury(int year) => (year - 1) ~/ 100 + 1;

is this what you want?
void main() {
int year1 = 1905;
int year2 = 1700;
print('year1 : ${getCentury(year1)}'); //20
print('year2 : ${getCentury(year2)}'); //17
}
int getCentury(int year){
if(1 > year || 2005 < year){
return 0;
}
int century = year ~/ 100;
int temp = year % 100;
if(temp != 0){
century += 1;
}
return century;
}

Related

How to calculate period between a specific date?

I'm studying Flutter and in my application I need to calculate how many days are left until the 17th of the next month. There is no way to have a specific date, so it would be crucial to have today's date and add 1 month and calculate how many days are left.
For example: Today is 2021-09-09 and there are 38 days to 2021-10-17. The function does not calculate the 17th of the current month, but the following month.
Any idea how to make a function that takes the current date, adds 1 month and calculates how many days are left until the 17th? Thanks.
Basically copy the DateTime object with 1 added to the current month number and the day set to the 17th. Then subtract the two DateTime objects and convert the resulting Duration to days. It's important to perform all calculations in UTC so that Daylight Saving Time adjustments aren't a factor.
/// Returns the number of days to the specified [day] in the month following
/// [startDate].
int daysToNextMonthDay(DateTime startDate, int day) {
// Recreate `startDate` as a UTC `DateTime`. We don't care about the
// time.
//
// Note that this isn't the same as `startDate.toUtc()`. We want a UTC
// `DateTime` with specific values, not necessarily the UTC time
// corresponding to same moment as `startDate`.
startDate = DateTime.utc(startDate.year, startDate.month, startDate.day);
var nextMonthDay = DateTime.utc(startDate.year, startDate.month + 1, day);
// Verify that the requested day exists in the month.
if (nextMonthDay.day != day) {
throw ArgumentError(
'Day $day does not exist for the month following ${startDate.month}',
);
}
return nextMonthDay.difference(startDate).inDays;
}
void main() {
var now = DateTime(2021, 9, 9);
print(daysToNextMonthDay(now, 17)); // Prints: 38
}
try this:
void main() {
DateTime date = DateTime.now();
print(daysLeft(date.subtract(Duration(days: 1))));
}
bool isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
int daysLeft(DateTime date){
int daysLeft = 0;
switch(date.month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: daysLeft = 31 - date.day + 17; break;
case 4:
case 6:
case 9:
case 11: daysLeft = 30 - date.day + 17; break;
case 2: if(isLeapYear(date.year)){
daysLeft = 29 - date.day + 17;
break;
}else{
daysLeft = 28 - date.day + 17;
}
}
return daysLeft;
}

How many weeks in a month with X weekday in them?

I'm looking for a way to get the number of weeks with X weekday in them a month has. For example:- How many weeks are there in march with Wednesday in them? Answer - 5.
I would be happy with a general answer but it would be great to know if there's an efficient way to do that using dart. Thank you.
Try this...
int weeksCountOfWeekday(int weekdayToCnt, int month, int year) {
assert(weekdayToCnt >= 1 && weekdayToCnt <= 7);
final DateTime startDate = DateTime(year, month);
final DateTime endDate =
DateTime(year, month + 1).subtract(const Duration(days: 1));
int res = 1;
int dayOfWeekday;
if (weekdayToCnt < startDate.weekday) {
dayOfWeekday = startDate.day + 7 - (startDate.weekday - weekdayToCnt);
} else {
dayOfWeekday = 1 + weekdayToCnt - startDate.weekday;
}
while (dayOfWeekday + 7 <= endDate.day) {
res++;
dayOfWeekday += 7;
}
return res;
}

Flutter: Find the difference between two dates in Years, Months including leap years

I am working on an application in flutter where I have to get the difference between two dates in Years, Months and Dates
like below Ex.
final _date1 = DateTime(2019, 10, 15);
final _date2 = DateTime(2020, 12, 20);
Answer Difference = 1 Year, 2 Month, 5 Days
I searched a lot but unfortunately din't get a proper solution to calculate the difference between two dates in Years, Months including leap years.
Note: I know how to get the difference between two dates in Days i.e
final _bd = DateTime(2020, 10, 12);
final _date = DateTime.now();
final _difference = _date.difference(_bd).inDays;
You may customize the following code as per your requirement to return a string or object or make it an extension:
void getDiffYMD(DateTime then, DateTime now) {
int years = now.year - then.year;
int months = now.month - then.month;
int days = now.day - then.day;
if (months < 0 || (months == 0 && days < 0)) {
years--;
months += (days < 0 ? 11 : 12);
}
if (days < 0) {
final monthAgo = DateTime(now.year, now.month - 1, then.day);
days = now.difference(monthAgo).inDays + 1;
}
print('$years years $months months $days days');
}

Formula of Hijri date

What is the formula of converting "Time since epoch" stamp such as 1525249213 to Hijri date (Year - Month - Day)?
I know there would be one day inaccuracy which is OK for me.
The algorithm I found is to convert the Date to a Julian Date then convert the julian date to Hijri.
P.S. I can not remember where I found it. I am not responsible for any error or miss-accuracy or an consequence bla bla bla of using this code.
unsigned long gregorian_to_julian(const int gyear, const int gmonth, const int gday) {
if (gmonth < 3) {
gyear -= 1;
gmonth += 12;
}
auto a = int(gyear / 100.0f);
auto b = (gyear == 1582 && (gmonth > 10 || (gmonth == 10 && gday > 4)) ? -10 :
(gyear == 1582 && gmonth == 10 ? 0 :
(gyear < 1583 ? 0 : 2 - a + int(a / 4.0f))));
return int(365.25f * (gyear + 4716)) + int(30.6001f * (gmonth + 1)) + gday + b - 1524;
}
std::array<int,3> julian_to_hijri(const unsigned long julian_datestamp) {
std::array<int,3> result;
auto y = 10631.0f / 30.0f;
auto epoch_astro = 1948084;
auto shift1 = 8.01f / 60.0f;
auto z = julian_day - epoch_astro;
auto cyc = int(z / 10631.0f);
z = z - 10631 * cyc;
auto j = int((z - shift1) / y);
z = z - int(j * y + shift1);
result[0] = 30 * cyc + j; //Hijri Year
result[1]= int((z + 28.5001f) / 29.5f); //Hijri Month
if (result[1] == 13) {
result[1]= 12;
}
result[2] = z - int(29.5001f * result[1]- 29);// Hijri day
return result;
}
P.S. there will be +-1 one day error in the result Hijri date.

Convert time/date to epoch (seconds since 1970)

I'm trying to write a function to convert a time & date into epoch seconds, it's for a small system that doesn't have the usual time_t library functions. I've got this code below but the calculation is a bit off, can anyone see what's wrong?
long getSecondsSinceEpoch(int h, int m, int s, int day, int month, int year) {
int i,leapDays;
long days;
long seconds;
const static DAYS_IN_MONTH[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
leapDays = 0;
days = (year - 1970) * 365;
for (i = year; i>1970; i--){
if ((i%4)==0) {
leapDays++;
}
}
days += leapDays;
for (i = 1;i < month;i++) {
days += DAYS_IN_MONTH[i - 1];
}
days += day;
seconds = days * 86400;
seconds += (h * 3600);
seconds += (m * 60);
seconds += s;
return seconds;
}
One error is maybe, that you don't take into consideration if you are before February 29th or not when adding the leap days. But I am not sure if this is the only mistake.
EDIT: I think I found the second error: You add all day to days. You should add day - 1 to days, as 08:00 of January 1st is only 8 hours from the start of the month and not 24+8 hours from it.