Calculating First Working Day adding holidays - algorithm

I need to find the first working day of the month. Ideally it should be 01/MM/YYYY but the condition is that it should not be a Saturday or Sunday or a day from the List of Holidays.
List being:
ListOfHoliday
Name Day
Someday 01/01/2021
Someday 01/02/2021
Someday 31/03/2021
Someday 04/07/2021
Someday 25/12/2021
I was trying to use the below algo:
FWD = Text.concat(01, DateTime.CurrentMonthOfToday, DateTime.CurrentyearOfToday)
foreach(holiday in ListOfHoliday)
{
if (FWD == holiday)
{
DateTime.AddDays(ConvertTo.DateTime(FWD), 1)
if (ConvertTo.DateTime(FWD).DayOftheWeek == Saturday)
{
FWD=ConvertTo.Text(DateTime.AddDays(ConvertTo.DateTime(FWD), 2));
} else
{
if ((ConvertTo.DateTime(FWD).DayOftheWeek == Sunday))
{
FWD=ConvertTo.Text(DateTime.AddDays(ConvertTo.DateTime(FWD), 2));
}
}
}
}
But the result I get for the current month is 01/01/0001.

Here is the python code to find the first working day of a given month.
from datetime import datetime, timedelta
def working_day(temp,holid):
if temp.strftime('%A') == 'Saturday':
temp += timedelta(2)
return working_day(temp,holid)
elif temp.strftime('%A') == 'Sunday' or temp in holid:
temp += timedelta(1)
return working_day(temp,holid)
else:
return temp.strftime('%A')
date_format = "%d/%m/%Y"
holidays = ['01/01/2021','01/02/2021','31/03/2021','04/07/2021','25/12/2021']
holid = []
for day in holidays:
holid.append(datetime.strptime(day, date_format))
month = int(input('Enter the month')) # input should be 1,2,3....12
today ='01/'+str(month)+'/2021'
today = datetime.strptime(today, date_format)
print(working_day(today, holid))
output:
Enter the month 12
Wednesday

Related

Writing a condition logics in kotlin in a better way in kotlin

I have a following condition logic and want to make it simple, is there a way to make it better without too much || && ? Some sample or example will be lovely! I would love to hear from you!
Logic that I want to achieve
From 0:00 on and 3 days before the birth month,
condition is the birth month is once or January 31st and
the present is run every December 29-31
val birthYear = child.birthYear!!.toInt()
val birthMonth = child.birthMonth!!.toInt()
val date = DateTime()
val year = date.year
val month = date.month
val day = date.day
val maxDayOfMonth = date.maxDayOfMonth
class DateTime {
private var mCalendar: Calendar = Calendar.getInstance().also { it.clear() }
val year: Int
get() = mCalendar.get(Calendar.YEAR)。。。。。
The following is the logic that I want to make it simpler
return date.year > child.birthYear!!.toInt() &&
((birthMonth == 1 && month == 12 || birthMonth - 1 == month)
&& day >= maxDayOfMonth - 2 || birthMonth == month)

Laravel carbon parse is giving incorrect output

I am working on chronic care health project in which i have to set future dates for patient injection. The doctor select start date for injection and then he select 3 days (i.e. Monday, Wednesday, Friday etc) which means patient will come 3 times a week.
The total injection are 20 that are divided by 3 times a week.
The problem that i am facing is that the parse function gives incorrect result if doctor select a day that matches today.
Lets suppose today is Wednesday and date is 2019-02-06 and doctor select 3 days that are:
Friday
Monday
Wednesday
Output should be:
- 2019-02-08
- 2019-02-11
- 2019-02-13
My code for controller:
if( $visitstart_date != null)
$startDate = Carbon::parse($visitstart_date);
else $startDate = Carbon::now();
if($perweek_visit1_day != '')
{ //Get date of selected day
$perweek_visit1_dayDate = $startDate->parse($perweek_visit1_day);
}
if($perweek_visit2_day != '')
{ //Get date of selected day
$perweek_visit2_dayDate = $startDate->parse($perweek_visit2_day);
}
if($perweek_visit3_day != '')
{ //Get date of selected day
$perweek_visit3_dayDate = $startDate->parse($perweek_visit3_day);
}
But when dump the 3 values it provide me :
2019-02-08
2019-02-11
2019-02-06 (incorrect)
For testing purpose I also parse the next visit date with each other like:
if( $visitstart_date != null)
$startDate = Carbon::parse($visitstart_date);
else $startDate = Carbon::now();
if($perweek_visit1_day != '')
{ //Get date of selected day
$perweek_visit1_dayDate = $startDate->parse($perweek_visit1_day);
}
if($perweek_visit2_day != '')
{ //Get date of selected day
$perweek_visit2_dayDate = $perweek_visit1_dayDate ->parse($perweek_visit2_day);
}
if($perweek_visit3_day != '')
{ //Get date of selected day
$perweek_visit3_dayDate = $perweek_visit2_dayDate ->parse($perweek_visit3_day);
}
but it again provided me the same output as :
- 2019-02-08
- 2019-02-11
- 2019-02-06
I don't know where i am going wrong, any support or help in this regard will be highly appreciated.
Thanks,

How to obtain first date of each month based on given year range in Nifi flow

I would like to know what could be the best way to obtain the starting date values for each month based on the date range.
For example: If I am given a year range of 2015-11-10 and 2018-01-15(format YYYY-mm-dd). Then I would like to extract following dates:
2015-12-01
2016-01-01
.
.
2018-01-01
You can try to use this flow for generating the first day of each month in the provided date range.
Overall flow
Step 1 Configuration: Start
Step 2 Configuration: Configure Date Range
Provide the start and end dates as configuration parameters via this step.
Step 3 Configuration: Generate First Dates For Months
This uses a Groovy script, which is provided below
Groovy script
flowFile = session.get();
if(!flowFile)
return;
DATE_FORMAT = 'yyyy-MM-dd';
startDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("start_date"));
endDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("end_date"));
allFirstDates = "";
Calendar calendar = Calendar.getInstance();
Set firstDaysOfMonths = new LinkedHashSet();
for (int i = 0; i <= endDate-startDate; i++) {
calendar.setTime(startDate.plus(i));
calendar.set(Calendar.DAY_OF_MONTH, 1);
firstDayOfMonth = calendar.getTime();
if (firstDayOfMonth.compareTo(startDate) >= 0) {
firstDaysOfMonths.add(calendar.getTime().format(DATE_FORMAT));
}
}
firstDaysOfMonths.each {
firstDayOfMonth -> allFirstDates = allFirstDates + firstDayOfMonth + "\n";
}
flowFile = session.putAttribute(flowFile,"all_first_dates", allFirstDates );
session.transfer(flowFile,REL_SUCCESS)
Step 4 Configuration: View Result
Output of run:
When the flow is run, the attribute all_first_dates will be populated with the first dates of each month in the date range.

Run trigger in partiuclar time interval only

I use this part of the code (and also time trigger) to run script every 5 minutes on "working hours" but now I need some adjustments
function officeHours(){
var nowH=new Date().getHours();
var nowD=new Date().getDay();
Logger.log('day : '+nowD+' Hours : '+nowH)
if(nowH>17||nowH<8||nowD==6||nowD==0){return}
Browser.msgBox('time to work !');//normally your real function should begin here...
}
Reference: Working hours only trigger
But how be more specific? e.g. set working hours from 8:30 to 17:30 or another e.g. when shift (working hours) starts at one day and ends next day (from 22:30 at day one to 06:30 at next day)?
You can check if the current hour is between the shift hours as in this example.
function officeHours(startShift, endShift){
startShift = startShift || 23;
endShift = endShift || 7;
var currentHour = new Date().getHours();
if (startShift > endShift) {
if (currentHour >= startShift || currentHour <= endShift) {
// run the trigger function
}
} else {
if (currentHour >= startShift && currentHour <= endShift) {
// run the trigger function
}
}
}

How to use date helper in CodeIgniter to find time conflict?

I have a database table
TABLE subject_loads
id (int)
subject_name (varchar)
time_start (time)
time_end (time)
time_diff (decimal)
When I save it on database, it will first check whether the time is not conflicting from the other time already inputed. If it's okay, then compute the time_start and time_end to give me a difference between the two.
Example, 7:30 AM - 8:30 AM is already in database, when i input 8:00 AM - 9:00 AM it will say "conflicting with the other time". Only I can input before 7:30 AM or after 8:30 AM that doesn't overlap from 7:30 AM - 8:30 AM.
Can someone help on how to do this?
First you need to check if overlapping values (compared to new value) already exist. You will do that with:
$query = $this->db->get_where('subject_loads', array('time_start >= ' => $time_start, 'time_end <= ' => $time_end));
if((int)$query->num_rows() > 0)
{
//similar values exist
}
else
{
//you are free to insert values
}
Second part of issue:
$hm1 = "2:12 AM";
$hm2 = "4:41 PM";
$e = conv($hm2) - conv($hm1);
echo $e;
function conv($time)
{
$expl_time = explode(' ', $time);
$t = explode(":", $expl_time[0]);
if ($expl_time[1] == 'PM' || $expl_time[1] == 'pm')
{
$t[0] += 12;
}
return ($t[0] + round($t[1]/60, 1, PHP_ROUND_HALF_UP));
}

Resources