Skip to content

Replace init() with explicit timezone initialization - #25

Merged
bamnet merged 2 commits into
masterfrom
issue-11-explicit-timezone
Jun 30, 2026
Merged

Replace init() with explicit timezone initialization#25
bamnet merged 2 commits into
masterfrom
issue-11-explicit-timezone

Conversation

@bamnet-bot

Copy link
Copy Markdown
Collaborator

Fixes #11

  • Removes global tz variable and init() from util.go
  • Adds location *time.Location field to Client struct
  • NewClient sets location to America/New_York (UTC fallback on error)
  • NewClientWithLocation allows callers to specify a custom timezone
  • NewCustomClient uses the default America/New_York location
  • parseTime becomes a method on *Client
  • Tests cover default location, custom location, invalid location fallback

- Remove global tz variable and init() from util.go
- Add location *time.Location field to Client struct
- NewClient sets location to America/New_York (UTC fallback)
- NewClientWithLocation allows callers to specify a custom timezone
- NewCustomClient sets default America/New_York location
- parseTime becomes a method on *Client
- Add tests for timezone initialization and fallback
@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.67%. Comparing base (36fecc3) to head (40da16c).

Files with missing lines Patch % Lines
client.go 87.50% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #25      +/-   ##
==========================================
+ Coverage   88.25%   88.67%   +0.42%     
==========================================
  Files           5        5              
  Lines         349      362      +13     
==========================================
+ Hits          308      321      +13     
  Misses         24       24              
  Partials       17       17              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the API client to support configurable timezone locations for parsing timestamps, replacing the previous hardcoded global timezone. The feedback suggests preventing potential nil pointer dereferences in parseTime when the client or its location is nil, and recommends passing *time.Location directly to NewClientWithLocation instead of a string to avoid silent fallbacks and align with Go idioms.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread util.go
Comment on lines +32 to 34
func (c *Client) parseTime(ts string) (time.Time, error) {
return time.ParseInLocation("02-Jan-2006 03:04:05 PM", ts, c.location)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If c or c.location is nil (for example, if a Client is initialized manually without using the constructor functions), calling c.parseTime will cause a nil pointer dereference panic. Adding a defensive check to default to time.UTC ensures safety and robustness.

func (c *Client) parseTime(ts string) (time.Time, error) {
	loc := time.UTC
	if c != nil && c.location != nil {
		loc = c.location
	}
	return time.ParseInLocation("02-Jan-2006 03:04:05 PM", ts, loc)
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread client.go
Comment on lines 54 to 73
func NewClient(baseURL, username, password string) *Client {
return NewClientWithLocation(baseURL, username, password, "America/New_York")
}

// NewClientWithLocation constructs a new client with a custom timezone location.
// The location string must be a valid IANA Time Zone name (e.g. "America/New_York").
// If the location fails to load, it falls back to UTC.
func NewClientWithLocation(baseURL, username, password, location string) *Client {
loc, err := time.LoadLocation(location)
if err != nil {
loc = time.UTC
}
return &Client{
httpClient: &http.Client{Timeout: 30 * time.Second},
baseURL: baseURL,
username: username,
password: password,
location: loc,
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Accepting a *time.Location directly in NewClientWithLocation is more idiomatic in Go and avoids silent fallbacks to UTC when an invalid timezone string is passed. Silent fallbacks can lead to hard-to-debug issues where times are parsed in UTC instead of the intended timezone due to a typo. Callers can load the location themselves using time.LoadLocation and handle any errors explicitly.

func NewClient(baseURL, username, password string) *Client {
	return NewClientWithLocation(baseURL, username, password, defaultLocation())
}

// NewClientWithLocation constructs a new client with a custom timezone location.
// If the provided location is nil, it falls back to UTC.
func NewClientWithLocation(baseURL, username, password string, loc *time.Location) *Client {
	if loc == nil {
		loc = time.UTC
	}
	return &Client{
		httpClient: &http.Client{Timeout: 30 * time.Second},
		baseURL:    baseURL,
		username:   username,
		password:   password,
		location:   loc,
	}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread client_test.go
Comment on lines +209 to +221
func TestNewClientWithLocation(t *testing.T) {
c := NewClientWithLocation("http://example.com", "user", "pass", "UTC")
if c.location != time.UTC {
t.Errorf("expected UTC location, got %v", c.location)
}
}

func TestNewClientWithLocationFallback(t *testing.T) {
c := NewClientWithLocation("http://example.com", "user", "pass", "bogus/timezone")
if c.location != time.UTC {
t.Errorf("expected UTC fallback for invalid location, got %v", c.location)
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update the tests to match the new signature of NewClientWithLocation which accepts a *time.Location directly.

Suggested change
func TestNewClientWithLocation(t *testing.T) {
c := NewClientWithLocation("http://example.com", "user", "pass", "UTC")
if c.location != time.UTC {
t.Errorf("expected UTC location, got %v", c.location)
}
}
func TestNewClientWithLocationFallback(t *testing.T) {
c := NewClientWithLocation("http://example.com", "user", "pass", "bogus/timezone")
if c.location != time.UTC {
t.Errorf("expected UTC fallback for invalid location, got %v", c.location)
}
}
func TestNewClientWithLocation(t *testing.T) {
c := NewClientWithLocation("http://example.com", "user", "pass", time.UTC)
if c.location != time.UTC {
t.Errorf("expected UTC location, got %v", c.location)
}
}
func TestNewClientWithLocationFallback(t *testing.T) {
c := NewClientWithLocation("http://example.com", "user", "pass", nil)
if c.location != time.UTC {
t.Errorf("expected UTC fallback for nil location, got %v", c.location)
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@bamnet
bamnet merged commit 807455c into master Jun 30, 2026
3 checks passed
@bamnet
bamnet deleted the issue-11-explicit-timezone branch June 30, 2026 04:06
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

Successfully merging this pull request may close these issues.

Replace init() with explicit timezone initialization

2 participants