customizable date display format

This commit is contained in:
Jill 2022-12-31 21:44:56 +03:00
parent 09eee68e51
commit 571041f8f8
4 changed files with 37 additions and 7 deletions

View File

@ -13,6 +13,14 @@
# leaving blank will disable this
append_path = ""
[formatting]
# whether to format dates as relative or absolute
# "relative" = relative, "absolute" = absolute
# do note that absolute times can result in uglier
# times due to colons being forbiddne in certain
# spots
date = "relative"
[accounts]
# allow new accounts to be created
allow_registration = true
@ -30,4 +38,12 @@ allow_demon_votes = true
# will go from NA to the average
min_votes = 10
# same as above, but for demon ratings
min_demon_votes = 10
min_demon_votes = 10
[levels]
# prevent users from deleting their own levels
# if they are rated
prevent_deletion_rated = true
# prevent users from deleting their own levels
# if they are featured
prevent_deletion_featured = true

View File

@ -98,7 +98,7 @@ CrystalGauntlet.endpoints["/getGJLevelScores211.php"] = ->(body : String): Strin
3 => percent,
6 => i,
13 => coins,
42 => Format.fmt_timespan(Time.utc - Time.parse(set_at, Format::TIME_FORMAT, Time::Location::UTC))
42 => Time.parse(set_at, Format::TIME_FORMAT, Time::Location::UTC)
})
end

View File

@ -29,7 +29,7 @@ CrystalGauntlet.endpoints["/getGJAccountComments20.php"] = ->(body : String): St
4 => likes,
5 => 0,
7 => likes < -3, # todo: config?
#9 => Format.fmt_timespan(Time.utc - Time.parse(created_at, Format::TIME_FORMAT, Time::Location::UTC)),
9 => Time.parse(created_at, Format::TIME_FORMAT, Time::Location::UTC),
6 => id
})
end

View File

@ -2,6 +2,10 @@ module CrystalGauntlet::Format
extend self
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
# used when sending back to the client as an absolute date
TIME_FORMAT_GD_FRIENDLY = "%d/%m/%Y %H.%M"
# safe to send back in comments
TIME_FORMAT_USER_FRIENDLY = "%d/%m/%Y %H:%M"
def fmt_timespan(s : Time::Span) : String
seconds = s.total_seconds.floor()
@ -26,14 +30,24 @@ module CrystalGauntlet::Format
end
end
def fmt_value(v) : String
def fmt_time(s : Time, colon_safe=false) : String
s.to_s(colon_safe ? TIME_FORMAT_USER_FRIENDLY : TIME_FORMAT_GD_FRIENDLY)
end
def fmt_value(v, colon_safe=false) : String
case v
when Bool
v ? "1" : "0"
when String
v
when Time::Span
fmt_span(v)
fmt_timespan(v)
when Time
if config_get("formatting.date") == "relative"
fmt_timespan(Time.utc - v)
else
fmt_time(v, colon_safe)
end
else
v.to_s
end
@ -44,11 +58,11 @@ module CrystalGauntlet::Format
end
def fmt_song(hash) : String
hash.map_with_index{ |(i, v)| "#{i}~|~#{fmt_value(v)}" }.join("~|~")
hash.map_with_index{ |(i, v)| "#{i}~|~#{fmt_value(v, true)}" }.join("~|~")
end
def fmt_comment(hash) : String
hash.map_with_index{ |(i, v)| "#{i}~#{fmt_value(v)}" }.join("~")
hash.map_with_index{ |(i, v)| "#{i}~#{fmt_value(v, true)}" }.join("~")
end
end