Shop It Docs
TradingView UDF

GET /time

Plain-text server time endpoint for TradingView clock synchronization

/time returns the current server time as plain-text Unix seconds. TradingView's Charting Library calls it to correct for clock skew between the browser and the server, which matters for real-time bar rollover.

  • Method: GET
  • Path: /api/nepse/tradingview/time
  • Content-Type: text/plain; charset=utf-8
  • Query params: none

Response

1776699552

A single integer, no trailing newline significance, no JSON wrapper.

c.String not c.JSON. UDF spec requires plain text. The library does parseInt(responseText) on the other end. Sending application/json breaks clock sync silently — the chart still loads, but real-time bar rollover misfires.

Why it exists

The Charting Library internally tracks "now" to decide:

  • when the currently-forming intraday bar has completed
  • when to trigger the next /history request for the most recent range
  • where to draw the "now" vertical line

If the browser clock drifts (laptop just woke from sleep, phone clock off by 90 seconds), /time gives the library the authoritative reference.

Freshness

The handler returns time.Now().UTC().Unix() on every call. There is no cache — Nginx is explicitly configured not to proxy-cache this route.

func (h *Handler) Time(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    w.WriteHeader(http.StatusOK)
    _, _ = fmt.Fprintf(w, "%d", time.Now().UTC().Unix())
}

Errors

/time has no failure modes short of a panic (which the TV-scoped Recoverer converts to {"s":"error","errmsg":"internal_error"} with JSON content type — violating the plain-text contract only in the panic case, which is an outage signal anyway).

Verification

# Must be text/plain
curl -sv "$BASE/time" 2>&1 | grep -i content-type
# Content-Type: text/plain; charset=utf-8

# Body must be unix seconds within a few seconds of local time
curl -s "$BASE/time"
# 1776699552

Implementation

  • Handler: internal/modules/nepse/tradingview/handler.go(*Handler).Time