Calendar: Difference between revisions

no edit summary
No edit summary
Line 2,368:
30
NIL</pre>
 
=={{header|Clojure}}==
Java interop version.
Does not use any external dependencies to demonstrat the java interop.
Requires "join" from "clojure.string" .
Written by Kyuvi.
<lang clojure>require '[clojure.string :only [join] :refer [join]])
 
(def day-row "Su Mo Tu We Th Fr Sa")
 
(def col-width (count day-row))
 
(defn month-to-word
"Translate a month from 1 to 12 into its word representation"
[month]
((vec (.getMonths (new java.text.DateFormatSymbols))) month))
 
(defn month [date]
(.get date (java.util.Calendar/MONTH)))
 
(defn total-days-in-month [date]
(.getActualMaximum date (java.util.Calendar/DAY_OF_MONTH)))
 
(defn first-weekday [date]
(.get date (java.util.Calendar/DAY_OF_WEEK)))
 
(defn center-string
" Return a string that is WIDTH long with STRING centered in it "
[string width]
(let [pad (- width (count string))
lpad (quot pad 2)
rpad (- pad (quot pad 2))]
(if (<= pad 0)
string
(str (apply str (repeat lpad " ")) ; remove vector
string
(apply str (repeat rpad " "))))))
 
 
(defn calc-columns
"Calcutate the number of columns given the width in CHARACTERS and the
MARGIN SIZE"
[characters margin-size]
(loop [cols 0 excess characters ]
(if (>= excess (count day-row))
(recur (inc cols) (- excess (+ margin-size (count day-row))))
cols)))
 
(defn month-vector
"Returns a vector with the month name, day-row and days formatted for printing"
[date]
(vec (concat
(vector (center-string (month-to-word (month date)) col-width))
(vector day-row)
(map #(join " " %)
(partition 7
(concat
(repeat (dec (first-weekday date)) " ")
(map #(format "%2s" %)
(range 1 (inc (total-days-in-month date))))
(repeat (- 42 (total-days-in-month date)
(dec (first-weekday date)) ) " ")))))))
 
 
(defn year-vector
"Retuns a 2d vector of all the months in the year of DATE"
[date]
(loop [m [] c (month date)]
(if (= c 11 )
(conj m (month-vector date))
(recur (conj m (month-vector date))
(do (.add date (java.util.Calendar/MONTH ) 1)
(month date))))))
 
(defn print-months
"prints the months to standard output with NCOLS and MARGIN "
[ v ncols margin]
(doseq [r (range (Math/ceil (/ 12 ncols)))]
(do (doseq [i (range 8)]
(do (doseq [c (range (* r ncols) (* (+ r 1) ncols)) :while (< c 12)]
(printf (str (apply str (repeat margin " ")) "%s") (get-in v [c i])))
(println)))
(println))))
 
(defn print-cal
"(print-cal [year [width [margin]]])
Print out the calendar for a given YEAR with WIDTH characters wide
with MARGIN spaces between months."
([]
(print-cal 1969 80 2))
([year]
(print-cal year 80 2))
([year width]
(print-cal year width 2))
([year width margin]
(assert (>= width (count day-row)) "width should be more than 20")
(assert (>= margin 0) "margin needs to be more than 0")
(let [date (new java.util.GregorianCalendar year 0 1)
column-count (calc-columns width margin)
total-size (+ (* column-count (count day-row))
(* (dec column-count) margin))]
(println (center-string "[Snoopy Picture]" total-size))
(println (center-string (str year) total-size))
(println)
(print-months (year-vector date) column-count margin))))</lang>
 
 
 
<pre>user=> (print-cal)
[Snoopy Picture]
1969
 
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1
5 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 29
30 31
 
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30
 
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
31
 
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
 
</pre>
 
=={{header|D}}==
Anonymous user