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

Display time in local timezone (e.g. New York timezone) #70

Open
pranavladkat opened this issue Jun 17, 2024 · 1 comment
Open

Display time in local timezone (e.g. New York timezone) #70

pranavladkat opened this issue Jun 17, 2024 · 1 comment

Comments

@pranavladkat
Copy link

When using utc timestamp for denoting time in CandlestickData and/or HistogramData, how can I display time in specific time zone (e.g. new york time zone or pacific time zone)? By default, it displays time zone in UTC.

@Mr-Alirezaa
Copy link

Mr-Alirezaa commented Nov 25, 2024

Hi @pranavladkat, hope this isn't too late.

You should pass a TimeScaleOptions to the LightweightChart, that defines a tickMarkFormatter. Like below:

let timeScaleOptions = TimeScaleOptions(
    visible: true,
    timeVisible: true,
    ticksVisible: true,
    tickMarkFormatter: .closure { parameters in
        switch parameters.tickMarkType {
        case .year:
            parameters.time.date
                .formatted(.dateTime.year(.relatedGregorian()))
        case .month:
            parameters.time.date
                .formatted(.dateTime.month(.twoDigits).year(.twoDigits))
        case .dayOfMonth:
            parameters.time.date
                .formatted(.dateTime.day(.twoDigits).month(.twoDigits))
        case .time:
            parameters.time.date
                .formatted(
                    .verbatim(
                        "\(hour: .twoDigits(clock: .twentyFourHour, hourCycle: .zeroBased)):\(minute: .twoDigits)",
                        timeZone: .current,
                        calendar: .current
                    )
                )
        case .timeWithSeconds:
            parameters.time.date
                .formatted(
                    .verbatim(
                        "\(hour: .twoDigits(clock: .twentyFourHour, hourCycle: .zeroBased)):\(minute: .twoDigits):\(second: .twoDigits)",
                        timeZone: .current,
                        calendar: .current
                    )
                )

        }
    }
)

Note that if you want to have granular access to customize your output date, you may use Date.VerbatimFormatStyle. Read more at this link. (Sorry for the language)

Now, you make the chart like following:

LightweightCharts(
    options: ChartOptions(
        // other parameters
        timeScale: timeScaleOptions,
        // ...
    )
)

Be aware that the date in parameters.time.date is an extension I wrote as below:

fileprivate extension EventTime {
    var date: Date {
        switch self {
        case let .utc(timestamp):
            return Date(timeIntervalSince1970: timestamp)

        case let .businessDay(businessDay):
            let dateComponent = DateComponents(
                timeZone: .gmt,
                year: businessDay.year,
                month: businessDay.month,
                day: businessDay.day
            )

            return Calendar.current.date(from: dateComponent)!

        case let .businessDayString(string):
            let components = string.components(separatedBy: "-").compactMap { Int($0) }
            let dateComponent = DateComponents(
                timeZone: .gmt,
                year: components[0],
                month: components[1],
                day: components[2]
            )

            return Calendar.current.date(from: dateComponent)!
        }
    }
}

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

2 participants