How to calculate available vacation days? - c#

I need to calculate number of vacation days of employee.
For example, he is working from 2020-05-13 to 2020-08-26.
If his first work day was from 1 to 15 - he gets 2 vacation days;
From 15 to 25 - 1 day;
From 26 to 31 - 0 days.
He get's 2 vacation days in the begining of each month.
And he could be on vacations on this period, so I need to decreace this value from general number of his vacation days.
There is a table with working (1) and vacation (3) days. I need to use it to calculate, how much days of vacation left.

This will a good place to start, https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=netcore-3.1

Related

Find all available combinations of bookings that can be made from a list of time periods

I'm trying to find all available bookings that could be made given a list of time periods and a booking duration.
For example if I have a list of 10 minute timespans and a booking duration of 20 minutes, I need to find all combinations of the 10 minute timespans that would produce a 20 minute slot.
Example list of durations:
10:00 - 10:10
10:10 - 10:20
10:20 - 10:30
10:50 - 11:00
11:30 - 11:40
11:40 - 11:50
would produce the list:
10:00 - 10:20
10:10 - 10:30
11:30 - 11:50
For the solution the input doesn't need to be timespans, it could be datetimes or even start datetime and a duration, I just made it timespans for ease of reading.
I've solved this in the past with a while loop that keeps checking the next and next slots whilst keeping track of the running duration until it reaches one that is not consecutive or the duration has been hit/exceeded and it would do this for every element in the list. I'm not sure if that is the optimal way of doing it so I thought I'd ask here
Cheers

The more you add hours to a DateTime class the more time you loss

I have problem with DateTime.AddHours method in C#. I have noticed that the more hours I add to a certain date/time. The more days/hours I lose. For example:
DateTime DateOne = DateTime.Now;;
DateTime DateTwo = DateOne.AddHours(438000); // 50 years
I add 50 years in hours to DateOne class so i should get 3/10/2069.
However, if you print DateOne you will get: 3/10/2019 which is the current date
,but if you print DateTwo you will get: 2/25/2069.
That's over 10 days lost within 50 years change.
There has to be something wrong. What exactly I'm doing wrong ?
Consider the impact of leap years over 50 years. You should lose approximately 12 days in this calculation.
Use AddYears(50) for add years.
Hours:
438000 = 50 * 365 * 24
In the Gregorian calendar, each leap year has 366 days instead of 365, by extending February to 29 days rather than the common 28.
More: https://en.wikipedia.org/wiki/Leap_year

How to get the remaining time from 24 hours in C#

This is currentTime(EST) "2015-09-20 04:25:49.090". I need the below calculation.
Remaining time of "2015-09-20 04:25:49.090" this day (Ex : remaining time of this day is : 19 hours 35 minutes)
againg i have subtract from 24 hours - remainingTime (With minutes)
Could you please suggest me the solution for this?
Regards,
Arun
Sure - you do exactly as you've described, using TimeSpan. You can get the time of day from a DateTime using the TimeOfDay property, and then just subtract that from 24 hours:
// You could use TimeSpan.FromDays(1) as well
var remaining = TimeSpan.FromHours(24) - dateTime.TimeOfDay;
Now, one thing to be wary of is that this doesn't necessarily give you the amount of time left in the day - because "local" days can be different lengths depending on time zone changes (e.g. for daylight saving time). If you need to take that into account (e.g. that on November 1st at 00:30 in New York, there's 24 1/2 hours left in the day...). That's a more complicated question - especially if you also need to take account of time zones where the day doesn't always start at 00:00.
As for the second part of getting "24 hours - remaining time" - that's just "the time of day", as you've got 24 hours - (24 hours - x) which is just x.

Best pattern for a monthly billing cycle

I wrote some code for my new billing system. The purpose is to bill the customer on the same day each month. (not the 1st or last day of the month)
static bool NeedToBill(DateTime planLastBilled, DateTime cycleDate)
{
// is today the same date as the cycleDate AND is was the planLastBilled not the same day as today?
if (DateTime.UtcNow.Day.Equals(cycleDate.Day) && !DateTime.UtcNow.Day.Equals(planLastBilled))
return true;
else
return false;
}
The 2 pitfalls are:
If his cycleDate.Day is the 31 and the current month only has 29 days
cycleDate is Feb 29 2012 - he will only get billed on leap years
Is there a common best practice here?
so it seems like there's a bunch things to check
has this account already been billed this month?
does the cycle day exists in the current month
is the cycle day greater than or equal to the current date (this is ideal if
the transaction failed the day before)
Thanks!
Only allow the choice of a billing day between 1 - 28. In my experience this is how most credit card / loan companies deal with it when given a choice.
What does the same day each month mean?
If I am a customer, I want to be billed on the 16th each month. No problem. If I want to be billed on the 31st on each month the obvious issue is not all months have 31 days as you've pointed out in your question.
Why not check the current month for the number of days. If it has less than 31 days, make the last day of the month the bill date.
Is there more to the problem?
I'd say make him choose between 1-28, or any day but charge on the last day on the month if the current month has less days than the chosen day of month.
Ok, I believe I have been totally over-thinking this.
This is simple and covers everything:
bool NeedToBill = ((DateTime.UTCNow – LastBillDate) >= 30 Days)
It will not necessarily bill on the exact same day, however it's close enough.
This also adds flexibility if the transaction was denied for a day, or if the scheduled task was not ran for 1 day the next time it runs it will pick it up.

How to get list of monthly periods?

I have starting date and number of months. I need to create specific number of monthly periods for example:
var startingDate = new DateTime(2010,1,15);
var months = 3;
for (int i = 0; i < months; i++)
{
Console.WriteLine("{0} from {1} to {2}", i + 1, startingDate.AddMonths(i),
startingDate.AddMonths(i + 1).AddDays(-1));
}
OUTPUT:
1 from 2010-1-15 to 2010-2-14
2 from 2010-2-15 to 2010-3-14
3 from 2010-3-15 to 2010-4-14
In this case code is simple and it works.
However when startDate is DateTime(2010,1,31) result is:
OUTPUT:
1 from 2010-1-31 to 2010-2-27
2 from 2010-2-28 to 2010-3-30
3 from 2010-3-31 to 2010-4-29
Are these periods correct?
The periods do look funky but they are correct.
If your periods are starting on the last day of the month they will end on the second to last day of the next month.
You get to choose if they are right or wrong. Here is what I mean :
If you start your period on the 15th of January, are you starting 14 days after the first day of the month, or are you starting 16 days before the last?
It can get even more tricky. If the 15th is a Tuesday, is your period defined as starting the 3rd Tuesday of a given month?
There is a lot of literature about this in the financial community, since the Day Count Conventions, the Business Days, Rolling Conventions, etc. can make a lot of difference in the pricing of a financial product, and in the cash flows associated with it.

Categories

Resources