Skip to content

Commit

Permalink
allow user to configure timezone
Browse files Browse the repository at this point in the history
refer #162
  • Loading branch information
ananthakumaran committed Jan 29, 2024
1 parent 6d66648 commit bb4e242
Show file tree
Hide file tree
Showing 18 changed files with 3,750 additions and 21 deletions.
6 changes: 6 additions & 0 deletions docs/blog/posts/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ numbers in the default currency.
Paisa also allows you to control the number of digits shown after the
decimal point. By default, it's set to zero.

### `time_zone`

Paisa by default will try to use the system time zone. But if you
deploy the application in a server, then you would have to set the
time zone explicitly, as the server time zone might not be same as
your local time zone. The time zone is used to parse and format dates.

## Journal

Expand Down
7 changes: 7 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ amount_alignment_column: 52
# OPTIONAL, DEFAULT: en-IN
locale: en-IN

# The time zone used to parse and format dates. If not set, system
# time zone will be used. Example values are Asia/Kolkata,
# America/New_York, etc
#
# OPTIONAL, DEFAULT: system timezone
time_zone: ""

# First month of the financial year. This can be set to 1 to follow
# January to December.
#
Expand Down
21 changes: 21 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ type Config struct {
DisplayPrecision int `json:"display_precision" yaml:"display_precision"`
AmountAlignmentColumn int `json:"amount_alignment_column" yaml:"amount_alignment_column"`
Locale string `json:"locale" yaml:"locale"`
TimeZone string `json:"time_zone" yaml:"time_zone"`
FinancialYearStartingMonth time.Month `json:"financial_year_starting_month" yaml:"financial_year_starting_month"`
WeekStartingDay time.Weekday `json:"week_starting_day" yaml:"week_starting_day"`
Strict BoolType `json:"strict" yaml:"strict"`
Expand All @@ -159,6 +160,7 @@ type Config struct {

var config Config
var configPath string
var location *time.Location

var defaultConfig = Config{
Readonly: false,
Expand All @@ -167,6 +169,7 @@ var defaultConfig = Config{
DisplayPrecision: 0,
AmountAlignmentColumn: 52,
Locale: "en-IN",
TimeZone: "",
Budget: Budget{Rollover: Yes},
FinancialYearStartingMonth: 4,
Strict: No,
Expand Down Expand Up @@ -320,6 +323,16 @@ func LoadConfig(content []byte, cp string) error {
configPath = cp
}

if config.TimeZone == "" {
location = time.Local
} else {
location, err = time.LoadLocation(config.TimeZone)
if err != nil {
location = time.Local
return errors.New(fmt.Sprintf("Invalid time zone: %s\n%#v", config.TimeZone, err))
}
}

return nil
}

Expand Down Expand Up @@ -393,3 +406,11 @@ func EnsureLogFilePath() (string, error) {
func DefaultCurrency() string {
return config.DefaultCurrency
}

func TimeZone() *time.Location {
if location != nil {
return location
}

return time.Local
}
Loading

0 comments on commit bb4e242

Please sign in to comment.