Check current time is between two times - date

I am trying to check if the current time if between two predermined times and that the day is Monday, Tuesday, Wednesday or Thursday. Something like:
if day = monday, tuesday, wednesday or thursday
if current_time > 6:00 PM and current_time < 6:15 PM
then do something
Here is what I have so far, the only bit left is setting the two times I want to check between:
weekday = datetime.datetime.today().weekday()
hour = datetime.datetime.time(datetime.datetime.now())
if weekday < 5:
if hour

You may use datetime.weekday() method and attributes: datetime.hour and datetime.minute.
now = datetime.datetime.now()
if now.weekday() < 5 and now.hour == 18 and 0 <= now.minute <= 15:
do_something()

Related

How do I count discontinued dates in PowerBI?

I want to count the discontinued dates per ID with filter "FilterByValue" by 1.
What I mean by discontinued dates.
04.01.2021
05.01.2021
06.01.2021
08.01.2021
07.01.2021 date would be missing to be a continued date when a day between dates is missing its discontinued.
Dates have also to be distinct and within the last 90 Days.
RowID is just for explanation purposes.
RowID
ID
FilterByValue
Date
1
1
1
Monday, 4. January 2021
2
1
1
Tuesday, 5. January 2021
3
1
1
Tuesday, 5. January 2021
4
1
1
Wednesday, 6. January 2021
5
1
1
Monday, 11. January 2021
6
1
99
Friday, 8. January 2021
7
2
1
Tuesday, 9. February 2021
8
2
1
Wednesday, 10. February 2021
9
2
1
Thursday, 11. March 2021
10
2
1
Friday, 12. March 2021
11
2
1
Monday, 15. March 2021
12
2
1
Tuesday, 16. March 2021
13
2
99
Sunday, 14. March 2021
14
2
1
Wednesday, 14. April 2021
What I want to achieve:
RowID
ID
CountDiscontinuedDates
1
1
2
2
2
4
What I tried, I think is a bad/ not helping approach:
discontinuesDates = COUNTAX(FILTER(TableName, [ID]=1 && TableName[Date] > (TODAY()-90) && OR (DATEADD( TableName[Date] = (TableName[Datum],1,DAY), DATEADD( TableName[Date] = (TableName[Datum],-1,DAY) ) && TableName[ID] = EARLIER(TableName[ID]) && TableName[Date] = TableName[Date] ), TableName[ID])
discontinuesDates = CALCULATE(COUNT(TableName[ID]), FILTER(TableName, TableName[FilterByValue]=34 && TableName[ID] = EARLIER( TableName[ID]) && DATEADD( TableName[Date],1,DAY) <> EARLIER( TableName[Date])) )
Maybe something like this:
Assuming that FilterByValue is available to use:
_Foo =
CALCULATE (
DISTINCTCOUNT ( DiscontinuedDatesData[Date] ),
FILTER (
DiscontinuedDatesData,
'DiscontinuedDatesData'[Date] >= CALCULATE (MIN ( 'DiscontinuedDatesData'[DATE] ), 'DiscontinuedDatesData', DiscontinuedDatesData[FilterByValue] = 99)
&& 'DiscontinuedDatesData'[Date] >= TODAY() - 90
)
)

Headcount total for the past 12 months

I have the below query which gives me the headcount per month when I select a month from the date table which is fine and works correctly.
I would like the average headcount for the past 12 months from the selected date e.g if i select Aug 2020, the average should be a sum of headcount from July 2019 - Aug 2020
Active Employees =
var currentdate = 'Date'[Max Date]
RETURN
CALCULATE(
DISTINCTCOUNT('Joined Query'[EMP_NO]),
'Joined Query'[DATE_OF_EMPLOYMENT] <= currentdate,
or(
ISBLANK('Joined Query'[DATE_OF_LEAVING]),
'Joined Query'[DATE_OF_LEAVING] > currentdate
)
)
You can reuse your headcount measure per month like this:
average 12 months =
VAR currentdate = SELECTEDVALUE('Date'[Max Date])
RETURN AVERAGEX(
SUMMARIZE(
FILTER(ALL('Date'),
'Date'[Max Date] <= currentdate && 'Date'[Max Date] >= EDATE(currentDate,-11)),
'Date'[Max Date],
"headcount", [Active Employees]
), [headcount])
Please note, -11 gives you the date 12 months ago in regards to the current selected date. In your question you say if you select August you want to see from July last year, and that would actualy be 14 months, not 12. If that's your use case you need to change it to -13.
Create 2 following measures for 12 month start and end date-
12 Month End Date =
VAR D1 =
DATEVALUE(
SELECTEDVALUE('Date'[Max Date].[MonthNo])
& "/1/"
& SELECTEDVALUE('Date'[Max Date].[Year])
)
RETURN D1-1
12 Month Start Date =
VAR D1 =
DATEVALUE(
SELECTEDVALUE('Date'[Max Date].[MonthNo])
& "/1/"
& SELECTEDVALUE('Date'[Max Date].[Year])
)
RETURN EDATE(D1,-12)
Now create this following measure for average calculation-
12_month_average =
(
CALCULATE(
DISTINCTCOUNT('Joined Query'[EMP_NO]),
DATESBETWEEN(
'Date'[Max Date],
[12 Month Start Date],
[12 Month End Date]
)
) + 0
) / 12

how to create 30 minutes time slots in flutter/dart

How to create time slots in flutter / dart?
I want to make a list of 30 minutes time slots from given start time and end time.
Example:
start time 9:00 AM
end Time is 10:00 PM
list will be
List<String> timeSlots = ['9:30 AM','10:00 AM','10:30 AM','11:00 AM'....]
Is there any library to solve this problem?
You can create this with a generator, though the syntax is a little ugly since the TimeOfDay class added by Flutter isn't very powerful:
Iterable<TimeOfDay> getTimes(TimeOfDay startTime, TimeOfDay endTime, Duration step) sync* {
var hour = startTime.hour;
var minute = startTime.minute;
do {
yield TimeOfDay(hour: hour, minute: minute);
minute += step.inMinutes;
while (minute >= 60) {
minute -= 60;
hour++;
}
} while (hour < endTime.hour ||
(hour == endTime.hour && minute <= endTime.minute));
}
Usage:
final startTime = TimeOfDay(hour: 9, minute: 0);
final endTime = TimeOfDay(hour: 22, minute: 0);
final step = Duration(minutes: 30);
final times = getTimes(startTime, endTime, step)
.map((tod) => tod.format(context))
.toList();
print(times);
// Results:
// [9:00 AM, 9:30 AM, 10:00 AM, 10:30 AM, 11:00 AM, 11:30 AM, 12:00 PM, 12:30 PM, 1:00 PM, 1:30 PM, 2:00 PM, 2:30 PM, 3:00 PM, 3:30 PM, 4:00 PM, 4:30 PM, 5:00 PM, 5:30 PM, 6:00 PM, 6:30 PM, 7:00 PM, 7:30 PM, 8:00 PM, 8:30 PM, 9:00 PM, 9:30 PM, 10:00 PM]
Create a list with length : List.generate
List<String>.generate(3, (int index) => "time ${index + 1}");
// ["time 1", "time 2", "time 3"]

Get week number, with weeks starting on Sunday, like Excel WEEKNUM

In PostgreSQL (I'm on version 9.6.6), what's the simplest way to get the week number, starting on Sunday?
DATE_PART('week',x) returns:
The number of the ISO 8601 week-numbering week of the year. By definition, ISO weeks start on Mondays and the first week of a year contains January 4 of that year. In other words, the first Thursday of a year is in week 1 of that year. (doc)
Say my query is like:
WITH dates as (SELECT generate_series(timestamp '2014-01-01',
timestamp '2014-01-31',
interval '1 day'
)::date AS date
)
SELECT
date,
TO_CHAR(date,'Day') AS dayname,
DATE_PART('week',date) AS weekofyear
FROM dates
Returns:
date dayname weekofyear
--------------------------------
2014-01-01 Wednesday 1
2014-01-02 Thursday 1
2014-01-03 Friday 1
2014-01-04 Saturday 1
2014-01-05 Sunday 1 <- I want this to be 2
2014-01-06 Monday 2
2014-01-07 Tuesday 2
2014-01-08 Wednesday 2
So far I have tried:
SELECT
date,
TO_CHAR(date,'Day') AS dayname,
DATE_PART('week',date) AS week_iso,
DATE_PART('week',date + interval '1 day') AS week_alt
FROM dates
which won't quite work if the year begins on a Sunday.
Also, I want week 1 to contain January 1 of that year. So if January 1 is a Saturday, I want week 1 to be one day long (instead of being week 53 in the ISO style). This behavior is consistent with the Excel WEEKNUM function.
To get the week number of the year, with weeks starting on Sunday, we need to know how many Sundays between the first day of the year and the target date.
I adapted the solution here by #Erwin Brandstetter. This solution counts Sundays inclusive of the first day of the year and exclusive of the target date.
Then, because I want the first (partial) week to be week one (not zero), I need to add 1 unless the first day of the year is a Sunday (in which case it's already week one).
WITH dates as (SELECT generate_series(timestamp '2014-01-01',
timestamp '2014-01-31',
interval '1 day'
)::date AS date
)
SELECT
date,
TO_CHAR(date,'Day') AS dayname,
DATE_PART('week',date) AS week_iso,
((date - DATE_TRUNC('year',date)::date) + DATE_PART('isodow', DATE_TRUNC('year',date)) )::int / 7
+ CASE WHEN DATE_PART('isodow', DATE_TRUNC('year',date)) = 7 THEN 0 ELSE 1 END
AS week_sundays
FROM dates
Returns
date dayname weekofyear week_sundays
--------------------------------
2014-01-01 Wednesday 1 1
2014-01-02 Thursday 1 1
2014-01-03 Friday 1 1
2014-01-04 Saturday 1 1
2014-01-05 Sunday 1 2
2014-01-06 Monday 2 2
2014-01-07 Tuesday 2 2
To show how this works for years starting on Sunday:
2017-01-01 Sunday 52 1
2017-01-02 Monday 1 1
2017-01-03 Tuesday 1 1
2017-01-04 Wednesday 1 1
2017-01-05 Thursday 1 1
2017-01-06 Friday 1 1
2017-01-07 Saturday 1 1
2017-01-08 Sunday 1 2
The task is not as daunting as it first appears. It mainly requires finding the first Sun on or after the 1-Jan. That date becomes the last day of the first week. From there calculation of subsequent weeks is merely. a matter of addition. The other significant point is with week definition there will always be 53 week per year and the last day of the last week is 31-Dec. The following generates an annual calendar for this week definition.
create or replace function non_standard_cal(year_in integer)
returns table (week_number integer, first_day_of_week date, last_day_of_week date)
language sql immutable leakproof strict rows 53
as $$
with recursive cal as
(select 1 wk, d1 start_of_week, ds end_of_week, de stop_date
from (select d1+substring( '0654321'
, extract(dow from d1)::integer+1
, 1)::integer ds
, d1, de
from ( select make_date (year_in, 1,1) d1
, make_date (year_in+1, 1,1) -1 de
) a
) b
union all
select wk+1, end_of_week+1, case when end_of_week+7 > stop_date
then stop_date
else end_of_week+7
end
, stop_date
from cal
where wk < 53
)
select wk, start_of_week, end_of_week from cal;
$$ ;
As a general rule I avoid magic numbers, but sometimes they're useful; as in this case. In magic number (actually a string) '0654321' each digit represents the number of days needed to reach the first Mon on or after 1-Jan when indexed by the standard day numbering system (0-6 as Sun-Sat). The result is the Mon being the last day of the first week. That generatess the 1st row of the recursive CTE. The remaining rows just add the appropriate number days for each week until the 53 weeks have been generated. The following shows the years needed to ensure each day of week gets it's turn to 1-Jan (yea some days duplicate). Run individual years to validate its calendar.
do $$
declare
cal record;
yr_cal cursor (yr integer) for
select * from non_standard_cal(2000+yr) limit 1;
begin
for yr in 18 .. 26
loop
open yr_cal(yr);
fetch yr_cal into cal;
raise notice 'For Year: %, week: %, first_day: %, Last_day: %, First day is: %'
, 2000+yr
,cal.week_number
,cal.first_day_of_week
,cal.last_day_of_week
,to_char(cal.first_day_of_week, 'Day');
close yr_cal;
end loop;
end; $$;
Following may work - tested with two cases in mind:
WITH dates as (SELECT generate_series(timestamp '2014-01-01',
timestamp '2014-01-10',
interval '1 day'
)::date AS date
union
SELECT generate_series(timestamp '2017-01-01',
timestamp '2017-01-10',
interval '1 day'
)::date AS date
)
, alt as (
SELECT
date,
TO_CHAR(date,'Day') AS dayname,
DATE_PART('week',date) AS week_iso,
DATE_PART('week',date + interval '1 day') AS week_alt
FROM dates
)
select date, dayname,
week_iso, week_alt, case when week_alt <> week_iso
then week_alt
else week_iso end as expected_week
from alt
order by date
Output:
date dayname week_iso week_alt expected_week
2014-01-01 Wednesday 1 1 1
2014-01-02 Thursday 1 1 1
2014-01-03 Friday 1 1 1
2014-01-04 Saturday 1 1 1
2014-01-05 Sunday 1 2 2
2014-01-06 Monday 2 2 2
2014-01-07 Tuesday 2 2 2
....
2017-01-01 Sunday 52 1 1
2017-01-02 Monday 1 1 1
2017-01-03 Tuesday 1 1 1
2017-01-04 Wednesday 1 1 1
2017-01-05 Thursday 1 1 1
2017-01-06 Friday 1 1 1
2017-01-07 Saturday 1 1 1
2017-01-08 Sunday 1 2 2
This query works perfectly replacing monday with sunday as the start of the week.
QUERY
SELECT CASE WHEN EXTRACT(day from '2014-01-05'::date)=4 AND
EXTRACT(month from '2014-01-05'::date)=1 THEN date_part('week',
'2014-01-05'::date) ELSE date_part('week', '2014-01-05'::date + 1)
END;
OUTPUT
date_part
-----------
2
(1 row)

Finding how many times a day will appear from now until target date

I am making an iPhone count down app and need to know how many times a weekend (like Saturday and Sundays ) will happen until the date the user sets.
An example is how could I find how many weekends will occur from now (Monday, August 6th) until next week? I know the answer is 1 weekend, but I need to be able to figure this out using code.
The closest I have gotten is using the NSDateComponents and NSCalender and doing something close to the following.
NSDateComponents *weekendsLeftComponents = [calendar components:NSWeekCalendarUnit
fromDate:targetDate
toDate:[NSDate date]
options:0];
But from there I have hit the dreaded coders wall of no passing.
My problem is not exactly knowing how to go about doing this. It sounds like an Array could work, but I am afraid the Array could get rather large as some of these "things" could go as long as a couple years. (I am making an app that finds out how many days, weekends, etc until a person graduates.) Also my audience is mainly kids to teens with iPods, older ones too. I don't know how full their memory (ram) can get before running out.
Thank you so much in advance,
Alex Kafer
It looks like your fromDate and toDate are backwards, assuming targetDate is in the future.
Here's how I'd solve this problem.
Create a category on NSCalendar to count the number of times a specific weekday occurs between two dates:
#interface NSCalendar (AlexCategory)
// The number of times weekday number `weekday` (1 = Sunday, 7 = Saturday)
// occurs between `fromDate` and `toDate`. If `fromDate` falls on the desired
// weekday, it is counted. If `toDate` falls on the desired weekday, it is NOT counted.
- (NSInteger)countOfWeekday:(NSInteger)weekday fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;
#end
To implement this new method, we'll start by getting the year, month, day, and weekday of the fromDate:
#implementation NSCalendar (AlexCategory)
- (NSInteger)countOfWeekday:(NSInteger)weekday fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
NSDateComponents *components = [self components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit fromDate:fromDate];
Next, we count how many days from fromDate to the next desired weekday:
NSInteger daysUntilDesiredWeekday = weekday - components.weekday;
// If fromDate is a Wednesday and weekday is Monday (for example),
// daysUntilDesiredWeekday is negative. Fix that.
NSRange weekdayRange = [self minimumRangeOfUnit:NSWeekdayCalendarUnit];
if (daysUntilDesiredWeekday < weekdayRange.location) {
daysUntilDesiredWeekday += weekdayRange.length;
}
Note that daysUntilDesiredWeekday will be zero if fromDate falls on the desired weekday.
Now we can create an NSDateComponents representing the first desired weekday on or after fromDate:
NSDateComponents *firstDesiredWeekday = [[NSDateComponents alloc] init];
firstDesiredWeekday.year = components.year;
firstDesiredWeekday.month = components.month;
firstDesiredWeekday.day = components.day + daysUntilDesiredWeekday;
We update fromDate to be that first desired weekday, and return 0 if it's on or after toDate:
fromDate = [self dateFromComponents:firstDesiredWeekday];
if ([fromDate compare:toDate] != NSOrderedAscending) {
return 0;
}
Next we count all the days (not just desired weekdays) from the updated fromDate to toDate:
NSInteger allDaysCount = [self components:NSDayCalendarUnit
fromDate:fromDate toDate:toDate options:0].day;
We can divide that by the number of days in a week to count just the number of desired weekdays. Since we started counting from a desired weekday, any partial week remainder would also contain the desired weekday, so we need to round up:
// Adding weekdayRange.length - 1 makes the integer division round up.
return (allDaysCount + weekdayRange.length - 1) / weekdayRange.length;
}
#end
We can test the method like this:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.year = 2012;
components.month = 1;
components.day = 1;
NSDate *fromDate = [calendar dateFromComponents:components];
for (NSUInteger i = 1; i <= 365; ++i) {
components.day = i;
NSDate *toDate = [calendar dateFromComponents:components];
NSLog(#"%# to %#: %ld Mondays", fromDate, toDate, [calendar countOfWeekday:2 fromDate:fromDate toDate:toDate]);
}
The output starts like this:
2012-08-06 16:27:05.751 tuesdays[81152:403] 0 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-01 06:00:00 +0000
2012-08-06 16:27:05.754 tuesdays[81152:403] 0 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-02 06:00:00 +0000
2012-08-06 16:27:05.756 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-03 06:00:00 +0000
2012-08-06 16:27:05.758 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-04 06:00:00 +0000
2012-08-06 16:27:05.759 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-05 06:00:00 +0000
2012-08-06 16:27:05.760 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-06 06:00:00 +0000
2012-08-06 16:27:05.762 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-07 06:00:00 +0000
2012-08-06 16:27:05.763 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-08 06:00:00 +0000
2012-08-06 16:27:05.763 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-09 06:00:00 +0000
2012-08-06 16:27:05.764 tuesdays[81152:403] 2 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-10 06:00:00 +0000
2012-08-06 16:27:05.765 tuesdays[81152:403] 2 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-11 06:00:00 +0000
This looks correct according to the output of cal 1 2012:
January 2012
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
(Remember, the toDate is not counted, so there are 0 Mondays from 2012-1-1 to 2012-1-2, even though 2012-1-2 is a Monday.)
You need to specify your problem.
Define what is an weekend? For me weekend is saturday and sunday. So every time a sunday appears it was an weekend(Keep in mind that sunday is not the whole weekend - I just want to simplify the requirements).
So how could you do this in code?
There are many ways. One quick approach would be create an Array containing every day in it [Monday,Tuesday,...,Sunday]
Now you want to know how many weekends appear between this Tuesday and Tuesday in 3 weeks. You will get the number three by subtracting targetDateComponent.week-currentDateComponent.week. You just go through this array until Tuesday appeared 3 times and counted all the sundays.
Really simple. I'm not sure if this meets yours problem exactly. But there tons of ways.
Just try to think discreet