jrefior
jrefior

Reputation: 4421

Making sense of `time.Time` values when debugging in Delve

When debugging a program that processes time.Time values, I need to be able to print the values and make sense of them. In Delve, if I print a variable of type time.Time it prints the internal structure of the object, and does not allow me to run time.Time methods on the object.

How do I makes sense of this structure and translate it to my normal understanding of what would be printed by the Unix(), UnixNano(), or String() functions.

For example:

$ dlv test
(dlv) b calendar.go:200
(dlv) p appt
time.Time {
wall: 0,
ext: 63673770600,
loc: *time.Location {
    name: "Local",
    zone: []time.zone len: 4, cap: 4, [
        (*time.zone)(0xc0000a8100),
    ],
    tx: []time.zoneTrans len: 235, cap: 235, [
        (*time.zoneTrans)(0xc0000bb000),
        ...+171 more
    ],
    cacheStart: 1520751600,
    cacheEnd: 1541311200,
    cacheZone: *(*time.zone)(0xc0000a8100),},}

Or in the case of a list of values:

(dlv) p dates
[]time.Time len: 2, cap: 2, [
{
    wall: 0,
    ext: 63673689600,
    loc: *(*time.Location)(0xc00008e9c0),},
{
    wall: 0,
    ext: 63673776000,
    loc: *(*time.Location)(0xc00008ea80),},
]

I found a github ticket to add pretty-printing of time.Time values to Delve. Until that is approved and released, what can I do to make sense of these values and translate them to a more readable form?

As a workaround, I considered adding new variables of string type that I would update (as needed) with the output of .String() or .Format(...). Is there a better alternative?

Upvotes: 12

Views: 1469

Answers (1)

Nicholas Corin
Nicholas Corin

Reputation: 2404

It looks like the issue adding the functionality you're wanting has been closed.

Closing, you can instead call methods on the time value to format via the call t.Format(...) or call t.String().

Upvotes: 1

Related Questions