feat: refactored code to use core:time package.

This commit is contained in:
Spencer Brower
2026-07-15 11:50:55 -04:00
parent 7aa411490a
commit 93cf74ee0b
2 changed files with 28 additions and 51 deletions
+18 -24
View File
@@ -2,8 +2,7 @@ package main
import "core:fmt" import "core:fmt"
import "core:strings" import "core:strings"
import "core:time"
WEEKDAYS: [7]string = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
generate_rss :: proc(pages: []Page, config: Site) -> string { generate_rss :: proc(pages: []Page, config: Site) -> string {
parts: [dynamic]string parts: [dynamic]string
@@ -104,35 +103,30 @@ generate_sitemap :: proc(pages: []Page, base_url: string) -> string {
return strings.join(parts[:], "") return strings.join(parts[:], "")
} }
// TODO: Leaks
format_rfc822 :: proc(iso: string) -> string { format_rfc822 :: proc(iso: string) -> string {
if len(iso) < 19 { if len(iso) < 19 {
// TODO: should indicate error somehow
return iso return iso
} }
year := date, offset, _ := time.iso8601_to_time_and_offset(iso)
(int(iso[0]) - 0x30) * 1000 +
(int(iso[1]) - 0x30) * 100 +
(int(iso[2]) - 0x30) * 10 +
(int(iso[3]) - 0x30)
month := (int(iso[5]) - 0x30) * 10 + (int(iso[6]) - 0x30)
day := (int(iso[8]) - 0x30) * 10 + (int(iso[9]) - 0x30)
time := iso[11:19]
// Timezone: -04:00 → -0400 weekday := fmt.tprintf("%s", time.weekday(date))
tz := "+0000" month := fmt.tprintf("%s", time.month(date))
if len(iso) >= 25 && (iso[19] == '+' || iso[19] == '-') { buf: [8]byte
tz = fmt.tprintf("%c%s%s", iso[19], iso[20:22], iso[23:25]) t := time.to_string_hms(date, buf[:])
}
// Sakamoto's method for day of week (0=Sunday) return fmt.aprintf(
t := [12]int{0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4} "%s, %02d %s %d %s %3d%2d",
y := year weekday[:3],
if month < 3 { time.day(date),
y -= 1 month[:3],
} time.year(date),
dow := (y + y / 4 - y / 100 + y / 400 + t[month - 1] + day) % 7 t,
offset / 60,
return fmt.aprintf("%s, %d %s %d %s %s", WEEKDAYS[dow], day, MONTHS[month - 1], year, time, tz) offset % 60,
)
} }
xml_escape :: proc(s: string) -> string { xml_escape :: proc(s: string) -> string {
+10 -27
View File
@@ -6,21 +6,8 @@ import "mustache"
import "core:fmt" import "core:fmt"
import "core:os" import "core:os"
import "core:strings" import "core:strings"
import "core:time"
MONTHS: [12]string = { import "core:time/datetime"
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}
Page_Context :: struct { Page_Context :: struct {
permalink: string, permalink: string,
@@ -199,7 +186,11 @@ load_partials :: proc(layouts_dir: string) -> map[string]string {
return partials return partials
} }
load_partials_recursive :: proc(partials: ^map[string]string, base_dir: string, rel_prefix: string) { load_partials_recursive :: proc(
partials: ^map[string]string,
base_dir: string,
rel_prefix: string,
) {
entries, err := os.read_all_directory_by_path(base_dir, context.allocator) entries, err := os.read_all_directory_by_path(base_dir, context.allocator)
if err != nil { if err != nil {
return return
@@ -334,19 +325,11 @@ render_posts_html :: proc(pages: []Page, config: Site) -> string {
return result return result
} }
// TODO: Leaks
format_date :: proc(iso: string) -> string { format_date :: proc(iso: string) -> string {
if len(iso) < 10 { date, _, _, _ := time.iso8601_to_components(iso)
return iso
}
year := iso[:4]
month_num := (int(iso[5]) - 0x30) * 10 + (int(iso[6]) - 0x30)
day_num := (int(iso[8]) - 0x30) * 10 + (int(iso[9]) - 0x30)
if month_num < 1 || month_num > 12 { return fmt.aprintf("%s %d, %d", time.Month(date.month), date.day, date.year)
return iso[:10]
}
return fmt.aprintf("%d %s %s", day_num, MONTHS[month_num - 1], year)
} }
get_year :: proc(iso: string) -> string { get_year :: proc(iso: string) -> string {