-
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
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
-
584
-
585
-
586
-
587
-
588
-
589
-
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
-
609
-
610
-
611
-
612
-
613
-
614
-
615
-
616
-
617
-
618
-
619
-
620
-
621
-
622
-
623
-
624
-
625
-
626
-
627
-
628
-
629
-
630
-
631
-
632
-
633
-
634
-
635
-
636
-
637
-
638
-
639
-
640
-
641
-
642
-
643
-
644
-
645
-
646
-
647
-
648
-
649
-
650
-
651
-
652
-
653
-
654
-
655
-
656
-
657
-
658
-
659
-
660
-
661
-
662
-
663
-
664
-
665
-
666
-
667
-
668
-
669
-
670
-
671
-
672
-
673
-
674
-
675
-
676
-
677
-
678
-
679
-
680
-
681
-
682
-
683
-
684
-
685
-
686
-
687
-
688
-
689
-
690
-
691
-
692
-
693
-
694
-
695
-
696
-
697
-
698
-
699
-
700
-
701
-
702
-
703
-
704
-
705
-
706
-
707
-
708
-
709
-
710
-
711
-
712
-
713
-
714
-
715
-
716
-
717
// 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 ArgIterator = @import("./RunOptions/ArgIterator.zig");
const datetime = @import("./RunOptions/datetime.zig");
const EventType = @import("./RunOptions/event.zig").EventType;
const ParseArgsError = @import("./RunOptions/parser.zig").ParseArgsError;
const TwilightAngle = @import("./RunOptions/twilight.zig").TwilightAngle;
pub const ProcessArgIterator = @import("./RunOptions/ProcessArgIterator.zig");
const c = @cImport({
@cInclude("time.h");
@cInclude("sunriset.h");
});
latitude: ?f64 = null,
longitude: ?f64 = null,
offset_mins: i32 = 0,
twilight_angle: ?TwilightAngle = null,
utc: bool = false,
debug: bool = false,
command: CommandOptions = .{ .poll = .{} },
const PollOptions = struct {
at: ?datetime.Datetime = null,
pub fn parseArg(self: *@This(), arg: []const u8, args: ArgIterator) ParseArgsError!void {
if (std.mem.eql(u8, "--at", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
const at = datetime.Datetime.fromString(next) catch |err| {
std.log.err("\"{s}\" is not a valid datetime string: {s}", .{ next, @errorName(err) });
return ParseArgsError.InvalidDatetimeFormat;
};
self.at = at;
return;
}
return ParseArgsError.UnknownArg;
}
};
pub const ReportOptions = struct {
/// Current time should not affect report generation. However, as C function prints
/// the current time, this option is necessary for snapshot testing. Will be removed
/// once the report generation was replaced with Zig impelementation.
now: ?datetime.Datetime = null,
day_of_month: ?u5 = null,
month: ?u4 = null,
year_since_2000: ?c_int = null,
pub fn parseArg(self: *@This(), arg: []const u8, args: ArgIterator) ParseArgsError!void {
if (std.mem.eql(u8, "--date", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
const date = datetime.CalendarDate.fromString(next) catch |err| {
std.log.err("\"{s}\" is not a valid date string: {s}", .{ next, @errorName(err) });
return ParseArgsError.InvalidDateFormat;
};
self.year_since_2000 = @as(c_int, date.year) - 2000;
self.month = date.month;
self.day_of_month = date.day;
return;
}
if (datetime.CalendarDate.fromString(arg)) |date| {
self.year_since_2000 = @as(c_int, date.year) - 2000;
self.month = date.month;
self.day_of_month = date.day;
return;
} else |_| {}
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;
}
if (std.mem.eql(u8, "--now", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
const now = datetime.Datetime.fromString(next) catch |err| {
std.log.err("\"{s}\" is not a valid datetime string: {s}", .{ next, @errorName(err) });
return ParseArgsError.InvalidDatetimeFormat;
};
self.now = now;
return;
}
return ParseArgsError.UnknownArg;
}
};
pub const WaitOptions = struct {
event_type: ?EventType = null,
pub fn parseArg(self: *@This(), arg: []const u8, args: ArgIterator) ParseArgsError!void {
self.event_type = try EventType.parseArg(self.event_type, arg, args);
}
};
pub const ListOptions = struct {
event_type: ?EventType = null,
days: c_uint = c.DEFAULT_LIST,
from: ?datetime.CalendarDate = null,
pub fn parseArg(self: *@This(), arg: []const u8, args: ArgIterator) ParseArgsError!void {
if (EventType.parseArg(self.event_type, arg, args)) |e| {
self.event_type = e;
return;
} else |err| switch (err) {
ParseArgsError.UnknownArg => {},
else => return err,
}
if (std.mem.eql(u8, "--from", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
self.from = datetime.CalendarDate.fromString(next) catch |err| {
std.log.err("\"{s}\" is not a valid date string: {s}", .{ next, @errorName(err) });
return ParseArgsError.InvalidDateFormat;
};
return;
}
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: PollOptions,
report: ReportOptions,
wait: WaitOptions,
list: ListOptions,
pub fn parseArg(self: *@This(), arg: []const u8, args: ArgIterator) ParseArgsError!void {
switch (self.*) {
.help => return ParseArgsError.UnknownArg,
.version => return ParseArgsError.UnknownArg,
.poll => try self.poll.parseArg(arg, args),
.report => try self.report.parseArg(arg, args),
.wait => try self.wait.parseArg(arg, args),
.list => try self.list.parseArg(arg, args),
}
}
};
const ParseState = enum {
no_command,
with_command,
};
pub fn parseArgs(self: *@This(), args: 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| switch (err) {
ParseArgsError.UnknownArg => {
std.log.err("Unknown argument: {s}", .{arg});
return err;
},
else => return err,
};
}
},
.with_command => {
while (args.next()) |arg| {
self.parseArg(arg, args) catch |err| switch (err) {
ParseArgsError.UnknownArg => {
std.log.err("Unknown argument: {s}", .{arg});
return err;
},
else => return err,
};
}
},
}
}
fn parseArg(self: *@This(), arg: []const u8, args: 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 (TwilightAngle.parseArg(arg, args)) |angle| {
if (self.twilight_angle) |prev| {
std.log.warn("Overwriting twilight angle \"{s}\" with \"{s}\"", .{
@tagName(prev),
@tagName(angle),
});
}
self.twilight_angle = angle;
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;
}
// Hyphen-less one is for compatibility.
// TODO: Delete the hyphen-less one once migration is completed.
if (std.mem.eql(u8, "-o", arg) or std.mem.eql(u8, "--offset", arg) or std.mem.eql(u8, "offset", arg)) {
const next = args.next() orelse {
std.log.err("{s} option requires a value", .{arg});
return ParseArgsError.MissingValue;
};
const i: u1, const modifier: i2 = sign: {
break :sign if (std.mem.startsWith(u8, next, "-")) .{
1, -1,
} else .{
0, 1,
};
};
if (std.mem.indexOfScalarPos(u8, next, i, ':')) |colon_pos| {
// HH:MM
const hrs = std.fmt.parseUnsigned(u7, next[i..colon_pos], 10) catch {
std.log.err("Hour part of {s} option must be valid integer between 0 and 99", .{arg});
return ParseArgsError.InvalidOffset;
};
if (hrs > 99) {
std.log.err("Hour part of {s} option must be between 0 and 99", .{arg});
return ParseArgsError.InvalidOffset;
}
if (next[i..].len == colon_pos + 1) {
std.log.err("Value of {s} option cannot end with colon", .{arg});
return ParseArgsError.InvalidOffset;
}
const mins = std.fmt.parseUnsigned(u6, next[colon_pos + 1 ..], 10) catch {
std.log.err("Minute part of {s} option must be valid integer between 0 and 59", .{arg});
return ParseArgsError.InvalidOffset;
};
if (mins > 59) {
std.log.err("Minute part of {s} option must be between 0 and 59", .{arg});
return ParseArgsError.InvalidOffset;
}
self.offset_mins = @as(i32, (@as(i32, hrs) * 60) + mins) * modifier;
return;
} else {
// MM
const parsed = std.fmt.parseUnsigned(u7, next[i..], 10) catch {
std.log.err("Value of {s} option must be valid integer between -99 and 99", .{arg});
return ParseArgsError.InvalidOffset;
};
if (parsed > 99) {
std.log.err("Value of {s} option must be between -99 and 99", .{arg});
return ParseArgsError.InvalidOffset;
}
self.offset_mins = @as(i32, parsed) * modifier;
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 {
const now: c.time_t = now: switch (self.command) {
.poll => |opts| {
if (opts.at) |at| {
var tm: c.tm = .{
.tm_year = at.date.year - 1900,
.tm_mon = at.date.month - 1,
.tm_mday = at.date.day,
.tm_hour = at.time.hour,
.tm_min = at.time.minute,
.tm_sec = at.time.second,
};
var now = c.mktime(&tm);
if (at.offset) |offset| {
now -= tm.tm_gmtoff;
now += @as(c_long, if (offset.positive) 1 else -1) *
(@as(c_long, offset.hour) * 60 + offset.minute) *
std.time.s_per_min;
} else if (self.utc) {
now -= tm.tm_gmtoff;
}
break :now now;
}
break :now c.time(null);
},
// This block contains totally duplicated code of the above,
// because `now` option will be removed soon.
.report => |opts| {
if (opts.now) |now| {
var tm: c.tm = .{
.tm_year = now.date.year - 1900,
.tm_mon = now.date.month - 1,
.tm_mday = now.date.day,
.tm_hour = now.time.hour,
.tm_min = now.time.minute,
.tm_sec = now.time.second,
};
var t = c.mktime(&tm);
if (now.offset) |offset| {
t -= tm.tm_gmtoff;
t += @as(c_long, if (offset.positive) 1 else -1) *
(@as(c_long, offset.hour) * 60 + offset.minute) *
std.time.s_per_min;
} else if (self.utc) {
t -= tm.tm_gmtoff;
}
break :now t;
}
break :now c.time(null);
},
.list => |opts| {
if (opts.from) |from| {
var tm: c.tm = .{
.tm_year = from.year - 1900,
.tm_mon = from.month - 1,
.tm_mday = from.day,
};
break :now if (self.utc) c.timegm(&tm) else c.timelocal(&tm);
}
break :now c.time(null);
},
else => {
break :now c.time(null);
},
};
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 }),
};
const event_type: EventType = switch (self.command) {
.list => |opts| opts.event_type orelse .both,
.wait => |opts| opts.event_type orelse .both,
else => .both,
};
const twilight_angle = self.twilight_angle orelse .daylight;
// This effectful function updates `c.timezone`.
c.tzset();
return c.runStruct{
.latitude = self.latitude orelse c.DEFAULT_LATITUDE,
.longitude = self.longitude orelse c.DEFAULT_LONGITUDE,
.offsetHour = @as(f64, @floatFromInt(self.offset_mins)) / 60.0,
.twilightAngle = twilight_angle.toFloat(),
.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 = if (event_type == .sunset) c.ONOFF_OFF else c.ONOFF_ON,
.reportSunset = if (event_type == .sunrise) c.ONOFF_OFF else c.ONOFF_ON,
.listDays = switch (self.command) {
.list => |opts| opts.days,
else => c.DEFAULT_LIST,
},
.utcBiasHours = @as(f64, @floatFromInt(-c.timezone)) / 60.0 / 60.0,
};
}
test {
_ = @import("RunOptions/datetime.zig");
_ = @import("RunOptions/TestArgIterator.zig");
}