Run trigger in partiuclar time interval only - time

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
}
}
}

Related

Calculating First Working Day adding holidays

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

Time comparison in html using NGIF Condition - Angular

I am in requirement for solution where I have one Admin posts application for an Android app. I have placed a delete button for post. The requirement is that delete button must be shown for 5 minutes from time of posting.
Here is the ngif condition which I have used..
*ngIf="((((post.CreatedDate | date:'dd/MM/yyyy') == (PresentDate | date:'dd/MM/yyyy')) && ((post.CreatedDate | date:'HH') == (post.CreatedDate | date:'HH')))&&((post.CreatedDate | date:'mm')< (time)))"
Code in TS page for present time + 5 minutes
const d: Date = new Date();
this.PresentDate = d;
var x = new Date();
d.getHours(); // => 9
d.getMinutes(); // => 30
this.time = d.getMinutes() +5;
this.hours = d.getHours();
Please help with the solution
Long expression in html is not good practice.
*ngIf="canDeletePost(post)"
canDeletePost(post) {
return Date.now() - post.CreatedDate.getTime() < 5 * 60 * 1000;
}
If CreatedDate is Js date. 5 * 60 * 1000 - 5 min in milliseconds. Actually, have a method in ngIf is not good practice also.
Anyway, you don't need date pipe. Pipes is used for changing view.

Observable Interval without drift

I am using Observable.interval to schedule code execuiton at specified times:
let obs = Observable.interval(50).subscribe(()=>{
console.log(this.currentFrame+" "+new Date().getTime());
this.currentFrame++
});
This is the output. As you can see, after 6 iterations I already have a 10ms drift. How can I use Observable.interval, but also specify that it needs to recalculate next iteration based on the current drift?
0 1513972329849
1 1513972329901
2 1513972329952
3 1513972330004
4 1513972330057
5 1513972330110
Until #cartant's fix gets repulled, you could use expand and create the behavior yourself. Assuming delay will always drift forward, try the following:
function correcting_interval(interval) {
const start_time = new Date().getTime();
return Observable.of(-1)
.expand(v => Observable.of(v + 1).delay(interval - (new Date().getTime() - start_time) % interval))
.skip(1);
}

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));
}

In windowsphone converting current time into millisecond

i have a time format like this:
string s = DateTime.Now.ToString();
which gives me output like
11/29/2013 6:26:13PM
Now how can i convert this output into millisecond in windowsPhone???
Updated:
First i want to save the current time when the user launch my app. after that whenever the user launch my app again then i also get the time and compare the current launching time with previously stored time and check whether the time difference becomes "one day" or not.
For this comparison i need to covert 11/29/2013 6:26:13PM this into millisecond.
Another question tell me how can i convert "6:26:13PM" only this into millisecond??
If I understood correctly just do this:
Create a date from your input:
DateTime yourInitialDateTime = DateTime.Parse("11/29/2013 6:26:13PM");
After that
TimeSpan span = DateTime.Now - yourInitialDateTime;
So in span.TotalDays you will have how many days has passed.
Edit
If you have only the time of day and want to know the millisecond of that time you must add a date and subtract it with hour 0:00:00 like this:
string dummyDate = "01/01/0001";
DateTime end = DateTime.Parse(dummyDate + " " + "6:26:13PM");
var milli = end.Subtract(new DateTime()).TotalMilliseconds;
That is it.
Try this.
var ThatDay = DateTime.Now.AddDays(-1); //This is hard coded but you have to get from where you are storing.
var Today = DateTime.Now;
var Diff = (Today - ThatDay).Milliseconds;
var FriendlyDiff = (Today - ThatDay).ToFriendlyDisplay(5);
public static class TimeSpanExtensions
{
private enum TimeSpanElement
{
Millisecond,
Second,
Minute,
Hour,
Day
}
public static string ToFriendlyDisplay(this TimeSpan timeSpan, int maxNrOfElements)
{
maxNrOfElements = Math.Max(Math.Min(maxNrOfElements, 5), 1);
var parts = new[]
{
Tuple.Create(TimeSpanElement.Day, timeSpan.Days),
Tuple.Create(TimeSpanElement.Hour, timeSpan.Hours),
Tuple.Create(TimeSpanElement.Minute, timeSpan.Minutes),
Tuple.Create(TimeSpanElement.Second, timeSpan.Seconds),
Tuple.Create(TimeSpanElement.Millisecond, timeSpan.Milliseconds)
}
.SkipWhile(i => i.Item2 <= 0)
.Take(maxNrOfElements);
return string.Join(", ", parts.Select(p => string.Format("{0} {1}{2}", p.Item2, p.Item1, p.Item2 > 1 ? "s" : string.Empty)));
}
}

Resources