Skip to content

Commit

Permalink
Merge pull request #614 from scarf005/feat-custom-date-format
Browse files Browse the repository at this point in the history
feat: customizable date format
  • Loading branch information
muety authored Mar 12, 2024
2 parents 80dc552 + 7671c1e commit df83e99
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ You can specify configuration options either via a config file (default: `config
| `app.heartbeat_max_age /`<br>`WAKAPI_HEARTBEAT_MAX_AGE` | `4320h` | Maximum acceptable age of a heartbeat (see [`ParseDuration`](https://pkg.go.dev/time#ParseDuration)) |
| `app.custom_languages` | - | Map from file endings to language names |
| `app.avatar_url_template` /<br>`WAKAPI_AVATAR_URL_TEMPLATE` | (see [`config.default.yml`](config.default.yml)) | URL template for external user avatar images (e.g. from [Dicebear](https://dicebear.com) or [Gravatar](https://gravatar.com)) |
| `app.date_format` /<br>`WAKAPI_DATE_FORMAT` | `Mon, 02 Jan 2006` | Go time format strings to format human-readable date (see [`Time.Format`](https://pkg.go.dev/time#Time.Format)) |
| `app.datetime_format` /<br>`WAKAPI_DATETIME_FORMAT` | `Mon, 02 Jan 2006 15:04` | Go time format strings to format human-readable datetime (see [`Time.Format`](https://pkg.go.dev/time#Time.Format)) |
| `app.support_contact` /<br>`WAKAPI_SUPPORT_CONTACT` | `[email protected]` | E-Mail address to display as a support contact on the page |
| `app.data_retention_months` /<br>`WAKAPI_DATA_RETENTION_MONTHS` | `-1` | Maximum retention period in months for user data (heartbeats) (-1 for unlimited) |
| `server.port` /<br> `WAKAPI_PORT` | `3000` | Port to listen on |
Expand Down
7 changes: 6 additions & 1 deletion config.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ app:
# defaults to wakapi's internal avatar rendering powered by https://codeberg.org/Codeberg/avatars
avatar_url_template: api/avatar/{username_hash}.svg

# go time format strings to format human-readable dates
# for details, check https://pkg.go.dev/time#Time.Format
date_format: Mon, 02 Jan 2006
datetime_format: Mon, 02 Jan 2006 15:04

db:
host: # leave blank when using sqlite3
port: # leave blank when using sqlite3
Expand Down Expand Up @@ -94,4 +99,4 @@ mail:
port:
username:
password:
tls:
tls:
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ type appConfig struct {
DataCleanupDryRun bool `yaml:"data_cleanup_dry_run" default:"false" env:"WAKAPI_DATA_CLEANUP_DRY_RUN"` // for debugging only
AvatarURLTemplate string `yaml:"avatar_url_template" default:"api/avatar/{username_hash}.svg" env:"WAKAPI_AVATAR_URL_TEMPLATE"`
SupportContact string `yaml:"support_contact" default:"[email protected]" env:"WAKAPI_SUPPORT_CONTACT"`
DateFormat string `yaml:"date_format" default:"Mon, 02 Jan 2006" env:"WAKAPI_DATE_FORMAT"`
DateTimeFormat string `yaml:"datetime_format" default:"Mon, 02 Jan 2006 15:04" env:"WAKAPI_DATETIME_FORMAT"`
CustomLanguages map[string]string `yaml:"custom_languages"`
Colors map[string]map[string]string `yaml:"-"`
}
Expand Down Expand Up @@ -482,6 +484,12 @@ func Load(configFlag string, version string) *Config {
if config.Security.TrustedHeaderAuth && len(config.Security.trustReverseProxyIpParsed) == 0 {
config.Security.TrustedHeaderAuth = false
}
if _, err := time.Parse(config.App.DateFormat, "2006-01-02"); err != nil {
logbuch.Fatal("invalid date format '%s'", config.App.DateFormat)
}
if _, err := time.Parse(config.App.DateTimeFormat, "2006-01-02 15:04"); err != nil {
logbuch.Fatal("invalid datetime format '%s'", config.App.DateTimeFormat)
}

cronParser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)

Expand Down
4 changes: 2 additions & 2 deletions helpers/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func FormatDateTime(date time.Time) string {
}

func FormatDateTimeHuman(date time.Time) string {
return date.Format("Mon, 02 Jan 2006 15:04")
return date.Format(config.Get().App.DateTimeFormat)
}

func FormatDateHuman(date time.Time) string {
return date.Format("Mon, 02 Jan 2006")
return date.Format(config.Get().App.DateFormat)
}

func FmtWakatimeDuration(d time.Duration) string {
Expand Down

0 comments on commit df83e99

Please sign in to comment.