0

I am trying to convert 12 digit c# date time ticks formatted time. (648000000000). With the help of following link I added an extension to my code How to get 18-digit current timestamp in Swift?.

extension Date {
    init(ticks: UInt64) {
        self.init(timeIntervalSince1970: Double(ticks)/10_000_000 - 62_135_596_800)
    }
}

let date = Date(ticks: 648000000000)

When I try to see result date it prints following;

0001-01-03 18:00:00 +0000

However, when I try to convert it hour and minute format output is irrelevant like 19:55

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.string(from: date)

My first question is how can I format it to get only 18:00. Second is why it is printing 18:00 when I print only date, but 19:55 when I format it?

atalayasa
  • 3,310
  • 25
  • 42
  • That method expects *"the time since 0001-01-01 measured in 100-nanosecond intervals"* – which would be a much larger number (about 18 digits). Where does your timestamp come from, and what date does it represent? – Martin R Dec 20 '18 at 12:45
  • That timestamp is coming from our server, I need to convert it somehow, actually I only need clock (18:00) date is not important for our case. @MartinR – atalayasa Dec 20 '18 at 12:49
  • I really doubt it's `19:55`, because your problem is that `print(date)` shows you time in `UTC/+0` time zone and date formatter is using your current local zone. And minimal timezone offset is multiplier of 1/4 hour, not 5 minutes. – user28434'mstep Dec 20 '18 at 12:49
  • If the example 648000000000 is a recent date, then it looks like milliseconds from 1970 to me. – Bemmu Dec 20 '18 at 12:53
  • 648000000000 is far too small for a C# DateTime.Ticks value. Unless you tell us exactly what it is, we can only *guess* about the proper solution. – Martin R Dec 20 '18 at 12:58
  • @MartinR Actually I do not know server-side too much that is why I could not say what exactly it is. – atalayasa Dec 20 '18 at 13:00
  • There must be *some* documentation. What year/month/day does 648000000000 represent? Why do you think it is a *"12 digit c# date time ticks"* – whatever that might be? – Martin R Dec 20 '18 at 13:01
  • @MartinR I asked them and they told me that its format 18:00:00 no year/month/day information inside it. – atalayasa Dec 20 '18 at 13:06
  • For me it prints as "18:00" after formatting – Joakim Danielson Dec 20 '18 at 13:07
  • @JoakimDanielson could you please share full code that you tried? – atalayasa Dec 20 '18 at 13:08
  • @AtalayAsa I used the code in the question. Also I got a different day 1st instead of 3rd – Joakim Danielson Dec 20 '18 at 13:11
  • 1
    Does it help if you add: dateFormatter.timeZone = TimeZone(abbreviation: "UTC") – Bemmu Dec 20 '18 at 13:14
  • Possible duplicate of [NSDate() or Date() shows the wrong time](https://stackoverflow.com/questions/39937019/nsdate-or-date-shows-the-wrong-time) – user28434'mstep Dec 20 '18 at 13:17
  • 1
    You should at least have an example of a timestamp AND what date is it representing, and what timezone is the server using. Otherwise it's pure guesswork. And once you know these things, I think you could figure it out on your own, as there are so many examples available online. – Bemmu Dec 20 '18 at 13:29

2 Answers2

2

Just don't subtract 62_135_596_800

extension Date {
    init(ticks: UInt64) {
        self.init(timeIntervalSince1970: Double(ticks)/10_000_000)
    }
}

1970-01-01 18:00:00 +0000

The other problem: When you create date and print it, the string is formatted in UTC time zone (offset GMT+0). But DateFormatter returns string representation dependent on its time zone, which is the local timezone by default.

You can fix your code just by setting dateFormatter's timeZone to UTC

dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

18:00

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
  • The last sentence is a bit strange. `Date` is always independent on time zone. Only string representation of dates depend on time zone. – Sulthan Dec 20 '18 at 13:02
  • @Sulthan So `date` created with init has different string representation than the one which `DateFormatter` returns to me? – Robert Dresler Dec 20 '18 at 13:05
  • When I tried your code it returns me 03:00 but it should be 18:00 what is that difference ? – atalayasa Dec 20 '18 at 13:08
  • I also tried this but it didn't give me any valid time only a date and 00:00 for time – Joakim Danielson Dec 20 '18 at 13:10
  • @Sulthan, yep, and that what in the question: `print(date)` shows `18:00:00 +0000` and string formatted with `DateFormatter` shows different time, and author wonders why. He asks about string representations, and why they differ. How he converts timestamps is pretty much irrelevant here. – user28434'mstep Dec 20 '18 at 13:12
  • The wording is still very confusing. Default time zone is not UTC (UTC = GTM+0, there is no UTC+0). The default timezone of a date formatter is actually the local time zone. The code is correct but the explanation is not. – Sulthan Dec 20 '18 at 13:30
  • After I have added time zone, it is working well. However, I do not understand why it is working. – atalayasa Dec 20 '18 at 13:31
2

Apparently your timestamp represents a duration as the number of 100-nanosecond ticks, not a date. If you divide the number by 10^7 then you get the number of seconds. These can be printed as a duration with a DateComponentsFormatter.

Example:

let ticks = 648000000000

let seconds = TimeInterval(ticks) / 10_000_000
let fmt = DateComponentsFormatter()
fmt.allowedUnits = [.hour, .minute]
print(fmt.string(from: seconds)!)

18:00

The duration is 64800 = 18 * 60 * 60 seconds, that are exactly 18 hours.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • I upvoted your answer too thank you very much for the explanation, but the first answer is written before you that is why I accepted first one. – atalayasa Dec 20 '18 at 13:35
  • @AtalayAsa it's totally up to you which answer you choose as accepted. ;) Choose Martin's if you want to (he explained it better than I did). – Robert Dresler Dec 20 '18 at 13:39
  • Yes I know it but I always prefer to choose right and first written answer thank both of you again :) @RobertDresler – atalayasa Dec 20 '18 at 13:41