I'm a little bit confused about the correct usage of Calendar of Apple Foundations framework.
let calendar = Calendar(identifier: .iso8601)
let dayComponent = DateComponents(year: 2019, weekday: 1, weekOfYear: 6)
let date = calendar.date(from: dayComponent)
I need to get the first day of a given week of year. When using the code above the following dates are given depending on weekday:
//weekday:
//0 -> 08 FEB
//1 -> 09 FEB
//2 -> 03 FEB
//3 -> 04 FEB
//4 -> 05 FEB
Why does weekday starts with 0 at the current week (6) while switching to week 5 when increased?
Thanks for any help.
A couple of observations:
When iterate through weekdays, you want to go from 1 through 7, because, “Day, week, weekday, month, and year numbers are generally 1-based...” Date and Time Programming Guide: Date Components and Calendar Units. You can use range(of:in:for:), maximumRange(of:), etc., to find the range of possible values.
The weekday values from 1 through 7 do not mean “first day of the week”, “second day of the week”, etc. They refer to specific days of the week, e.g. for .iso8601, “Sun” is 1, “Mon” is 2, etc.
Make sure when you use weekOfYear, you use yearForWeekOfYear:
let calendar = Calendar(identifier: .iso8601)
let firstOfWeek = DateComponents(calendar: calendar, weekOfYear: 6, yearForWeekOfYear: 2019).date!
Your code is iterating through the weekdays. Consider this code that enumerates all of the days of the week for the sixth week of 2019 (i.e. the week starting Monday, February 4th and ending Sunday, February 10th):
let weekdays = calendar.range(of: .weekday, in: .weekOfYear, for: firstOfWeek)!
let daysOfTheWeek = Dictionary(uniqueKeysWithValues: zip(weekdays, calendar.weekdaySymbols))
for weekday in weekdays {
let date = DateComponents(calendar: calendar, weekday: weekday, weekOfYear: 6, yearForWeekOfYear: 2019).date!
print("The", daysOfTheWeek[weekday]!, "in the 6th week of 2019 is", formatter.string(from: date))
}
That results in:
The Sun in the 6th week of 2019 is Sunday, February 10, 2019
The Mon in the 6th week of 2019 is Monday, February 4, 2019
The Tue in the 6th week of 2019 is Tuesday, February 5, 2019
The Wed in the 6th week of 2019 is Wednesday, February 6, 2019
The Thu in the 6th week of 2019 is Thursday, February 7, 2019
The Fri in the 6th week of 2019 is Friday, February 8, 2019
The Sat in the 6th week of 2019 is Saturday, February 9, 2019
This is effectively what your code does and why you’re not seeing what you expected.
If you want iterate through the seven days of the week in order, just get the start of the week and then offset it from there:
let calendar = Calendar(identifier: .iso8601)
let startOfWeek = DateComponents(calendar: calendar, weekOfYear: 6, yearForWeekOfYear: 2019).date!
for offset in 0 ..< 7 {
let date = calendar.date(byAdding: .day, value: offset, to: startOfWeek)!
print(offset, "->", formatter.string(from: date))
}
That results in:
0 -> Monday, February 4, 2019
1 -> Tuesday, February 5, 2019
2 -> Wednesday, February 6, 2019
3 -> Thursday, February 7, 2019
4 -> Friday, February 8, 2019
5 -> Saturday, February 9, 2019
6 -> Sunday, February 10, 2019
You asked:
I need to get the first day of a given week of year.
Probably needless to say at this point, but just omit the weekday:
let startOfWeek = DateComponents(calendar: calendar, weekOfYear: 6, yearForWeekOfYear: 2019).date!
Also see Date and Time Programming Guide: Week-Based Calendars.
Related
I cant get the next 15th day but not the working day.
DateTime.now.next_day(+15).strftime('%d %^B %Y')
how can i get the next 15th weekday?
You're just adding 15 days to the current date. What you want is to adjust the date:
date = DateTime.now
if (date.mday > 15)
date = date.next_month
end
date = date.next_day(15 - date.mday)
Where that adjusts to be the 15th of the next month if it's already past the 15th of the current month.
Now this can be extended to be an Enumerator:
def each_mday(mday, from: nil)
from ||= DateTime.now
Enumerator.new do |y|
loop do
if (from.mday > mday)
from = from.next_month
end
from = from.next_day(mday - from.mday)
y << from
from += 1
end
end
end
Which makes it possible to find the first day matching particular criteria, like being a weekday:
each_mday(15, from: Date.parse('2019-06-14')).find { |d| (1..5).include?(d.wday) }
Where that returns July 15th, as June 15th is a weekend.
The from argument is optional but useful for testing cases like this to ensure it's working correctly.
15.times.reduce(Date.civil 2019, 03, 24) do |acc, _|
begin
acc += 1
end while [0, 6].include? acc.wday
acc
end
#⇒ #<Date: 2019-04-12 ((2458586j,0s,0n),+0s,2299161j)>
So you want to add 15 business days from the current date. You can go with iGian or Aleksei vanilla ruby answers or use business_time gem:
15.business_days.from_now
If I understood correctly, you want to get next Monday if you hit Saturday or Sunday. Since wday gives you 0 for Sun and 6 for Sat, you can use it to as a conditional to add days towards Monday.
def date_add_next_week_day(days)
date = (Date.today + days)
date += 1 if date.wday == 6
date += 1 if date.wday == 0
date
end
date_add_next_week_day(15).strftime('%d %^B %Y')
If I get the point you need to find the 15th day after a specified date, skipping weekends.
One possible option is to define the skipping_weekend hash like this, considering Date.html#wday:
skip_weekend = { 6 => 2, 0 => 1}
skip_weekend.default = 0
Then:
next15 = DateTime.now.next_day(15)
next15_working = next15.next_day(skip_weekend[next15.wday]).strftime('%d %B %Y')
Now if next15 falls on a working day, next15_working is the same day (hash defaults to 0), otherwise it skips 2 days if Saturday (6th week day, hash maps to 2) or 1 day if Sunday (0th week day, hash maps to 1)
I assume that, given a starting date, ds (a Date object), and a positive integer n, the problem is determine a later date, dt, such that between ds+1 and dt, inclusive, there n weekdays.
require 'date'
def given_date_plus_week_days(dt, week_days)
wday = dt.wday
weeks, days = (week_days + {0=>4, 6=>4}.fetch(wday, wday-1)).divmod(5)
dt - (wday.zero? ? 6 : (wday - 1)) + 7*weeks + days
end
The variable wday is assigned to the day of week for the start date, dt. The start date is moved back to the previous Monday, unless it falls on a Monday, in which case it is not changed. That is reflected in the expression
wday.zero? ? 6 : (wday - 1)
which is subtracted from dt. The number of week days is correspondingly adjusted to
week_days + { 0=>4, 6=>4 }.fetch(wday, wday-1)
The remaining calculations are straightforward.
def display(start_str, week_days)
start = Date.parse(start_str)
7.times.map { |i| start + i }.each do |ds|
de = given_date_plus_week_days(ds, week_days)
puts "#{ds.strftime("%a, %b %d, %Y")} + #{week_days} -> #{de.strftime("%a, %b %d, %Y")}"
end
end
display("April 8", 15)
Mon, Apr 08, 2019 + 15 -> Mon, Apr 29, 2019
Tue, Apr 09, 2019 + 15 -> Tue, Apr 30, 2019
Wed, Apr 10, 2019 + 15 -> Wed, May 01, 2019
Thu, Apr 11, 2019 + 15 -> Thu, May 02, 2019
Fri, Apr 12, 2019 + 15 -> Fri, May 03, 2019
Sat, Apr 13, 2019 + 15 -> Fri, May 03, 2019
Sun, Apr 14, 2019 + 15 -> Fri, May 03, 2019
display("April 8", 17)
Mon, Apr 08, 2019 + 17 -> Wed, May 01, 2019
Tue, Apr 09, 2019 + 17 -> Thu, May 02, 2019
Wed, Apr 10, 2019 + 17 -> Fri, May 03, 2019
Thu, Apr 11, 2019 + 17 -> Mon, May 06, 2019
Fri, Apr 12, 2019 + 17 -> Tue, May 07, 2019
Sat, Apr 13, 2019 + 17 -> Tue, May 07, 2019
Sun, Apr 14, 2019 + 17 -> Tue, May 07, 2019
I have successfully created a DST method called dst_datechange that takes a date, is parsed out using Time.parse. It looks like this:
require 'time'
def dst_datechange(date)
date = Time.parse(date.to_s) # if date.class.eql?(String)
case
when (date > Time.parse('March 11, 2018 2:00am')) && (date <
Time.parse('November 4, 2018 2:00am'))
date = Time.parse('November 4, 2018 2:00am')
puts "the date rounded to november 4, 2018"
when (date > Time.parse('November 4 2018, 2:00am')) && (date <
Time.parse('March 10, 2:00am'))
date = Time.parse('March 10, 2019 2:00am')
puts "the date rounded to march 10 2019"
when (date > Time.parse('March 10, 2019 2:00am')) && (date <
Time.parse('November 3, 2019 2:00am'))
date = Time.parse('November 3, 2019 2:00am')
when (date > Time.parse('November 3, 2019 2:00am')) && (date <
Time.parse('March 8, 2020 2:00am'))
date = Time.parse('March 8, 2020 2:00am')
when (date > Time.parse('March 8, 2020 2:00am')) && (date <
Time.parse('November 1, 2020 2:00am'))
date = Time.parse ('November 1, 2020 2:00am')
else
raise "The date #{date} does not match any dst date parameter"
end
date
puts "the new DST date is #{date}"
end
and my "puts" displays this...
the date rounded to: november 4, 2018
the new DST date is now: 2018-11-04 02:00:00 -0600
Now that I am receiving the correct date, I have a step that takes that dst_datechange and performs a subtraction, however, I am getting an error that says:
TypeError: no implicit conversion of Integer into Array
I am not sure what I am doing wrong but I know its most likely a formatting issue where I am trying to subtract a date time object with just a time object. here is my step below where the stacktrace is pointing the failure at:
date = (dst_datechange(Time.now) - (60*60*3))
puts "the date is now adjusted 3 hours back from 2:00am: #{date} "
end
I am unsure how to format that (60*60*3) to subtract 3 hours from that new November 2018-11-04 02:00:00 -0600 date and basically roll it back to 2018-11-03 23:00:00 -0600
Your method def dst_datechange(date) doesn't return the date you want it to, but instead puts your string.
When you call that in your second part, dst_datechange(Time.now), that doesn't return the date, but the return value for the last puts.
Try calling 'date' again after your final puts in your dst_datechange method:
when (date > Time.parse('March 8, 2020 2:00am')) && (date < Time.parse('November 1, 2020 2:00am'))
date = Time.parse ('November 1, 2020 2:00am')
else
raise "The date #{date} does not match any dst date parameter"
end
puts "the new DST date is #{date}"
date
end
You need to specify that you want to subtract hours. Try:
(Time.now - 3.hours).to_datetime
Or, if using ActiveSupport you can specify that you want three hours ago:
3.hours.ago
Since 2007, in most jurisdictions in the United States and Canada, Daylight Saving Time ("DST") has begun at 2:00am on the second Sunday of March, and ended at 2:00am on the first Sunday in November. In Mexico, DST begins at 2:00am on the first Sunday in April and ends at 2:00am on the last Sunday in October. The experience varies in other parts of the world.1
Given a Time object (possibly computed from a string representation of a time) your code returns a second Time object equal to the time at which the next time change occurs, from DST to STD or vice-versa (i.e., in those parts of the United States and Canada that have adopted DST). There is no need to hard-wire those dates. The calculation can be done as follows.
require 'time'
def dst_datechange(time)
year = time.year
if time.dst?
start_std(year)
else
year += 1 if time >= Time.new(year, 11)
start_dst(year)
end
end
def start_std(year)
date = Date.new(year, 11)
date += (7-date.wday) % 7
Time.new(date.year, date.month, date.day, 2)
end
def start_dst(year)
date = Date.new(year, 3, 8)
date += (7-date.wday) % 7
Time.new(date.year, date.month, date.day, 2)
end
['June 17, 2018 1:00pm', 'November 4, 2018 12:59am', 'November 4, 2018 1:00am',
'March 10, 2019 1:59am', 'March 10, 2019 2:00am'].
each do |s|
t = DateTime.strptime(s, '%B %e, %Y %l:%M%P').to_time
t = Time.new(t.year, t.month, t.day, t.hour, t.min, t.sec)
puts "#{s}: #{ dst_datechange(t) }"
end
June 17, 2018 1:00pm: 2018-11-04 02:00:00 -0800
November 4, 2018 12:59am: 2018-11-04 02:00:00 -0800
November 4, 2018 1:00am: 2019-03-10 03:00:00 -0700
March 10, 2019 1:59am: 2019-03-10 03:00:00 -0700
March 10, 2019 2:00am: 2019-11-03 02:00:00 -0800
Notice that the first, second and last examples return a time whose hour is 2. This is in fact one hour after the time changed. The other two examples return an hour of 3, which is an instant after the time changed. Note:
Time.new(2019, 3, 10, 2)
#=> 2019-03-10 03:00:00 -0700
Time.parse('March 10, 2019 2:00am')
#=> 2019-03-10 03:00:00 -0700
In future, DST date ranges could change (as they did in 2007) or DST could be eliminated altogether. In production code, therefore, it would be prudent to address these possibilities. Assuming the method Time#dst? would continue to exist, Time.new(year,6).dst? should tell us if we still have DST, and if so, one could search day-by-day to see when the time changes occur.
1. Source
Let's suppose I have the following data on ElasticSearch:
#timestamp; userId; currentPoints
August 7th 2017, 00:30:37.319; myUserName; 4
August 7th 2017, 00:43:22.121; myUserName; 10
August 7th 2017, 00:54:29.177; myUserName; 7
August 7th 2017, 01:10:29.352; myUserName; 4
August 7th 2017, 00:32:37.319; myOtherUserName; 12
August 7th 2017, 00:44:22.121; myOtherUserName; 17
August 7th 2017, 00:56:29.177; myOtherUserName; 8
August 7th 2017, 01:18:29.352; myOtherUserName; 11
I'm looking to draw a date histogram that will show me the sum of all max:currentPoints per username per hour, which whould generate the following data to plot:
August 7th 2017, 00; SumOfMaxCurrentPoints -> 27 (max from hour 00h from both users 10 + 17)
August 7th 2017, 00; SumOfMaxCurrentPoints -> 15 (max from hour 01h from both users 4 + 11)
This would usually be done with a subquery, extracting the max(currentPoints) for each hour, user and then sum the results and aggregate per hour.
Is this possible with Kibana Timelion for instance? I can't find a way to achieve this using the documentation.
Thanks
Alex
While working on another project, I've stumpled upon the answer to do this in Kibana/Elasticsearch without using Timelion.
The feature is called Sibling Pipeline Aggregation, and in this case you use the Sum Bucket. You can use it with any recent Kibana/Elastic visualization (I'm using version 5.5).
For a dataset such as:
#timestamp; userId; currentPoints
August 7th 2017, 00:30:37.319; myUserName; 4
August 7th 2017, 00:43:22.121; myUserName; 10
August 7th 2017, 00:54:29.177; myUserName; 7
August 7th 2017, 01:10:29.352; myUserName; 4
August 7th 2017, 00:32:37.319; myOtherUserName; 12
August 7th 2017, 00:44:22.121; myOtherUserName; 17
August 7th 2017, 00:56:29.177; myOtherUserName; 8
August 7th 2017, 01:18:29.352; myOtherUserName; 11
Where you want an hourly SUM of(currentPoints) all MAXs(currentPoints) per userId, resulting in:
August 7th 2017, 00; SumOfMaxCurrentPoints -> 27 (max from hour 00h from both users 10 + 17)
August 7th 2017, 00; SumOfMaxCurrentPoints -> 15 (max from hour 01h from both users 4 + 11)
You can do:
Metric
Aggregation: Sibling Pipeline Aggregation (Sum Bucket)
Bucket Aggregation Type: Terms
Bucket Field: userId
Bucket Size: Comfortable value above the # of users if you want total precision
Metric Aggregation: Max
Metric Field: currentPoints
Bucket
Buckets type: Split Rows
Bucket Aggregation: Date Histogram
Histogram Field: #timestamp
Histogram Interval: Hourly
My customer has an event each second Monday of each month.
I need to mark them with red in calendar.
How do i "cleanly" find out the date of that Mondays?
Here's my version.
If the eighth of the month is a Monday, then it is the second Monday. If it is not a Monday, then how many days until the next Monday?
oct_2012 = Date.new 2012, 10, 8
oct_2012.wday # => 1, We're done!
nov_2012 = Date.new 2012, 11, 8
nov_2012.wday # => 4
nov_2012 + (8 - nov_2012.wday) # => 2012-11-12
Does that help?
Edit
Easier version: Just add and be done. This algorithm works even if the month starts on a Monday.
oct_2012 = Date.new 2012, 10, 1
oct_2012 + (8 - oct_2012.wday) # => 2012-10-08
nov_2012 = Date.new 2012, 11, 1
nov_2012 + (8 - nov_2012.wday) # => 2012-11-12
One rule and done!
You second Monday will always fall within the 8th and 14th of each month.
I can only find algorithm for getting ISO 8601 week (week starts on a Monday).
However, the iCal spec says
A week is defined as a seven day period, starting on the day of the
week defined to be the week start (see WKST). Week number one of the
calendar year is the first week that contains at least four (4) days
in that calendar year.
Therefore, it is more complex than ISO 8601 since the start of week can be any day of the week.
Is there an algorithm to determine what is the week number of a date, with a custom start day of week?
or... is there a function in iCal4j that does this? Determine a weekno from a date?
Thanks!
p.s. Limitation: I'm using a JVM language that cannot extend a Java class, but I can invoke Java methods or instantiate Java classes.
if (input_date < firstDateOfTheYear(WKST, year))
{
return ((isLeapYear(year-1))?53:52);
}
else
{
return ((dayOfYear(input_date) - firstDateOfTheYear(WKST, year).day)/7 + 1);
}
firstDateOfTheYear returns the first calendar date given a start of week(WKST) and the year, e.g. if WKST = Thursday, year = 2012, then it returns Jan 5th.
dayOfYear returns sequencial numerical day of the year, e.g. Feb 1st = 32
Example #1: Jan 18th, 2012, start of week is Monday
dayOfYear(Jan 18th, 2012) = 18
firstDateOfTheYear(Monday, 2012) = Jan 2nd, 2012
(18 - 2)/7 + 1 = 3
Answer Week no. 3
Example #2: Jan 18th, 2012, start of week is Thursday
dayOfYear(Jan 18th, 2012) = 18
firstDateOfTheYear(Thursday, 2012) = Jan 5th, 2012
(18 - 5)/7 + 1 = 2
Answer Week no. 2
Example #3: Jan 1st, 2012, start of week is Monday
firstDateOfTheYear(Monday, 2012) = Jan 2nd, 2012
IsLeapYear(2012-1) = false
Jan 1st, 2012 < Jan 2nd, 2012
Answer Week no. 52
Let daysInFirstWeek be the number of days on the first week of the year that are in January. Week starts on a WKST day. (e.g. if Jan 1st is a WKST day, return 7)
Set dayOfYear to the n-th days of the input date's year (e.g. Feb 1st = 32)
If dayOfYear is less than or equal to daysInFirstWeek
3.1. if daysInFirstWeek is greater than or equal to 4, weekNo is 1, skip to step 5.
3.2. Let daysInFirstWeekOfLastYear be the number of days on the first week of the previous year that are in January. Week starts on a WKST day.
3.3. if daysInFirstWeekOfLastYear is 4 or last year is Leap year and daysInFirstWeekOfLastYear is 5, weekNo is 53, otherwise weekNo is 52, skip to step 5.
Set weekNo to ceiling((dayOfYear - daysInFirstWeek) / 7)
4.1. if daysInFirstWeek greater than or equal to 4, increment weekNo by 1
4.2. if daysInFirstWeek equal 53 and count of days on the first week (starting from WKST) of January in the year of inputDate's year + 1 is greater than or equal to 4, set weekNo to 1
return weekNo