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

Time isZero after String/Byte conv #60

Open
HenryVolkmer opened this issue Mar 28, 2024 · 1 comment
Open

Time isZero after String/Byte conv #60

HenryVolkmer opened this issue Mar 28, 2024 · 1 comment

Comments

@HenryVolkmer
Copy link

hi,

this test fails, any ideas?

package main

import (
    "testing"
    "time"
    "github.com/influxdata/line-protocol/v2/lineprotocol"
)

func TestTime(t *testing.T) {
    enc := lineprotocol.Encoder{}
    enc.StartLine("a_measurement")
    enc.AddField("temp",lineprotocol.MustNewValue(int64(10)))
    enc.EndLine(time.Now())

    // explicite from bytes to string and from string to bytes
    str := string(enc.Bytes())
    dec := lineprotocol.NewDecoderWithBytes([]byte(str))

    tests := map[string]lineprotocol.Precision {
        "nano": lineprotocol.Nanosecond,
        "micro": lineprotocol.Microsecond,
        "second": lineprotocol.Second,
    }

    for label,p := range tests {
        ti, err := dec.Time(p, time.Time{})
        if err != nil {
            panic(err)
        }
        if ti.IsZero() {
            t.Errorf("decoder.Time for precision %s should not be zero",label)
        }   
    }
}
@tesibelda
Copy link

Hi @HenryVolkmer, you are encodig with default precision (nanosecond) but trying to decode with other precisions. Using SetPrecision to encode and calling decoder.Next() before decoding makes the code not to fail:

package main

import (
	"testing"
	"time"

	"github.com/influxdata/line-protocol/v2/lineprotocol"
)

func TestTime(t *testing.T) {
	tests := map[string]lineprotocol.Precision{
		"nano":   lineprotocol.Nanosecond,
		"micro":  lineprotocol.Microsecond,
		"second": lineprotocol.Second,
	}

	for label, p := range tests {
		enc := lineprotocol.Encoder{}
		enc.SetPrecision(p)
		enc.StartLine("a_measurement")
		enc.AddField("temp", lineprotocol.MustNewValue(int64(10)))
		enc.EndLine(time.Now())

		// explicite from bytes to string and from string to bytes
		str := string(enc.Bytes())
		dec := lineprotocol.NewDecoderWithBytes([]byte(str))
		dec.Next()
		ti, err := dec.Time(p, time.Time{})
		if err != nil {
			panic(err)
		}
		if ti.IsZero() {
			t.Errorf("decoder.Time for precision %s should not be zero", label)
		}
	}
}

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