DateTime and duration add - dart

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

Related

how to get the month start and end minisecond in dart

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.

how to calculate (date - duration) in google sheets

How can I reduce a specific duration from a date?
for example -
the base date and time to reduce from
15/05/2018 02:00:00
the duration to reduce -
03:00:00 hours
the expected output -
14/05/2018 23:00:00
=A1-3/24-5/(24*60)-55/(24*60*60)
where
A1 = 15/05/2018 02:00:00
3 = the number of hours to reduce
5 = minutes
55 = seconds
If you have time in format: 03:05:55, the formula is simple:
=A1 - A2
where
A1 = 15/05/2018 02:00:00
A2 = 03:05:55

Ruby on rails get hours, minutes, seconds from two dates and times

I need the number of hours, minutes, seconds between two dates and times.I'm able to get the number of days, hours, minutes, seconds but I don't want no.of days instead of it, I need hours, minutes, seconds only enough.
Here my code,
start_time is Wed, 13 Dec 2017 20:35:19 -0800 and end_time is today datetime
def time_diff(end_time, start_time)
diff = end_time - start_time
mm, ss = diff.divmod(60)
hh, mm = mm.divmod(60)
dd, hh = hh.divmod(24)
time = "%d h, %d m, %d s" % [hh, mm, ss]
return time
end
I need output like this "35 h, 29 m, 12 s"
Thanks for your help.
Just out of curiosity, a pure [almost] functional solution, without intermediate local variables:
start_time = DateTime.parse 'Wed, 13 Dec 2017 23:00:00 UTC'
end_time = DateTime.parse 'Wed, 15 Dec 2017 23:30:20 UTC'
sec, min, hrs = [60, 60, 1].
map.
with_object([[[end_time, start_time].
map(&:to_time).
map(&:to_i).
reduce(:-), nil]]) do |div, obj|
obj << obj.last.first.divmod(div)
obj[-2].rotate!
end.
map(&:first).
compact
#⇒ [20, 30, 48]
You've already got the answer - just don't divide by 24!
If the start_time and end_time are DateTime value you can use the following
difference = end_time - start_time
hours = (difference * 24).to_i
minutes = (difference * 24 * 60).to_i
seconds = (difference * 24 * 60 * 60).to_i

Date in darwin standard library giving me the wrong date?

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

Subtract dates in Ruby and get the difference in minutes

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.

Resources