Skip to content

Instantly share code, notes, and snippets.

@gernest
Created January 8, 2014 14:31
Show Gist options
  • Save gernest/8317625 to your computer and use it in GitHub Desktop.
Save gernest/8317625 to your computer and use it in GitHub Desktop.
A simple commandline calendar in go
package main
import (
"fmt"
"time"
)
func main() {
//A simple application for printing out a calendar
//Algorithm used(Hey I'm no good in algorithms just
// wanted the job done)
var (
dw int //day of the week
lm int //lenth of the month
dm int //day of the month
cm int //current month
)
weekd := [7]string{"mon", "Tues", "wed", "Thur", "Fri", "Sat", "Sun"}
sm := make(map[string]int) // standard length of a month
if time.Now().Year()%4 == 0 { // gosh! rember leap year
sm["February"] = 29
} else {
sm["February"] = 28
}
sm["January"] = 31
sm["March"] = 31
sm["April"] = 30
sm["May"] = 31
sm["June"] = 30
sm["July"] = 31
sm["August"] = 31
sm["September"] = 30
sm["October"] = 31
sm["November"] = 30
sm["Dcember"] = 31
sd := make(map[string]int) //standard day of the week
sd["Monday"] = 1
sd["Tuesday"] = 2
sd["Wednesday"] = 3
sd["Thursday"] = 4
sd["Friday"] = 5
sd["Saturday"] = 6
sd["Sunday"] = 7
//creaate a new date object set to current year,this time
//but on january
d := time.Date(time.Now().Year(), 1, 1, time.Now().Hour(), time.Now().Minute(), time.Now().Second(), time.Now().Nanosecond(), time.UTC)
//we are only interested in current month and the day of the week
dw = sd[d.Weekday().String()]
lm = sm[d.Month().String()]
for cm = 1; cm <= 12; cm++ {
fmt.Printf("Month--%s \n", d.Month().String())
for _, v := range weekd {
fmt.Printf(" %s", v)
}
fmt.Println()
for i := 1; i < dw; i++ {
fmt.Printf(" ")
}
for dm = 1; dm <= lm; dm++ {
if (dm+dw-1)%7 == 0 {
fmt.Printf(" %d\n", dm)
} else {
fmt.Printf(" %d", dm)
}
}
fmt.Println()
d = d.AddDate(0, 1, 0)
lm = sm[d.Month().String()]
dw = sd[d.Weekday().String()]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment