Creepy date-function!

Hi folks!
I want to calculate time distances and wrote a little program:

let formattierer = DateFormatter()
formattierer.dateFormat = "dd/MM/yyyy"
let cleangeburtstag = "06/12/2005"
let cg = formattierer.date(from: cleangeburtstag)
formattierer.timeZone = TimeZone.current

and now the creepy part:
for w_d in 1...10000 {
dateComponentc.day = w_d*8 // to add 1 week and 1 day to the test date cg
let cgr = Calendar.current.date(byAdding: dateComponentc, to: cg!)

let tage_g = Calendar.current.dateComponents([.day], from:cgr!, to: now)
// to test if there are days left after adding 1 week+1 day several times
let test_g = Int(tage_g.day!) // put the test result to integer

if test_g == 0 {
print ("Result: ",w_d,"weeks and",w_d,"days!")
break
}
if test_g < 0 {
break
}
} // gives me THE SAME output for 06/12/2005 and 07/12/2005

605 weeks and 605 days -> I didn't change the timezone or anything else! I just change the date and get the same output!!! for other test-dates everything seems to work

has anybody an idea?

Please refer this question to a different forum. This forum is dedicated to the development of the Swift language.

May I politely suggest that you

  • use a more descriptive title to describe your problem,
  • use the "preformatted text" tool to format the code to make it better readable,
  • post self-contained code that actually compiles.

Calendar, DateComponents etc are actually part of the Foundation framework and not part of the Swift language, so https://forums.developer.apple.com/community/app-frameworks/cocoa or Stack Overflow may be a better place for your question.

Having said that, for the starting date "06/12/2005" your loop ends at 2019-03-08 00:00:00 (local time), and for the starting date "07/12/2005" it ends at 2019-03-09 00:00:00 (local time). Both end dates differ by less than one day from now. If you calculate more components of the difference then you'll notice that the hours difference is positive in the first case and negative in the second case.

There is nothing creepy here.

1 Like

Eh, Foundation is a swift "core library" and part of the overall open-source project.

2 Likes

As far as I know, only the “Foundation for non-Apple platforms” is open source, and the same problem that David experienced would have happened if those classes are called from Objective-C.

Anyway: I am not sure if this forum is the right place for such a “how to do correct calendrical calculations” question, but that may be my personal opinion. I intentionally used a vague formulation (“... may be a better place ...”) because that is – of course – not for me to decide. I did not want to discourage David from posting here.

1 Like

Foundation is not off-topic for these forums. It is a Swift core library on all supported platforms.

That said, I do think that @davidberger could benefit from taking a more proactive approach with exploring the resources that are out there for Swift basics and for calendrical operations in any language. It would be of greater learning value for all if you explain what you think should happen in your code, what you think might be going on that caused the actual behavior to differ from your expectations, and what you have tried to troubleshoot.

The point of these forums isn't to correct your code (basically, to write your code for you) but to correct your understanding so that you can write your own code. However, it isn't possible to do that if you don't share anything about what your misunderstanding or confusion actually is.

6 Likes

Thanks for your replies, folks!
My problem is that I don't understand why the end date´s time isn't 12:00 am when I add 605*8 days to the start date 2005-12-06 12:00 am! (local time) I add 4048 days and get the output 2019-03-07 23:00. Clear is that when I call the function at lets say 22:59 pm
on 2019-03-08 that the return value of [.day] is 0 because the difference between the end date and now (let now=Date() ) is less than 24 hours, but I thought that the end date would be at 12:00 am like the start date

Depending on your starting point, the addition of 4048 days to a starting date may cross daylight savings time transitions an even or odd number of times (depending on the time zone, too). If you cross over DST an odd number of times, usually (ignoring the usual caveats + historical weirdnesses), you will be an hour ahead or an hour behind.

(I haven't confirmed that this is specifically what's happening here, but this is a good avenue to consider when adding large number of days/months/years/etc. to a starting date; DST is very often overlooked in general.)

1 Like

I found out that the date is always created in UTC but printed out in local time. that confused me. I had the problem that the function showed "605 Weeks 605 Days" two times because my condition was false: days == 0 is true whenever .hour < 24 - so depending on when the function was called (1:00 am the next day) it outputted a wrong amount of days considering the date...
I fixed it with counting the hours with .hour and set the condition "if .hour < 12 print ...weeks and ...days" and "if .hour < -12 break" because the start date is always 12:00 am
thanks for your help folks!

Terms of Service

Privacy Policy

Cookie Policy