Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lets-swapcode opened this issue Dec 27, 2019 · 4 comments

Comments

@lets-swapcode
Copy link

lets-swapcode commented Dec 27, 2019

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 lets-swapcode changed the title SwiftyChrono - Behaving weird and no time zone. SwiftyChrono - It gives from different timezone. Is there any option to change the timezone ? Jan 10, 2020
@jerrywell
Copy link
Member

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.

@harshsurati
Copy link

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
Copy link

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

@harshsurati
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants