-
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
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
// 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 c = @cImport({
@cInclude("time.h");
@cInclude("sunriset.h");
});
latitude: ?f64 = null,
longitude: ?f64 = null,
offset_hour: f64 = 0,
twilight_angle: f64 = c.TWILIGHT_ANGLE_DAYLIGHT,
utc: bool = false,
debug: bool = false,
report_sunrise: c.OnOff = c.ONOFF_ON,
report_sunset: c.OnOff = c.ONOFF_ON,
utc_bias_hours: f64 = 0,
command: CommandOptions = .poll,
pub const ParseArgsError = error{
UnknownArg,
MissingValue,
InvalidLatitudeFormat,
InvalidLongitudeFormat,
InvalidDayOfMonth,
InvalidMonth,
InvalidYearSince2000,
};
pub const ReportOptions = struct {
day_of_month: ?u5 = null,
month: ?u4 = null,
year_since_2000: ?c_int = null,
pub fn parseArg(self: *@This(), arg: []const u8, args: *std.process.ArgIterator) ParseArgsError!void {
if (std.mem.eql(u8, "d", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
const d = std.fmt.parseUnsigned(u5, next, 10) catch {
std.log.err("Value of {s} option must be unsigned integer between 1 and 31", .{arg});
return ParseArgsError.InvalidDayOfMonth;
};
if (d == 0 or d > 31) {
std.log.err("Value of {s} option must be between 1 and 31", .{arg});
return ParseArgsError.InvalidDayOfMonth;
}
self.day_of_month = d;
return;
}
if (std.mem.eql(u8, "m", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
const m = std.fmt.parseUnsigned(u4, next, 10) catch {
std.log.err("Value of {s} option must be unsigned integer between 1 and 12", .{arg});
return ParseArgsError.InvalidMonth;
};
if (m == 0 or m > 12) {
std.log.err("Value of {s} option must be between 1 and 12", .{arg});
return ParseArgsError.InvalidMonth;
}
self.month = m;
return;
}
if (std.mem.eql(u8, "y", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
const y = std.fmt.parseUnsigned(c_int, next, 10) catch {
std.log.err("Value of {s} option must be integer", .{arg});
return ParseArgsError.InvalidYearSince2000;
};
// Although the original program accepts 0 for year, it will crash because
// some place produce NaN (division? idk) and attempt int cast (`(int) NaN`).
if (y == 0) {
std.log.err("Value of {s} option must be greater than 0", .{arg});
return ParseArgsError.InvalidYearSince2000;
}
self.year_since_2000 = y;
return;
}
return ParseArgsError.UnknownArg;
}
};
pub const ListOptions = struct {
days: c_uint = c.DEFAULT_LIST,
pub fn parseArg(self: *@This(), arg: []const u8, _: *std.process.ArgIterator) ParseArgsError!void {
if (std.fmt.parseUnsigned(c_uint, arg, 10)) |days| {
self.days = days;
return;
} else |_| {}
return ParseArgsError.UnknownArg;
}
};
pub const Command = enum {
help,
version,
poll,
report,
wait,
list,
pub const ParseError = error{
UnknownCommand,
};
pub fn parse(str: []const u8) ParseError!@This() {
inline for (@typeInfo(@This()).@"enum".fields) |field| {
if (std.mem.eql(u8, field.name, str)) {
return @enumFromInt(field.value);
}
}
return ParseError.UnknownCommand;
}
};
pub const CommandOptions = union(Command) {
help: void,
version: void,
poll: void,
report: ReportOptions,
wait: void,
list: ListOptions,
pub fn parseArg(self: *@This(), arg: []const u8, args: *std.process.ArgIterator) ParseArgsError!void {
switch (self.*) {
.help => return ParseArgsError.UnknownArg,
.version => return ParseArgsError.UnknownArg,
.poll => return ParseArgsError.UnknownArg,
.report => try self.report.parseArg(arg, args),
.wait => return ParseArgsError.UnknownArg,
.list => try self.list.parseArg(arg, args),
}
}
};
const ParseState = enum {
no_command,
with_command,
};
pub fn parseArgs(self: *@This(), args: *std.process.ArgIterator) ParseArgsError!void {
state: switch (ParseState.no_command) {
.no_command => {
while (args.next()) |arg| {
if (Command.parse(arg)) |command| {
switch (command) {
.version => {
self.command = .version;
return;
},
.help => {
self.command = .help;
return;
},
.poll => {
self.command = .poll;
},
.report => {
self.command = .{ .report = .{} };
},
.wait => {
self.command = .wait;
},
.list => {
self.command = .{ .list = .{} };
},
}
continue :state .with_command;
} else |_| {}
self.parseArg(arg, args) catch |err| {
std.log.err("Unknown argument: {s}", .{arg});
return err;
};
}
},
.with_command => {
while (args.next()) |arg| {
self.parseArg(arg, args) catch |err| {
std.log.err("Unknown argument: {s}", .{arg});
return err;
};
}
},
}
}
fn parseArg(self: *@This(), arg: []const u8, args: *std.process.ArgIterator) ParseArgsError!void {
if (std.mem.eql(u8, "-v", arg) or std.mem.eql(u8, "--version", arg)) {
self.command = .version;
return;
}
if (std.mem.eql(u8, "-h", arg) or std.mem.eql(u8, "--help", arg)) {
self.command = .help;
return;
}
if (self.command.parseArg(arg, args)) |_| {
return;
} else |err| switch (err) {
ParseArgsError.UnknownArg => {},
else => return err,
}
if (std.mem.eql(u8, "--debug", arg)) {
self.debug = true;
return;
}
if (std.mem.eql(u8, "--utc", arg)) {
self.utc = true;
return;
}
// For compatibility.
// TODO: Delete this once migration is complete.
if (std.mem.eql(u8, "utc", arg)) {
self.utc = true;
return;
}
if (std.mem.eql(u8, "--gmt", arg)) {
std.log.warn("--gmt is deprecated. Use --utc instead.", .{});
self.utc = true;
return;
}
if (std.mem.eql(u8, "--lat", arg) or std.mem.eql(u8, "--latitude", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
const lat = parseSuffixedLatitude(next) orelse std.fmt.parseFloat(f64, next) catch {
return ParseArgsError.InvalidLatitudeFormat;
};
if (self.latitude) |prev| {
std.log.warn("Got more than one latitude, overwriting {d} with {d}", .{ prev, lat });
}
self.latitude = lat;
return;
}
if (std.mem.eql(u8, "--lon", arg) or std.mem.eql(u8, "--longitude", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
const lon = parseSuffixedLongitude(next) orelse std.fmt.parseFloat(f64, next) catch {
return ParseArgsError.InvalidLongitudeFormat;
};
if (self.longitude) |prev| {
std.log.warn("Got more than one latitude, overwriting {d} with {d}", .{ prev, lon });
}
self.longitude = lon;
return;
}
if (parseSuffixedLatitude(arg)) |lat| {
if (self.latitude) |prev| {
std.log.warn("Got more than one latitude, overwriting {d} with {d}", .{ prev, lat });
}
self.latitude = lat;
return;
}
if (parseSuffixedLongitude(arg)) |lon| {
if (self.longitude) |prev| {
std.log.warn("Got more than one longitude, overwriting {d} with {d}", .{ prev, lon });
}
self.longitude = lon;
return;
}
return ParseArgsError.UnknownArg;
}
fn parseSuffixedLatitude(x: []const u8) ?f64 {
if (x.len <= 1) {
return null;
}
const lastChar = x[x.len - 1];
const value = std.fmt.parseFloat(f64, x[0 .. x.len - 1]) catch return null;
if (value < 0) {
return null;
}
return switch (lastChar) {
'N' => value,
'S' => -value,
else => null,
};
}
test parseSuffixedLatitude {
try std.testing.expectEqual(29.977435, parseSuffixedLatitude("29.977435N"));
try std.testing.expectEqual(-29.977435, parseSuffixedLatitude("29.977435S"));
try std.testing.expectEqual(null, parseSuffixedLatitude("29.977435W"));
try std.testing.expectEqual(null, parseSuffixedLatitude("-29.977435N"));
}
fn parseSuffixedLongitude(x: []const u8) ?f64 {
if (x.len <= 1) {
return null;
}
const lastChar = x[x.len - 1];
const value = std.fmt.parseFloat(f64, x[0 .. x.len - 1]) catch return null;
if (value < 0) {
return null;
}
return switch (lastChar) {
'E' => value,
'W' => -value,
else => null,
};
}
test parseSuffixedLongitude {
try std.testing.expectEqual(31.132484, parseSuffixedLongitude("31.132484E"));
try std.testing.expectEqual(-31.132484, parseSuffixedLongitude("31.132484W"));
try std.testing.expectEqual(null, parseSuffixedLongitude("31.132484N"));
try std.testing.expectEqual(null, parseSuffixedLongitude("-31.132484E"));
}
const GetTargetDayOptions = struct {
is_utc: bool = false,
day_of_month: ?c_int = null,
month: ?c_int = null,
year_since_2000: ?c_int = null,
};
fn getTargetDay(time: c.time_t, opts: GetTargetDayOptions) c.time_t {
var tm: c.tm = undefined;
if (opts.is_utc) {
_ = c.gmtime_r(&time, &tm);
} else {
c.tzset();
_ = c.localtime_r(&time, &tm);
}
if (opts.year_since_2000) |y| {
// tm_year ... Year since 1900.
tm.tm_year = y + 100;
}
if (opts.month) |m| {
// tm_mon ... An index of English month notation, not regular month. [0, 11]
tm.tm_mon = m - 1;
}
if (opts.day_of_month) |d| {
tm.tm_mday = d;
}
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
return c.timegm(&tm);
}
pub fn toC(self: *const @This()) c.runStruct {
var now: c.time_t = undefined;
_ = c.time(&now);
var target_time: c.time_t = switch (self.command) {
.report => |opts| getTargetDay(now, .{
.is_utc = self.utc,
.year_since_2000 = opts.year_since_2000,
.month = if (opts.month) |m| @intCast(m) else null,
.day_of_month = if (opts.day_of_month) |d| @intCast(d) else null,
}),
else => getTargetDay(now, .{ .is_utc = self.utc }),
};
return c.runStruct{
.latitude = self.latitude orelse c.DEFAULT_LATITUDE,
.longitude = self.longitude orelse c.DEFAULT_LONGITUDE,
.offsetHour = self.offset_hour,
.twilightAngle = self.twilight_angle,
.nowTimet = now,
.targetTimet = target_time,
.now2000 = c.daysSince2000(&now),
.target2000 = c.daysSince2000(&target_time),
.functionVersion = c.ONOFF_OFF,
.functionUsage = c.ONOFF_OFF,
.functionReport = c.ONOFF_OFF,
.functionList = c.ONOFF_OFF,
.functionPoll = c.ONOFF_OFF,
.functionWait = c.ONOFF_OFF,
.utc = if (self.utc) c.ONOFF_ON else c.ONOFF_OFF,
.debug = if (self.debug) c.ONOFF_ON else c.ONOFF_OFF,
.reportSunrise = self.report_sunrise,
.reportSunset = self.report_sunset,
.listDays = switch (self.command) {
.list => |opts| opts.days,
else => c.DEFAULT_LIST,
},
.utcBiasHours = self.utc_bias_hours,
};
}