SwiftyChrono icon indicating copy to clipboard operation
SwiftyChrono copied to clipboard

SwiftyChrono - It gives from different timezone. Is there any option to change the timezone ?

Open lets-swapcode opened this issue 4 years ago • 4 comments

how to change the time zone?

right now, I'm passing refdate as my local time so in some scenarios it is showing right and some it shows according to different timezone.

import SwiftyChrono

extension Date {

    // Convert local time to UTC (or GMT)
    func toGlobalTime() -> Date {
        let timezone = TimeZone.current
        let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
        return Date(timeInterval: seconds, since: self)
    }

    // Convert UTC (or GMT) to local time
    func toLocalTime() -> Date {
        let timezone = TimeZone.current
        let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
        return Date(timeInterval: seconds, since: self)
    }

}

let chrono = Chrono()
chrono.parse(text: "Bring a book tomorrow")

let refDate = Date().toLocalTime()
let val = chrono.parse(text: "Bring a book tomorrow", refDate: refDate)
let date = chrono.parseDate(text: "tomorrow ",refDate: refDate)

let date1 = chrono.parseDate(text: "at 5 PM",refDate: refDate)
let date2 = chrono.parseDate(text: "in 2 minutes",refDate: refDate)

print(date)
print(date1)
print(date2)

Output-

Optional(2019-12-28 06:30:00 +0000) //different time zone Optional(2019-12-27 11:30:00 +0000) //different time zone Optional(2019-12-27 15:49:03 +0000) //right

Screenshot 2019-12-27 at 3 53 37 PM

lets-swapcode avatar Dec 27 '19 10:12 lets-swapcode

can you print the refDate?

the default Swift Date constructor is located to local time and Date itself doesn't contain the concept of timezone.

jerrywell avatar Feb 14 '20 03:02 jerrywell

now the time is 26 march 11pm.

Scenarios-


  1. if i use ref date as Date() and the text is "at 9 pm"

// the time has gone, so expected output is tomorrow's date i.e. 27 march 9 pm output - 26 march 9 pm

which is wrong

  1. if i use ref date as Date() and the text is "at 11.15 pm"

// the time has yet to come, so expected output is 26 march 11.15 pm output - 26 march 11.15 pm

which is correct

3.if i use ref date as Date().toLocalDate() and the text is "at 9 pm"

func toLocalTime() -> Date { let timezone = TimeZone.current let seconds = TimeInterval(timezone.secondsFromGMT(for: self)) return Date(timeInterval: seconds, since: self) }

// the time has gone, so expected output is 27 march 9 pm output - 27 march 9 pm

which is correct

4.if i use ref date as Date().toLocalDate() and the text is "at 11.15 pm"

func toLocalTime() -> Date { let timezone = TimeZone.current let seconds = TimeInterval(timezone.secondsFromGMT(for: self)) return Date(timeInterval: seconds, since: self) }

// the time has gone, so expected output is 26 march 11.15 pm output - 27 march 11.15 pm

which is wrong

so, even using either of reference date (local date or direct date) one scenario passes and one fails

(scenario are 1. time yet to come, 2. time which has pass)

harshsurati avatar Mar 26 '20 17:03 harshsurati

added opt: [.forwardDate: 1] as well. still the same.

harshsurati avatar Mar 26 '20 18:03 harshsurati

In simple words,

now time is 27 march 2020, 12.42 am

Input -> at 12:15 am

excepted output -> 28 march 2020, 12:15 am (as the time has pass so next day and I'm using opt: [.forwardDate: 1] )

real output -> 27 march 2020, 12:15 am (which is now a past date)

harshsurati avatar Mar 26 '20 19:03 harshsurati