Discordian date: Difference between revisions

Content added Content deleted
No edit summary
(Go solution)
Line 241:
let season, dday = Math.DivRem(dayOfYear, 73)
sprintf "%s %i, %i YOLD" seasons.[season] dday dyear</lang>
=={{header|Go}}==
A package modeled after the time package in the Go standard library
<lang go>package ddate
 
import (
"strconv"
"strings"
"time"
)
 
// Predefined formats for DiscDate.Format
const (
DefaultFmt = "Pungenday, Discord 5, 3131 YOLD"
OldFmt = `Today is Pungenday, the 5th day of Discord in the YOLD 3131
Celebrate Mojoday`
)
 
// Formats passed to DiscDate.Format are protypes for formated dates.
// Format replaces occurrences of prototype elements (the constant strings
// listed here) with values corresponding to the date being formatted.
// If the date is St. Tib's Day, the string from the first date element
// through the last is replaced with "St. Tib's Day".
const (
protoLongSeason = "Discord"
protoShortSeason = "Dsc"
protoLongDay = "Pungenday"
protoShortDay = "PD"
protoOrdDay = "5"
protoCardDay = "5th"
protoHolyday = "Mojoday"
protoYear = "3131"
)
 
var (
longDay = []string{"Sweetmorn", "Boomtime", "Pungenday",
"Prickle-Prickle", "Setting Orange"}
shortDay = []string{"SM", "BT", "PD", "PP", "SO"}
longSeason = []string{
"Chaos", "Discord", "Confusion", "Bureaucracy", "The Aftermath"}
shortSeason = []string{"Chs", "Dsc", "Cfn", "Bcy", "Afm"}
holyday = [][]string{{"Mungday", "Chaoflux"}, {"Mojoday", "Discoflux"},
{"Syaday", "Confuflux"}, {"Zaraday", "Bureflux"}, {"Maladay", "Afflux"}}
)
 
type DiscDate struct {
StTibs bool
Dayy int // zero based day of year, meaningless if StTibs is true
Year int // gregorian + 1166
}
 
func New(eris *time.Time) DiscDate {
t := *eris
t.Month = 1
t.Day = 1
bob := int(eris.Seconds()-t.Seconds()) / (24 * 60 * 60)
raw := int(eris.Year)
hastur := DiscDate{Year: raw + 1166}
if raw%4 == 0 && (raw%100 != 0 || raw%400 == 0) {
if bob > 59 {
bob--
} else if bob == 59 {
hastur.StTibs = true
return hastur
}
}
hastur.Dayy = bob
return hastur
}
 
func (dd DiscDate) Format(f string) (r string) {
var st, snarf string
var dateElement bool
f6 := func(proto, wibble string) {
if !dateElement {
snarf = r
dateElement = true
}
if st > "" {
r = ""
} else {
r += wibble
}
f = f[len(proto):]
}
f4 := func(proto, wibble string) {
if dd.StTibs {
st = "St. Tib's Day"
}
f6(proto, wibble)
}
season, day := dd.Dayy/73, dd.Dayy%73
for f > "" {
switch {
case strings.HasPrefix(f, protoLongDay):
f4(protoLongDay, longDay[dd.Dayy%5])
case strings.HasPrefix(f, protoShortDay):
f4(protoShortDay, shortDay[dd.Dayy%5])
case strings.HasPrefix(f, protoCardDay):
funkychickens := "th"
if day/10 != 1 {
switch day % 10 {
case 0:
funkychickens = "st"
case 1:
funkychickens = "nd"
case 2:
funkychickens = "rd"
}
}
f4(protoCardDay, strconv.Itoa(day+1)+funkychickens)
case strings.HasPrefix(f, protoOrdDay):
f4(protoOrdDay, strconv.Itoa(day+1))
case strings.HasPrefix(f, protoLongSeason):
f6(protoLongSeason, longSeason[season])
case strings.HasPrefix(f, protoShortSeason):
f6(protoShortSeason, shortSeason[season])
case strings.HasPrefix(f, protoHolyday):
if day == 4 {
r += holyday[season][0]
} else if day == 49 {
r += holyday[season][1]
}
f = f[len(protoHolyday):]
case strings.HasPrefix(f, protoYear):
r += strconv.Itoa(dd.Year)
f = f[4:]
default:
r += f[:1]
f = f[1:]
}
}
if st > "" {
r = snarf + st + r
}
return
}</lang>
Example program using above package
<lang go>package main
 
import (
"ddate"
"fmt"
"os"
"strings"
"time"
)
 
func main() {
pi := 1
fnord := ddate.DefaultFmt
if len(os.Args) > 1 {
switch os.Args[1][0] {
case '+':
pi++
fnord = os.Args[1][1:]
case '-':
usage()
}
}
var eris *time.Time
switch len(os.Args) - pi {
case 0:
eris = time.LocalTime()
case 3:
var err os.Error
eris, err = time.Parse("2 1 2006", strings.Join(os.Args[pi:pi+3], " "))
if err != nil {
fmt.Println(err)
usage()
}
default:
usage()
}
fmt.Println(ddate.New(eris).Format(fnord))
}
 
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s [+format] [day month year]\n", os.Args[0])
os.Exit(1)
}</lang>
Example output:
<pre>
> ddate
Pungenday, Confusion 62, 3177 YOLD
 
> ddate 29 2 2012
St. Tib's Day, 3178 YOLD
</pre>
 
=={{header|Haskell}}==