Skip to content

feat: support configuring custom time.Location for datetime encoding and decoding via DSN #260

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

almas-x
Copy link

@almas-x almas-x commented May 22, 2025

This improvement introduces the ability to specify a custom timezone through the DSN, enabling users to define a global timezone offset for all DATETIME and TIMESTAMP fields. It is designed to handle scenarios where the database stores datetime values in a specific local timezone rather than UTC.

By allowing a timezone parameter in the DSN, this change resolves issues such as microsoft/go-mssqldb#233, ensuring consistent parsing and encoding of datetime values. This enhancement significantly improves compatibility and flexibility, especially for applications operating across multiple timezones.

Fix issue goravel/goravel#699

@almas-x
Copy link
Author

almas-x commented May 22, 2025

@microsoft-github-policy-service agree

@codecov-commenter
Copy link

codecov-commenter commented May 23, 2025

Codecov Report

Attention: Patch coverage is 91.04478% with 6 lines in your changes missing coverage. Please review.

Project coverage is 74.93%. Comparing base (d3c6336) to head (4012cfa).

Files with missing lines Patch % Lines
bulkcopy.go 62.50% 2 Missing and 1 partial ⚠️
types.go 95.34% 2 Missing ⚠️
token.go 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #260      +/-   ##
==========================================
- Coverage   75.11%   74.93%   -0.19%     
==========================================
  Files          32       33       +1     
  Lines        6471     6458      -13     
==========================================
- Hits         4861     4839      -22     
- Misses       1325     1331       +6     
- Partials      285      288       +3     

☔ View full report in Codecov by Sentry.
📢 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.

@shueybubbles
Copy link
Collaborator

thx for the PR!
Please add some tests that fail without your change.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds support for configuring a custom time.Location via the DSN for all DATETIME and TIMESTAMP operations

  • Introduces a new Timezone DSN parameter and parses it into msdsn.Config.Timezone
  • Defines a global loc variable and swaps out time.UTC for loc in all encode/decode functions
  • Updates bulk-copy methods and modern Go 1.19 code paths to use the configured timezone

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
types.go Swapped hardcoded time.UTC for loc in multiple encode/decode functions
mssql.go Added global loc, set from Config.Timezone in createDialer
mssql_go19.go Updated civil.Date/DateTime/Time encoding calls to use loc
msdsn/conn_str.go Added Timezone constant, Config.Timezone field, and parsing
bulkcopy.go Renamed receiver in CreateBulk* and changed time.ParseInLocation to use loc
Comments suppressed due to low confidence (2)

bulkcopy.go:55

  • [nitpick] Receiver name c is ambiguous. For consistency with other methods, consider renaming it to cn or conn to clarify that it's a connection.
func (c *Conn) CreateBulk(table string, columns []string) (_ *Bulk) {

msdsn/conn_str.go:332

  • There are no unit tests covering the new Timezone DSN parameter parsing. Consider adding tests for valid, invalid, and missing timezone values to ensure robust behavior.
tz, ok := params[Timezone]

@almas-x
Copy link
Author

almas-x commented May 26, 2025

thx for the PR! Please add some tests that fail without your change.

I’ve tried to add some test cases, but I’m not sure if they’re correct. I’m still new to this project and not yet very familiar with how the tests are typically written.

@almas-x almas-x force-pushed the main branch 6 times, most recently from a270a91 to 2a8b901 Compare May 27, 2025 10:31
Copy link
Collaborator

@shueybubbles shueybubbles left a comment

Choose a reason for hiding this comment

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

🕐

@almas-x almas-x force-pushed the main branch 3 times, most recently from 094fd19 to b92afbd Compare May 28, 2025 02:52
@almas-x
Copy link
Author

almas-x commented May 28, 2025

Hi @shueybubbles I’m now passing the timezone through the encoding parameters instead of relying on a global variable. However, despite multiple adjustments, the test continues to fail with the same error (missing Location in call to Date). I could use some help identifying what might be going wrong or if there’s a better way to ensure the timezone is correctly applied during decryption and scanning.

@shueybubbles shueybubbles requested a review from Copilot June 2, 2025 13:59
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enables specifying a custom time.Location via the DSN so that all DATETIME and TIMESTAMP fields are encoded and decoded using that timezone rather than UTC.

  • Updated all decode/encode helpers to accept a *time.Location.
  • Propagated the Encoding.Timezone setting through readFixedType, makeParam, and bulk-copy paths.
  • Extended DSN parsing (msdsn.Parse) and URL generation to handle a timezone parameter, and added tests covering default vs. custom timezone behavior.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
types.go Added loc *time.Location parameters to decode/encode funcs & wrapped reader to capture DSN timezone
msdsn/conn_str.go Added Timezone DSN key, parsed timezone param, and included it in URL
mssql.go Pulled Timezone from Config to drive encodeDateTimeOffset, encodeDateTime2, etc.
mssql_go19.go Same propagation of custom timezone for Go 1.9 driver path
bulkcopy.go Applied custom timezone when encoding values in bulk operations
tds_test.go Initialized Encoding.Timezone from env var
queries_test.go New TestCustomTimezone covering behavior with/without custom timezone
Comments suppressed due to low confidence (1)

queries_test.go:2826

  • Instead of passing a formatted string, pass the time.Time value directly (sql.Named("ts", inputTime)) to ensure the driver’s timezone encoding logic is exercised.
_, err = conn.Exec("insert into test (ts) values (@ts)", sql.Named("ts", inputTime.Format("2006-01-02 15:04:05")))

@almas-x almas-x force-pushed the main branch 3 times, most recently from 8fb2517 to 4e508fc Compare June 3, 2025 06:27
@almas-x almas-x requested a review from shueybubbles June 4, 2025 01:09
@shueybubbles
Copy link
Collaborator

update the readme with directions on how to use the parameter

@almas-x almas-x changed the title feat: support configuring custom time.Location for timestamp/datetime encoding and decoding via DSN feat: support configuring custom time.Location for datetime encoding and decoding via DSN Jun 5, 2025
@almas-x almas-x requested a review from shueybubbles June 5, 2025 03:27
@almas-x
Copy link
Author

almas-x commented Jun 7, 2025

Hi @shueybubbles Please let me know if there’s anything else that needs to be done from my end. I appreciate your time and effort in reviewing this.

@shueybubbles
Copy link
Collaborator

please resolve the comments that have been addressed

@almas-x
Copy link
Author

almas-x commented Jun 8, 2025

please resolve the comments that have been addressed

Completed!

@almas-x almas-x requested a review from shueybubbles June 9, 2025 06:22
@shueybubbles shueybubbles requested a review from stuartpa June 9, 2025 14:25
Copy link
Collaborator

@shueybubbles shueybubbles left a comment

Choose a reason for hiding this comment

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

:shipit:

@almas-x
Copy link
Author

almas-x commented Jun 11, 2025

Hi , @apoorvdeshmukh @stuartpa
I’ve addressed and improved the PR based on all the feedback and suggestions from @shueybubbles . Please let me know if there’s anything else I can do to help move it forward or if any further changes are needed for merging. The Goravel project depends on this package, and we’re hoping to include some recent bug fixes in our upcoming release.
Thank you for your time and consideration!

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.

3 participants