Repository: nats-io/nsc
Pull request #358 (2021-02-04 20:26:37)

[added] ability to set locale to use for times
Signed-off-by: Matthias Hanel <mh@synadia.com>


Patch:
diff --git a/cmd/edituser.go b/cmd/edituser.go
index 5ae844b..e647284 100644
--- a/cmd/edituser.go
+++ b/cmd/edituser.go
@@ -74,6 +74,7 @@ nsc edit user --name <n> --rm-response-perms
 
 	cmd.Flags().VarP(&params.times, "time", "", fmt.Sprintf(`add start-end time range of the form "%s-%s" (option can be specified multiple times)`, timeFormat, timeFormat))
 	cmd.Flags().StringSliceVarP(&params.rmTimes, "rm-time", "", nil, fmt.Sprintf(`remove start-end time by start time "%s" (option can be specified multiple times)`, timeFormat))
+	cmd.Flags().VarP(&params.locale, "locale", "", "set the locale with which time values are interpreted")
 
 	cmd.Flags().StringSliceVarP(&params.tags, "tag", "", nil, "add tags for user - comma separated list or option can be specified multiple times")
 	cmd.Flags().StringSliceVarP(&params.rmTags, "rm-tag", "", nil, "remove tag - comma separated list or option can be specified multiple times")
@@ -117,6 +118,7 @@ type EditUserParams struct {
 	src         []string
 	times       timeSlice
 	rmTimes     []string
+	locale      timeLocale
 	payload     DataParams
 	bearer      bool
 	connTypes   []string
@@ -296,6 +298,9 @@ func (p *EditUserParams) Run(ctx ActionCtx) (store.Status, error) {
 	sort.Strings(srcList)
 	p.claim.Src = srcList
 
+	if flags.Changed("locale") {
+		p.claim.Locale = p.locale.String()
+	}
 	for _, v := range p.times {
 		r.AddOK("added time range %s-%s", v.Start, v.End)
 		p.claim.Times = append(p.claim.Times, v)
@@ -410,3 +415,21 @@ func (t *timeSlice) String() string {
 func (t *timeSlice) Type() string {
 	return "time-ranges"
 }
+
+type timeLocale string
+
+func (l *timeLocale) Set(val string) error {
+	v, err := time.LoadLocation(val)
+	if err == nil {
+		*l = timeLocale(v.String())
+	}
+	return err
+}
+
+func (l *timeLocale) String() string {
+	return string(*l)
+}
+
+func (t *timeLocale) Type() string {
+	return "time-locale"
+}


Test patch:
diff --git a/cmd/edituser_test.go b/cmd/edituser_test.go
index 89c62cb..1b6fbec 100644
--- a/cmd/edituser_test.go
+++ b/cmd/edituser_test.go
@@ -183,7 +183,7 @@ func Test_EditUser_Times(t *testing.T) {
 
 	ts.AddUser(t, "A", "a")
 
-	_, _, err := ExecuteCmd(createEditUserCmd(), "--time", "16:04:05-17:04:09", "--time", "18:04:05-19:04:09")
+	_, _, err := ExecuteCmd(createEditUserCmd(), "--time", "16:04:05-17:04:09", "--time", "18:04:05-19:04:09", "--locale", "America/New_York")
 	require.NoError(t, err)
 
 	cc, err := ts.Store.ReadUserClaim("A", "a")
@@ -193,8 +193,9 @@ func Test_EditUser_Times(t *testing.T) {
 	require.ElementsMatch(t, cc.Times, []jwt.TimeRange{
 		{Start: "16:04:05", End: "17:04:09"},
 		{Start: "18:04:05", End: "19:04:09"}})
+	require.Equal(t, "America/New_York", cc.Locale)
 
-	_, _, err = ExecuteCmd(createEditUserCmd(), "--rm-time", "16:04:05")
+	_, _, err = ExecuteCmd(createEditUserCmd(), "--rm-time", "16:04:05", "--locale", "")
 	require.NoError(t, err)
 
 	cc, err = ts.Store.ReadUserClaim("A", "a")
@@ -202,6 +203,7 @@ func Test_EditUser_Times(t *testing.T) {
 	require.NotNil(t, cc)
 	require.ElementsMatch(t, cc.Times, []jwt.TimeRange{
 		{Start: "18:04:05", End: "19:04:09"}})
+	require.Equal(t, "UTC", cc.Locale)
 }
 
 func Test_EditUserSK(t *testing.T) {