I want to using dart Dart SDK version: 2.17.3 (stable) (Wed Jun 1 11:06:41 2022 +0200) on "macos_arm64" to get start and end minisecond of current month. I tried like this but I did not know how to get the last day 23:59:59:999 unix timestamp:
endOfMonthMilliseconds(DateTime now) {
var beginningNextMonth = (now.month < 12) ? new DateTime(now.year, now.month + 1, 1) : new DateTime(now.year + 1, 1, 1);
var lastDay = beginningNextMonth.subtract(new Duration(days: 1)).millisecondsSinceEpoch;
}
I recommend using UTC dates, because then you won't get into daylight saving issues, but if you do the need local time end-of-month, this should work:
DateTime endOfMonth(DateTime now) {
return DateTime(now.year, now.month + 1, 1, 0, 0, 0, -1);
}
The DateTime constructor allows overflow and underflow between fields, so DateTime(now.year, now.month + 1, 1) would give you the first of the following month (no need to do the December-to-January overflow yourself), and appending the , 0, 0, 0, -1 should give you one millisecond earlier than that.
Related
I have some troubles with the following task:
Given are the following parameters:
Year: 2016
Month: 2
Week of Month: 2
Day of week: Monday
The result should be 2016-02-08
Is there a method to get this?
Thanks in advance
A way to do this would be
weekday_index = {monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 7}
day_in_a_week = 7
year = year_param # 2016
month = month_param # 8
week_number = 2
day_of_month = (week_number * days_in_a_week) - (days_in_a_week - weekday_index[:monday])
Date.new(year, month, day_of_month).to_s
You can use this code. Values used were supplied in the question month = 2, week_number = 2 and year = 2016
month = 2
year = 2016
week_number = 2
FIRST_DAY = 1 #first day of every month
day = { monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 7}
first_wk_of_month = Date.new(year, month, FIRST_DAY).cweek #=> 05
date_week_num = first_wk_of_month + (week_number - 1) #=> 06
date = Date.commercial(year, date_week_num, day[:monday]).to_s # 2016-02-08
There are 52 weeks in a year and a month can have as low as 1 day in a week if it starts on Sunday. Taking that into consideration,
first_wk_of_month finds the week number in the year (between 1 and 52) on which the month in question starts.
date_week_num finds the actual week number in the year the date falls on. In this scenario which is 2nd week of February it returns 6 representing the 6th week of the year
Passing the date_week_num and the day to the Date.commercial allows us to find the date
Tested it with a couple of days and works fine. You could probably change the variable names. Hope this solves your challenge
I need to count amount of time in specified range. For example, I have range (let's call it peak hours) 12:00-14:00. And i have another range(visit time), that might change, for ex 9:00-15:00. How do I get intersected hours for these 2 ranges?
As result I would like to get something like: {peak_hours: 2, regular_hours: 4}
Here peak_hours value is 2 as that many peak hours overlap with regular hours. And, regular_hours value is 4 as that many regular hours do not overlap with peak hours.
I'm kinda stuck with solution. I tried to use time ranges, but that didn't work for me. This is the code
peak_hours_range = Time.parse(peak_hour_start)..Time.parse(peak_hour_end)
session_range = visit_start..visit_end
inters = session_range.to_a & peak_hours_range.to_a
But this throws me type error
Here is one way to do it, we find the total hours in both ranges included, and then remove the peak hours from it to get effective regular hours.
require "time"
peak_hour_start = "12:00"
peak_hour_end = "14:00"
regular_hour_start = "9:00"
regular_hour_end = "15:00"
ph = (Time.parse(peak_hour_start).hour...Time.parse(peak_hour_end).hour).to_a
#=> [12, 13]
rh = (Time.parse(regular_hour_start).hour...Time.parse(regular_hour_end).hour).to_a
#=> [9, 10, 11, 12, 13, 14]
total = (ph + rh).uniq
#=> [12, 13, 9, 10, 11, 14]
r = {peak_hours: (ph - rh).size, regular_hours: (total - ph).size}
#=> {:peak_hours=>2, :regular_hours=>4}
You can always try to find the intersection yourself.
inters = nil
intersection_min = [peak_hour_start, visit_start].max
intersection_max = [peak_hour_end, visit_end].min
if intersection_min < intersection_max
inters = [intersection_min, intersection_max]
end
inters
Of course this can be cleaned up by extracting it out into it's own method.
What is wrong with this code in swift accessing the time and date functions in C? The date it gives me is off by 3 days even though the difftime function is correct on the time difference.
import Darwin
var time1 = tm(tm_sec: 00, tm_min: 00, tm_hour: 00, tm_mday: 13, tm_mon: 06, tm_year: 1977, tm_wday: 0, tm_yday: 0, tm_isdst: 0, tm_gmtoff: 0, tm_zone: nil)
var time1secs = timegm(&time1)
var time2secs = timegm(&time1) + 1_000_000_000
var time2 = gmtime(&time2secs).memory
difftime(time2secs, time1secs) // 1,000,000,000
print("\(time2.tm_year)-\(time2.tm_mon)-\(time2.tm_mday)") //2009-2-22
// The correct answer is 2009-02-19
In the struct tm, the tm_year field is the number of years
since 1900, and tm_mon is the month in the range 0 .. 11:
// struct tm for 1977/06/13:
var time1 = tm()
time1.tm_year = 1977 - 1900
time1.tm_mon = 06 - 1
time1.tm_mday = 13
// Add 10^9 seconds:
var time2secs = timegm(&time1) + 1_000_000_000
var time2 = gmtime(&time2secs).memory
// Extract year/month/day:
let year = time2.tm_year + 1900
let month = time2.tm_mon + 1
let day = time2.tm_mday
print("\(year)-\(month)-\(day)") // 2009-2-19
Consider the following code:
void main() {
var duration = new Duration(days : 1);
print ("duration " + duration.toString());
var d1 = new DateTime(2014, 10, 26);
print ("d1 " + d1.toString());
d1 = d1.add(duration);
print ("d1 + duration " + d1.toString());
var d2 = new DateTime(2014, 10, 20);
print ("d2 " + d2.toString());
d2 = d2.add(duration);
print ("d2 + duration " + d2.toString());
}
and the output:
duration 24:00:00.000000
d1 2014-10-26 00:00:00.000
d1 + duration 2014-10-26 23:00:00.000
d2 2014-10-20 00:00:00.000
d2 + duration 2014-10-21 00:00:00.000
Why does October 20 and 26 behave differently. I have checked the same code for every day of the year and every year has one day in which the date + 1 day equals the same date.
Every year the date seems to be in October between 25/10 and 30/10.
Is this a bug or have I missed something?
Regards
Peyman
As per Günter Zöchbauer answer - it is due daylight saving time.
To properly add day you may use the following:
var d1 = new DateTime(2014, 10, 26);
var d1 = new DateTime(d1.year, d1.month, d1.day + 1);
I guess the Oct 26. (and the other days between 25/10 and 30/10 is due to daylight saving period ending.
The difference of 1h (23:00:00.000) indicates this as the cause.
Just to expand on Александр Бабич answer - you don't have to care about the range of the days in the month when creating the DateTime in constructor. For example
DateTime(2014, 9, 57)
will correctly return 2014-10-27 and will not introduce any daylight saving shifts.
Negative numbers work as well, but are offset by 1, because 0 also works e.g.
DateTime(2014, 9, 0)
DateTime(2014, 9, -1)
will yield 2014-08-31 and 2014-08-30 respectively
how do i subtract two different UTC dates in Ruby and then get the difference in minutes?
Thanks
If you subtract two Date or DateTime objects, the result is a Rational representing the number of days between them. What you need is:
a = Date.new(2009, 10, 13) - Date.new(2009, 10, 11)
(a * 24 * 60).to_i # 2880 minutes
or
a = DateTime.new(2009, 10, 13, 12, 0, 0) - DateTime.new(2009, 10, 11, 0, 0, 0)
(a * 24 * 60).to_i # 3600 minutes
(time1 - time2) / 60
If the time objects are string, Time.parse(time) them first
https://rubygems.org/gems/time_difference - Time Difference gem for Ruby
start_time = Time.new(2013,1)
end_time = Time.new(2014,1)
TimeDifference.between(start_time, end_time).in_minutes
Let's say you have two dates task_signed_in and task_signed_out for a simple #user object. We could do like this:
(#user.task_signed_out.to_datetime - #user.task_signed_in.to_datetime).to_i
This will give you result in days. Multiply by 24 you will get result in hours and again multiply by 60 you will result in minutes and so on.
This is the most up to date solution tested in ruby 2.3.x and above.