Skip to content

feat: add support for Go-style date parsing and formatting in nepalitime package#30

Closed
codernirdesh wants to merge 1 commit intoopensource-nepal:mainfrom
codernirdesh:main
Closed

feat: add support for Go-style date parsing and formatting in nepalitime package#30
codernirdesh wants to merge 1 commit intoopensource-nepal:mainfrom
codernirdesh:main

Conversation

@codernirdesh
Copy link

This pull request introduces support for Go-style date and time layout parsing and formatting in the nepalitime package, along with corresponding updates to the documentation and test cases. The key changes include the addition of ParseGoLayout and FormatGoLayout methods, tests for these new features, and updates to the README.md to document their usage.

New Feature: Go-Style Layout Parsing and Formatting

  • Parsing Go-Style Layouts: Added the ParseGoLayout function in nepalitime/parser.go to parse the current system date using Go-style layouts. This provides seamless integration with Go's native date and time formatting conventions.
  • Formatting Go-Style Layouts: Added the FormatGoLayout method in nepalitime/formatter.go to format NepaliTime objects into Go-style layout strings.

Documentation Updates

  • Usage Examples: Updated README.md with examples for parsing and formatting Go-style layouts, including detailed explanations and code snippets. [1] [2]

Test Enhancements

  • New Tests for Parsing: Added TestParseGoLayout in nepalitime/parser_test.go to validate the functionality of ParseGoLayout.
  • New Tests for Formatting: Added TestFormatGoLayout in nepalitime/formatter_test.go to ensure the FormatGoLayout method formats dates correctly.

Minor Changes

  • Import Update: Added the time package to nepalitime/parser.go to support the new parsing functionality

Fixes Support GO parsing and formatting layout #6

@aj3sh
Copy link
Member

aj3sh commented Apr 22, 2025

Thank you @codernirdesh for your contribution. We will review the code shortly.

@aj3sh
Copy link
Member

aj3sh commented Apr 22, 2025

@codernirdesh, Your tests are failing on CI, could you please check on it.

@sugat009
Copy link
Contributor

@codernirdesh Thanks for the contribution and the fantastic write-up! Quick note: we just bumped the Go version to 1.24 (used to be 1.19). Could you grab the latest from main and tweak your changes if required?

Copy link
Contributor

@sugat009 sugat009 left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution, @codernirdesh . I've found a few issues.

@aj3sh and I originally considered implementing all this functionality directly in the Parse and Format functions. However, splitting into two separate functions aligns better with the single responsibility principle.
To make our code more similar to the time library in the future, we should eventually move the changes in this PR into the Parse and Format functions, while renaming the current functions to ParseLayout and FormatLayout. Since this would be a breaking change requiring a major version release, the interim solution is to implement these changes as ParseLayout and FormatLayout functions now, then rename them in a future major release.

Comment on lines +183 to +185
### New Feature: Go-Style Layout Parsing

The `nepalitime` package now supports parsing Go-style layouts for dates and times. This feature allows you to parse the current system date using a layout string that follows Go's native date and time formatting conventions.
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: I feel like this section is not necessary. Right now, the feature is new and makes sense, but down the line, after some time, it won't be a "new feature" anymore and this section will need editing.

// Updated ParseGoLayout to only use layout
func ParseGoLayout(layout string) (*NepaliTime, error) {
currentTime := time.Now()
parsedTime, err := time.Parse(layout, currentTime.Format(layout))
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: This function would parse the current time, which is constantly moving, to a given layout. What if I wanted to parse a custom date string like in the example given in the ticket?

Comment on lines +53 to +56
func (obj *NepaliFormatter) FormatGoLayout(layout string) string {
englishTime := obj.nepaliTime.GetEnglishTime()
return englishTime.Format(layout)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: the target of formatting is intended to be for the Nepali dates and not the English date equivalents. For example: when I do nepaliFormatter.FormatGoLayout('2006-02-01'), I'd be expecting the Nepali date to be formatted in the 2006-02-01 format, which would be 2056-10-18.


layout := "Jan 2, 2006 at 3:04pm (MST)"

npTime, err := nepalitime.ParseGoLayout(layout)
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Let's rename this to a simpler ParseLayout. I feel like the Go is a given.
CC: @aj3sh

layout := "Jan 2, 2006 at 3:04pm (MST)"

formatter := nepalitime.NewFormatter(npTime)
formatted := formatter.FormatGoLayout(layout)
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: similar to above. Let's rename this to FormatLayout.

@aj3sh aj3sh added the stale label Jun 7, 2025
@aj3sh aj3sh closed this Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants