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

Add Time() method to MQMD type #159

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ibmmq/ibmmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.
package ibmmq

import (
"errors"
"testing"
"time"
)

// Tests for mqistr.go
Expand Down Expand Up @@ -289,3 +291,38 @@ func TestRoundTo4(t *testing.T) {
}
}
}

func TestMQMDTime(t *testing.T) {
testDateTime, _ := time.Parse(time.RFC3339, "2015-12-14T07:00:00.99Z")

// correct time
md := &MQMD{
PutDate: "20151214",
PutTime: "07000099",
}
got, err := md.Time()
if err != nil {
t.Errorf("%s%s: expected nil error, got %v", md.PutDate, md.PutTime, err)
}
if got != testDateTime {
t.Errorf("%s%s: expected %v, got %v", md.PutDate, md.PutTime, testDateTime, got)
}

// fail time parsing
md = &MQMD{
PutDate: "bad date",
PutTime: "07000099",
}
got, err = md.Time()
var parseErr *time.ParseError
if !errors.As(err, &parseErr) {
t.Errorf("%s%s: expected time.ParseError{}, got %T", md.PutDate, md.PutTime, err)
}

// empty fields
md = NewMQMD()
got, err = md.Time()
if err != ErrMDEmptyTime {
t.Errorf("empty fields: expected ErrMQMDTimeIsNil, got %v", err)
}
}
26 changes: 26 additions & 0 deletions ibmmq/mqiMQMD.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import "C"

import (
"bytes"
"errors"
"time"
)

/*
Expand Down Expand Up @@ -103,6 +105,30 @@ func NewMQMD() *MQMD {
return md
}

// Go time package reference date time as
// concatenated date and time from MD
const mqmdGoRefTime = "20060102150405.99"

// ErrMDEmptyTime is returned from the MQMD.Time() method
// if the PutTime field is empty
var ErrMDEmptyTime = errors.New("PutTime field in MQMD is empty")

// Time returns the MQMD PutDate and PutTime as time.Time
func (md *MQMD) Time() (time.Time, error) {
if len(md.PutTime) != 8 {
return time.Time{}, ErrMDEmptyTime
}

// separate fractions of a second with a dot
s := md.PutDate + md.PutTime[:6] + "." + md.PutTime[6:]
t, err := time.Parse(mqmdGoRefTime, s)
if err != nil {
return time.Time{}, err
}

return t, nil
}

func checkMD(gomd *MQMD, verb string) error {
mqrc := C.MQRC_NONE

Expand Down