File: Time.k

package info (click to toggle)
kaya 0.2.0-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,012 kB
  • ctags: 1,307
  • sloc: cpp: 6,691; haskell: 4,833; sh: 2,868; yacc: 768; makefile: 700; perl: 87
file content (150 lines) | stat: -rw-r--r-- 3,548 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/** -*-C-*-ish
    Kaya standard library
    Copyright (C) 2004, 2005 Edwin Brady

    This file is distributed under the terms of the GNU Lesser General
    Public Licence. See COPYING for licence.
*/

module Time;

import Prelude;
import Builtins;
import Strings;

%include "time.h";

"Months of the year."
public data Month = Jan | Feb | Mar | Apr | May | Jun 
                  | Jul | Aug | Sep | Oct | Nov | Dec;

"Days of the week."
public data Day = Sunday | Monday | Tuesday | Wednesday 
                | Thursday | Friday | Saturday;

"Representation of time."
public data Time = Time(Int second, 
			Int minute, 
			Int hour,
			Int mday,
			Month mon,
			Int year,
			Day wday,
			Int yday,
			Bool dst);

"Invalid month number"
public Exception NoSuchMonth(Int m) = Exception("No such month number "+String(m),190);

foreign "stdfuns.o" {
    "Get the current time.
     Returns seconds since Jan 1st 1970."
    public Int time() = gettime;
    "Returns the number of microseconds so far this second"
    public Int microtime() = dogettimeofday;
    Time dogmtime(Int secs) = dogmtime;
    Time dolocaltime(Int secs) = dolocaltime;
    Int domktime(Time t) = domktime;
}

"Convert a time to GMT.
Defaults to current time."
public Time gmtime(Int secs = time()) = dogmtime(secs);
"Convert a time to local time.
Defaults to current time."
public Time localtime(Int secs = time()) = dolocaltime(secs);

"Return a given Time as seconds since the epoch."
public Int mktime(Time t) = domktime(t);

"Add some days (positive or negative) to a date."
public Time addDays(Time t, Int days) = gmtime(mktime(t)+(24*60*60*days));

"Convert a month to a String"
public String monthString(Month m) {
    return case m of {
	Jan -> "January";
	| Feb -> "February";
	| Mar -> "March";
	| Apr -> "April";
	| May -> "May";
	| Jun -> "June";
	| Jul -> "July";
	| Aug -> "August";
	| Sep -> "September";
	| Oct -> "October";
	| Nov -> "November";
	| Dec -> "December";
    };
}

"Convert a month to an Int"
public Int monthInt(Month m) {
    return case m of {
	Jan -> 1;
	| Feb -> 2;
	| Mar -> 3;
	| Apr -> 4;
	| May -> 5;
	| Jun -> 6;
	| Jul -> 7;
	| Aug -> 8;
	| Sep -> 9;
	| Oct -> 10;
	| Nov -> 11;
	| Dec -> 12;
    };
}

"Convert an Int to a month"
public Month intMonth(Int m) {
    if (m==1) return Jan;
    if (m==2) return Feb;
    if (m==3) return Mar;
    if (m==4) return Apr;
    if (m==5) return May;
    if (m==6) return Jun;
    if (m==7) return Jul;
    if (m==8) return Aug;
    if (m==9) return Sep;
    if (m==10) return Oct;
    if (m==11) return Nov;
    if (m==12) return Dec;
    throw(NoSuchMonth(m));
}

"Convert a day to a String"
public String dayString(Day d) {
    return case d of {
	Sunday -> "Sunday";
	| Monday -> "Monday";
	| Tuesday -> "Tuesday";
	| Wednesday -> "Wednesday";
	| Thursday -> "Thursday";
	| Friday -> "Friday";
	| Saturday -> "Saturday";
    };
}

"Return a time formatted according to RFC 2822 (e.g. for email)."
public String rfc2822Time(Time t, String tz="+0000") {
  ts = substr(dayString(t.wday),0,3)+", ";
  if (t.mday < 10) {
    ts += " "+t.mday;
  } else {
    ts += String(t.mday);
  }
  ts += " "+substr(monthString(t.mon),0,3)+" "+t.year+" "+twodigits(t.hour)+":"+twodigits(t.minute)+":"+twodigits(t.second)+" "+tz;
  return ts;
}

String twodigits(Int i) {
  if (i > 9) { return String(i); }
  return "0"+i;
}

"Return a time formatted for MySQL database insertion."
public String mySQLTime(Time t) {
  return t.year+"-"+monthInt(t.mon)+"-"+t.mday+" "+t.hour+":"+t.minute+":"+t.second;
}