Initial revision
[citadel.git] / wincit / unixtime.bas
1 Option Explicit
2 DefInt A-Z
3
4 Global dayo(13) As Integer
5 Global mnth(12) As String
6 Const FordConstant = 18032'/* 5 hours 32 seconds, 5 hours is GMT, 32 seconds, I have no idea. */
7
8 Sub humantime (ByVal l&, mo%, da%, yr%, h%, m%, s%)
9   ' here art, pass it l  the unix time number, and it'll return the others
10     Dim lm&, lh&, ld&, ll&
11     Dim leap%
12     Dim Craig%
13
14     l = l - FordConstant ' account for GMT
15     lm = Int(l / 60)
16     s = (l - lm * 60)
17     l = Int(l / 60)
18
19     lh = Int(l / 60) ' was lm
20     m = (l - lh * 60)
21     l = Int(l / 60)
22
23     ll = Int(l / 24)
24     h = (l - ll * 24)
25     l = Int(l / 24)
26
27     lm = Int((l + 365) / 365)
28     lm = Int(lm / 4)'; /* leap days */
29
30     '/* l is now whole days left */
31     yr = Int((l - lm) / 365)
32     yr = yr + 1970
33
34     If (yr Mod 4) = 0 Then
35       leap = 1
36     Else
37       leap = 0
38     End If
39
40     ld = Int((l - lm) / 365)
41     da = ((l - ld * 365) - lm) + 1
42
43     '/* da is days in this year */
44
45     For lm = 1 To 12 ' was 0
46       If (leap And lm > 1) Then
47         Craig = dayo(lm) + 1
48       Else
49         Craig = dayo(lm)
50       End If
51       If (da <= Craig) Then
52           ld = dayo(lm)
53           mo = lm
54           Exit For
55       End If
56     Next lm
57
58     If (mo > 1) Then
59       If (leap) And (mo > 2) Then
60         da = da - (dayo(mo - 1) + 1)
61       Else
62         da = da - dayo(mo - 1)
63       End If
64     End If
65
66 End Sub
67
68 Sub setvars ()
69   
70   'setup for unixcalc
71   dayo(0) = 0
72   dayo(1) = 31
73   dayo(2) = 59
74   dayo(3) = 90
75   dayo(4) = 120
76   dayo(5) = 151
77   dayo(6) = 181
78   dayo(7) = 212
79   dayo(8) = 243
80   dayo(9) = 273
81   dayo(10) = 304
82   dayo(11) = 334
83   dayo(12) = 365
84
85   mnth(1) = "Jan"
86   mnth(2) = "Feb"
87   mnth(3) = "Mar"
88   mnth(4) = "Apr"
89   mnth(5) = "May"
90   mnth(6) = "Jun"
91   mnth(7) = "Jul"
92   mnth(8) = "Aug"
93   mnth(9) = "Sep"
94   mnth(10) = "Oct"
95   mnth(11) = "Nov"
96   mnth(12) = "Dec"
97   
98 End Sub
99
100 Function strtime (t As Long) As String
101   'given a unixtime make a good string out of it
102   Dim s As String
103   Dim xmo%
104   Dim xda%
105   Dim xyr%
106   Dim xh%
107   Dim xm%
108   Dim xs%
109   Dim ap As String
110   Dim tm$
111   
112   Call humantime(t, xmo, xda, xyr, xh, xm, xs)
113   s = mnth(xmo) + " " + Trim$(Str$(xda)) + ", " + Trim$(Str$(xyr))
114   If xh >= 12 Then
115     ap = "pm"
116     xh = xh - 12
117   Else
118     ap = "am"
119   End If
120   If xh = 0 Then xh = 12
121   tm$ = Trim$(Str$(xm))
122   If (xm < 10) Then tm$ = "0" + tm$
123   s = s + " " + Trim$(Str$(xh)) + ":" + tm$ + ap
124   strtime = s
125 End Function
126
127 Function unixtime (mo%, da%, yr%, hr%, min%, sec%) As Long
128   'art, this is the other way, pass it month, day year ..., and it
129   'returns the long int
130
131   Dim yr2%, leap%
132   Dim ret As Long, first As Long, retl As Long
133
134   yr2 = yr - 1970
135   '/* leap year is divisible by four except every 4 hunred years, don't worry about it
136   '   we'll just die in year 2000, it happens to be divisible by 400 */
137   leap = 0
138   If (yr Mod 4 = 0) Then
139       leap = 1'; /* leap a bit wrong 12/31/88 to 1/1/89 */
140   End If
141
142   first = yr2 * 365
143   first = first + ((yr2 + 1) \ 4)
144
145   first = first + dayo(mo - 1)' /* add up days in this year so far */
146   If ((leap = 1) And (mo > 2)) Then
147       first = first + 1
148   End If
149   
150   first = first + da'; /* add the days in this month */
151   first = first - 1';/* don't count today's seconds */
152
153   '/* first is number of days go to seconds */
154   ret = (60 * 60)
155   ret = ret * 24
156   ret = ret * first'; /* number of seconds from 1970 to last nite */
157   retl = 60 * 60
158   retl = retl * hr
159   ret = ret + retl
160   ret = ret + (60 * min)
161   ret = ret + sec
162   ret = ret + FordConstant'; /* I have no idea */
163   unixtime = ret
164 End Function
165