-
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
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
// Copyright (C) 2025 Shota FUJI
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-only
const std = @import("std");
const config = @import("config");
const ListOptions = struct {
bin: []const u8,
utc: bool = false,
longitude: []const u8 = "31.132484E",
latitude: []const u8 = "29.977435N",
days: ?[]const u8 = "3",
tz: []const u8 = "UTC",
event: ?enum { sunset, sunrise } = null,
twilight: ?[]const u8 = null,
twilight_angle: ?[]const u8 = null,
offset: ?[]const u8 = null,
};
fn list(allocator: std.mem.Allocator, opts: ListOptions) !std.process.Child.RunResult {
var env = std.process.EnvMap.init(allocator);
defer env.deinit();
try env.put("TZ", opts.tz);
var args = std.ArrayList([]const u8).init(allocator);
defer args.deinit();
try args.append(opts.bin);
if (opts.utc) {
try args.append("utc");
}
if (opts.offset) |offset| {
try args.append("offset");
try args.append(offset);
}
try args.append("list");
if (opts.days) |days| {
try args.append(days);
}
try args.append(opts.latitude);
try args.append(opts.longitude);
if (opts.event) |e| {
try args.append(@tagName(e));
}
if (opts.twilight) |t| {
try args.append(t);
}
if (opts.twilight_angle) |angle| {
try args.append("angle");
try args.append(angle);
}
return try std.process.Child.run(.{
.allocator = allocator,
.argv = args.items,
.env_map = &env,
});
}
test {
const legacy = try list(std.testing.allocator, .{ .bin = config.legacy_bin });
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{ .bin = config.new_bin });
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "Should have same default DAYS" {
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.days = null,
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.days = null,
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "Should behave same on non-UTC timezone" {
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Asia/Tokyo",
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Asia/Tokyo",
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "Should behave same with UTC flag" {
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Europe/Paris",
.utc = true,
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Europe/Paris",
.utc = true,
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "Should behave same on printing only sunrise" {
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunrise,
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunrise,
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "Should behave same on printing only sunset" {
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "Should behave same even with twilight angle set" {
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
.twilight = "civil",
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
.twilight = "civil",
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "Should behave same on custom twilight angle" {
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
.twilight_angle = "2.1",
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
.twilight_angle = "2.1",
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "Should behave same with an offset" {
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
.offset = "00:30",
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
.offset = "00:30",
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "Should behave same with a negative offset" {
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
.offset = "-11:00",
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Europe/Paris",
.utc = true,
.event = .sunset,
.offset = "-11:00",
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "From original USAGE.txt: Example 2" {
// List civil sunrise and sunset times for today and next 6 days. Moscow.
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Europe/Moscow",
.twilight = "civil",
.days = "7",
.latitude = "55.752163N",
.longitude = "37.617524E",
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Europe/Moscow",
.twilight = "civil",
.days = "7",
.latitude = "55.752163N",
.longitude = "37.617524E",
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}
test "From original USAGE.txt: Example 4" {
// List next 7 days sunrise times, custom +3 degree twilight angle, default location.
// Uses GMT; as any change in daylight saving over the specified period is not considered.
const legacy = try list(std.testing.allocator, .{
.bin = config.legacy_bin,
.tz = "Asia/Tokyo",
.twilight_angle = "3",
.days = "7",
.event = .sunrise,
.utc = true,
});
defer std.testing.allocator.free(legacy.stderr);
defer std.testing.allocator.free(legacy.stdout);
const new = try list(std.testing.allocator, .{
.bin = config.new_bin,
.tz = "Asia/Tokyo",
.twilight_angle = "3",
.days = "7",
.event = .sunrise,
.utc = true,
});
defer std.testing.allocator.free(new.stderr);
defer std.testing.allocator.free(new.stdout);
try std.testing.expectEqual(legacy.term.Exited, new.term.Exited);
try std.testing.expectEqualStrings(legacy.stderr, new.stderr);
try std.testing.expectEqualStrings(legacy.stdout, new.stdout);
}