-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dates.scala
107 lines (97 loc) · 2.58 KB
/
Dates.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
object Dates {
abstract class Date
case class Day(day: [1 .. 31], month: [1 .. 12], year: Int) extends Date
case class Month(month: [1 .. 12], year: Int) extends Date
case class Year(year: Int) extends Date
def intToString(i: Int): String = {
if (i == 0) {
"0"
} else {
loopIntToString(i)
}
}
def loopIntToString(i: Int): String = {
if (i == 0) {
""
} else {
val q: Int = i / 10;
val r: [0 .. 9] = i - q*10;
loopIntToString(q) ++ basicIntToString(r)
}
}
def basicIntToString(i: [0 .. 9]): String = {
i match {
case 0 =>
"0"
case 1 =>
"1"
case 2 =>
"2"
case 3 =>
"3"
case 4 =>
"4"
case 5 =>
"5"
case 6 =>
"6"
case 7 =>
"7"
case 8 =>
"8"
case 9 =>
"9"
}
}
def printDate(dt: Date): Unit = {
dt match {
case Day(d, m, y) =>
Std.printString(intToString(d) ++ "/" ++ intToString(m) ++ "/" ++ intToString(y))
case Month(m, y) =>
Std.printString(intToString(m) ++ "/" ++ intToString(y))
case Year(y) =>
Std.printString(intToString(y))
}
}
def getMonth(m: [1 .. 12]): String = {
m match {
case 1 =>
"January"
case 2 =>
"February"
case 3 =>
"March"
case 4 =>
"April"
case 5 =>
"May"
case 6 =>
"June"
case 7 =>
"July"
case 8 =>
"August"
case 9 =>
"September"
case 10 =>
"October"
case 11 =>
"November"
case 12 =>
"December"
}
}
def prettyPrintDate(dt: Date): Unit = {
dt match {
case Day(d, m, y) =>
Std.printString(intToString(d) ++ " " ++ getMonth(m) ++ " " ++ intToString(y))
case Month(m, y) =>
Std.printString(getMonth(m) ++ " " ++ intToString(y))
case Year(y) =>
Std.printString(intToString(y))
}
}
val x: Date = Day(15,5,1998);
printDate(x);
prettyPrintDate(x)
}