comparison date.rhope @ 46:429b5f441381

Added Date Time object
author Mike Pavone <pavone@retrodev.com>
date Tue, 01 Dec 2009 03:59:31 -0500
parents
children
comparison
equal deleted inserted replaced
45:6420c35edb43 46:429b5f441381
1 //Note that the following code does not currently take into account leap seconds
2
3 Days From Secs[secs:days,secs left]
4 {
5 secs per day <- [[60]*[60]]*[24]
6 days <- [secs]/[secs per day]
7 secs left <- [secs]%[secs per day]
8 }
9
10 //This doesn't take into account the mod 100 rule
11 Year From Unix Days[days:year,day in year]
12 {
13 block days <- [[365]*[4]]+[1]
14 base year <- [1970]+[[[days]/[block days]]*[4]]
15 after base <- [days]%[block days]
16 If[[after base] > [365]]
17 {
18 year <- [[base year]+[1]]+[ [[after base]-[366]]/[365] ]
19 day in year <- [[after base]-[366]]%[365]
20 }{
21 year <- Val[base year]
22 day in year <- Val[after base]
23 }
24 }
25
26 Is Leap Year[year:is,is not]
27 {
28 is <-If[[[year]%[400]]=[0]] {}
29 {
30 ,is not <- If[[[year]%[100]]=[0]]
31 {
32 is,is not <- If[[[year]%[4]]=[0]]
33 }
34 }
35 }
36
37 _Month From Day[day,days,current:month,day in month]
38 {
39 curdays <- [days]Index[current]
40 If[[day]<[curdays]]
41 {
42 month <- current
43 day in month <- day
44 }{
45 month,day in month <- _Month From Day[[day]-[curdays], days, [current]+[1]]
46 }
47 }
48
49 Month From Day[day,year:month,day in month]
50 {
51 base <- (31,28,31,30,31,30,31,31,30,31,30,31)
52 Is Leap Year[year]
53 {
54 days <- [base]Set[1, 29]
55 }{
56 days <- Val[base]
57 }
58 month,day in month <- _Month From Day[day, days, 0]
59 }
60
61 Blueprint Date Time
62 {
63 Long:Fifty Micros
64 Word:Year
65 Byte:Month
66 Byte:Day
67 }
68
69 Date Time From Unix[unix:date]
70 {
71 ,sec in day <- Days From Secs[unix]
72 { year <- Year From Unix Days[~] {}
73 { month, day <- Month From Day[~, year] }}
74 date <- [[[[Build["Date Time"]]Fifty Micros <<[[sec in day]*[20000]]]Year <<[year]]Month <<[[month]+[1]]]Day <<[[day]+[1]]
75 }
76
77 Now[:date]
78 {
79 date <- Date Time From Unix[Unix Time[]]
80 }
81
82 Seconds@Date Time[date:out]
83 {
84 out <- [[[date]Fifty Micros >>]/[20000]]%[60]
85 }
86
87 Milliseconds@Date Time[date:out]
88 {
89 out <- [[[date]Fifty Micros >>]/[20]]%[60000]
90 }
91
92 Microseconds@Date Time[date:out]
93 {
94 out <- [[[date]Fifty Micros >>]%[20]]*[50]
95 }
96
97 Hours@Date Time[date:out]
98 {
99 out <- [[date]Fifty Micros >>]/[[[20000]*[60]]*[60]]
100 }
101
102 Minutes@Date Time[date:out]
103 {
104 out <- [[[date]Fifty Micros >>]/[[20000]*[60]]]%[60]
105 }
106
107 Day in Year@Date Time[date:out]
108 {
109 base day <- [(0,0,31,59,90,120,151,181,212,243,273,304,334)]Index[[date]Month >>]
110 ,noleap <- If[[[date]Month >>] > [2]]
111 {
112 ,noleap <- Is Leap Year[[date]Year >>]
113 {
114 out <- [base day] + [[date]Day >>]
115 }
116 }
117 Val[noleap]
118 {
119 out <- [[base day] + [[date]Day >>]]-[1]
120 }
121 }
122
123 //Gregorian only for the moment
124 //Sunday = 0, Saturday = 6
125 //Uses Zeller's algorithm
126 Day of Week@Date Time[date:out]
127 {
128 If[[[date]Month >>] < [3]]
129 {
130 zmonth <- [[date]Month >>]+[12]
131 zyear <- [[date]Year >>]-[1]
132 }{
133 zmonth <- [date]Month >>
134 zyear <- [date]Year >>
135 }
136 [zyear]Slice@String[2]
137 { century <- <String@Whole Number[~] }
138 { y <- <String@Whole Number[~] }
139
140 a <- [[26]* [[zmonth]+[1]]]/[10]
141 b <- [[5]*[y]]/[4]
142 c <- [century]/[4]
143 d <- [2]*[century]
144
145 out <- [[-[+[+[+[[date]Day >>, a], b], c], d]] + [6]] % [7]
146 }
147
148 Format@Date Time[date,format:out]
149 {
150 If[[format]=[""]]
151 {
152 out <- ""
153 }{
154 months <- ("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
155 days <- ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
156 cur,rest format <- [format]Slice[1]
157 If[[cur] = ["Y"]]
158 {
159 piece <- [date]Year >>
160 }{ If[[cur] = ["M"]]
161 {
162 piece <- [date]Month >>
163 }{ If[[cur] = ["N"]]
164 {
165 tpiece <- [months]Index[[date]Month >>]
166 }{ If[[cur] = ["B"]]
167 {
168 tpiece <- [[months]Index[[date]Month >>]]Slice[3]
169 }{ If[[cur] = ["D"]]
170 {
171 ppiece <- [date]Day >>
172 }{ If[[cur] = ["d"]]
173 {
174 piece <- [date]Day >>
175 }{ If[[cur] = ["n"]]
176 {
177 tpiece <- [days]Index[[date]Day of Week]
178 }{ If[[cur] = ["b"]]
179 {
180 tpiece <- [[days]Index[[date]Day of Week]]Slice[3]
181 }{ If[[cur] = ["w"]]
182 {
183 piece <- Day of Week[date]
184 }{ If[[cur] = ["h"]]
185 {
186 hour <- [date]Hours
187 If[[hour] > [12]]
188 {
189 ppiece <- [hour]-[12]
190 }{
191 If[[hour] = [0]]
192 { ppiece <- 12 }
193 { ppiece <- Val[hour] }
194 }
195 }{ If[[cur] = ["H"]]
196 {
197 ppiece <- Hours[date]
198 }{ If[[cur] = ["m"]]
199 {
200 ppiece <- Minutes[date]
201 }{ If[[cur] = ["s"]]
202 {
203 ppiece <- Seconds[date]
204 }{ If[[cur] = ["a"]]
205 {
206 If[[[date]Hours] < [12]]
207 { tpiece <- "AM" }
208 { tpiece <- "PM" }
209 }{ If[[cur] = ["t"]]
210 {
211 ppiece <- Milliseconds[date]
212 }{
213 tpiece <- Val[cur]
214 }}}}}}}}}}}}}}}
215
216 sppiece <- <Whole Number@String[ppiece]
217
218 If[[[sppiece]Length] < [2]]
219 {
220 tpiece <- ["0"]Append[sppiece]
221 }{
222 tpiece <- Val[sppiece]
223 }
224 tpiece <- <Whole Number@String[piece]
225 out <- [tpiece]Append[[date]Format[rest format]]
226
227 }
228 }
229
230 RFC 2822@Date Time[date:out]
231 {
232 out <- [date]Format["d B Y H:m:s -0000"]
233 }
234
235 //Preferred format for HTTP as specified by RFC 2616
236 RFC 2616@Date Time[date:out]
237 {
238 out <- [[date]Format["b, D B Y H:m:s"]]Append[" GMT"]
239 }
240
241