-
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
// 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 = c.DEFAULT_LATITUDE,
longitude: f64 = c.DEFAULT_LONGITUDE,
offset_hour: f64 = 0,
twilight_angle: f64 = c.TWILIGHT_ANGLE_DAYLIGHT,
target_time: ?c.time_t = null,
utc: bool = false,
debug: bool = false,
report_sunrise: c.OnOff = c.ONOFF_OFF,
report_sunset: c.OnOff = c.ONOFF_OFF,
list_days: c_uint = c.DEFAULT_LIST,
utc_bias_hours: f64 = 0,
command: CommandOptions = .poll,
pub const ParseArgsError = error{
UnknownArg,
};
pub const ListOptions = struct {
days: c_uint = 1,
pub fn parseArg(self: *@This(), arg: []const u8, args: *std.process.ArgIterator) ParseArgsError!void {
_ = self;
_ = arg;
_ = args;
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: void,
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 => return ParseArgsError.UnknownArg,
.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 |_| {}
if (std.mem.eql(u8, "--debug", arg)) {
self.debug = true;
return;
}
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 (parseSuffixedLatitude(arg)) |lat| {
self.latitude = lat;
return;
}
if (parseSuffixedLongitude(arg)) |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;
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"));
}
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;
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"));
}
pub fn toC(self: *const @This()) c.runStruct {
var now: c.time_t = undefined;
_ = c.time(&now);
var target_time: c.time_t = self.target_time orelse now;
return c.runStruct{
.latitude = self.latitude,
.longitude = self.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 = self.list_days,
.utcBiasHours = self.utc_bias_hours,
};
}