This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtfstool
executable file
·6351 lines (5156 loc) · 194 KB
/
gtfstool
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -W
# gtfstool - handle gtfs feeds
# This file is part of Tripover, a broad-search journey planner.
# Copyright (C) 2014-2017 Joris van der Geer.
# primary use is to import gtfs feeds into tripover format.
# see usage below
# basic provisions for feed merge : add agencyID to all IDs, have timezone per agency in routes, trips and calendar
# basic filtering on transport mode
use 5.012;
use strict;
use utf8;
use POSIX ();
# perl -MO=Xref,-ounused.out
my $version_maj = 1;
my $version_min = '0.2';
my $lastchanged = " 16 May 2017";
my $copyright = "Copyright (C) 2014-2017 Joris van der Geer";
my $verbose = 0;
my $dryrun = 0;
my $testonly = 0;
my $conditional = 1;
my $Conditional = 1;
my $anaout = 0;
my $anadir = '.';
my $canonout = 0;
my $canondir = '.';
my $warningcnt = 0;
my $errorcnt = 0;
my $verbose_hops = 1;
my $writerefs = 1;
my $idprefix = '';
my $showinfer = 1;
my $showmerge = 2;
my $show_unconnect = 0;
my $use_stopseqs = 1;
my $do_patch = 1;
my $airport_ref = 0;
my $airport_use = 0;
my $tzmin = 0;
my $mergedir = '';
my $runto = '';
my $allreserved = 0;
my $include_tram = 1;
my $include_metro = 1;
my $include_rail = 1;
my $include_bus = 1;
my $include_ferry = 1;
my $include_taxi = 1;
my $include_cabcar = 1;
my $include_gondola = 1;
my $include_dom_air = 1;
my $include_int_air = 1;
my $routelim = 0;
my $taximode = 2; # 0: none 1: auto 2: only if duration
my $dateshift = 0;
my $geoacc = 6; # decimals for lat/lon in canon output
my $warn_nostop = 1;
my $omit_unconnect = 1;
my $omit_noparent = 0;
my $chkduproute = 0;
my $chkdupagroute = 0;
my $chkovertake = 0;
my $do_chksid = 0;
my $watchfile = 'watches.cfg';
my $gtfscfgfile = 'gtfsfeed.cfg';
my %stops2watch = ('0','noname');
my %rsids2watch;
my $logname = 'gtfstool.log';
my $logfd;
my $canonmagic = 'x4BAb35S';
my $iscanonin = 0;
my $geomagic = 'geo-JBKScycH'; # sync with gtfsprep
my $feedstamp = '';
my ($indir,$outdir,$format);
my $glinno = 0;
my $gfilename = '';
my $haltonperl = 1;
# stop at compile-time warnings
local $SIG{__WARN__} = sub {
print "$_[0]";
$. = "?" unless defined $.;
print " $gfilename line $.\n" if length $gfilename;
exit 1 if $haltonperl;
};
if (-t STDOUT) {
for (my $bck = 8; $bck >= 0; $bck--) {
rename($logname . '.' . $bck,$logname . '.' . ($bck+1)) if (-f $logname . '.' . $bck);
}
rename($logname,$logname . '.0') if -f $logname;
open($logfd,'>:encoding(UTF-8)',$logname) or print("cannot create $logname:$!");
}
# unbuffered i/o
my $orgfh = select STDOUT; $| = 1;
select STDERR; $| = 1;
if (defined $logfd) { select $logfd; $| = 1; }
select $orgfh;
binmode(STDOUT,':utf8');
sub basemsg($) {
my ($m) = @_;
print("$m\n");
print($logfd "$m\n") if defined $logfd;
return 1;
}
my $clron = sprintf('%c[1;36m',27); # 32 green 36 cyan
my $clrof = sprintf('%c[0m',27);
my $inprogress = 0;
my %msgcounts;
my $lastlvl;
sub msg($$) {
my ($m,$lvl) = @_;
$lastlvl = $lvl;
my $t = time2yyyymmdd(time(),1,'sec');
my $tt = substr($t,11); # only from hh in yyyy-mm-dd hh:mm:ss
my ($package,$filename,$line,$sub,$hasargs) = caller(1);
my $fln = sprintf("%4u",$line);
print "\n" if $inprogress;
print("$clron$tt $fln $clrof$m\n");
print($logfd "$t $fln $m\n") if defined $logfd;
$inprogress = 0;
return 1;
}
my %infocnts;
sub info($) {
my ($package,$filename,$line,$sub,$hasargs) = caller(0);
($package,$filename,$line,$sub) = caller(1) if defined $sub and $sub eq 'main::info';
$line = 0 unless defined $line;
$infocnts{$line} = 0 unless defined $infocnts{$line};
my $cnt = $infocnts{$line};
return 1 if $cnt > 1000 and $line > 0;
$cnt++;
if ($cnt == 1000) { msg("message at line $line repeated 100 times",'i'); $cnt++; }
$infocnts{$line} = $cnt;
return msg($_[0],'i');
}
my $prog_t0 = 0;
my $prog_t1 = 0;
sub progress($$$$) {
my ($fmt,$a,$b,$c) = @_;
my $t = time();
if ($a == 0) { $prog_t0 = $t; $prog_t1 = 0; return; }
if ($prog_t1 == 0 or $a < $b) {
return if $t - $prog_t0 < 1;
}
$prog_t0 = $prog_t1 = $t;
my $ft = time2yyyymmdd($t,1,'sec');
my $tt = substr($ft,11);
my ($package,$filename,$line) = caller(0);
my $fln = sprintf("%4u",$line);
my $perc = ($a * 100) / $b;
if ($a < $b) {
print "\n" unless $inprogress;
$inprogress = 1;
printf("\r%s%s %4u %s $fmt %.0f %%\r",$clron,$tt,$line,$clrof,$a,$b,$c,$perc);
} else {
printf("\r%s%s %4u %s $fmt done\n",$clron,$tt,$line,$clrof,$a,$b,$c);
$inprogress = 0;
}
}
sub dinfo($) {
my ($package,$filename,$line,$sub,$hasargs) = caller(0);
return msg($_[0] . " fn $sub ln $line",'i');
}
my (%warnings,%warncnts);
sub warning($) {
my ($package,$filename,$line,$sub,$hasargs) = caller(0);
($package,$filename,$line,$sub) = caller(1) if defined $sub and $sub eq 'main::error';
$line = 0 unless defined $line;
$warnings{$line} = $_[0] unless exists $warnings{$line};
$warncnts{$line} = 0 unless defined $warncnts{$line};
$warningcnt++;
my $cnt = $warncnts{$line};
return 1 if $cnt > 100;
$cnt++;
if ($cnt == 100) { msg("warning at line $line repeated 100 times",'i'); $cnt++; }
$warncnts{$line} = $cnt;
msg("warning: " . $_[0],'w');
return 1;
}
sub uwarning($) {
my ($package,$filename,$line,$sub,$hasargs) = caller(0);
($package,$filename,$line,$sub) = caller(1) if defined $sub and $sub eq 'main::error';
$line = 0 unless defined $line;
$warnings{$line} = $_[0] unless exists $warnings{$line};
$warncnts{$line} = 0 unless defined $warncnts{$line};
$warningcnt++;
my $cnt = $warncnts{$line};
$cnt++;
$warncnts{$line} = $cnt;
msg("warning: " . $_[0],'w');
return 1;
}
my (%errors,%errcnts);
sub error($) {
my ($package,$filename,$line,$sub) = caller(0);
($package,$filename,$line,$sub) = caller(1) if defined $sub and $sub eq 'main::error';
$sub = '(main)' unless defined $sub;
$sub =~ s/^main:://;
msg("error: $_[0] in $sub",'e');
$errors{$line} = $_[0] unless exists $errors{$line};
$errcnts{$line} = 0 unless defined $errcnts{$line};
$errorcnt++;
$errcnts{$line}++;
return 0;
}
sub serror($) {
msg("$_[0]",'e');
return 0;
}
sub error_exit($) {
my ($package,$filename,$line,$sub) = caller(0);
($package,$filename,$line,$sub) = caller(1) if defined $sub and $sub eq 'main::error_exit';
$sub = '(main)' unless defined $sub;
msg("error: $_[0] in $sub",'e');
$errorcnt++;
exit 1;
}
sub vrb($) {
msg($_[0],'v') if $verbose;
return 1;
}
sub vrb0($) { vrb($_[0]); }
sub infovrb($$) {
info($_[1]) if $_[0];
vrb($_[1]);
}
sub plural($$)
{
my ($n,$str) = @_;
my $s = "$n $str";
$s .= 's' if $n != 1;
return $s;
}
sub usage()
{
my $m = "usage: gtfstool [options] [cmd]\n\n";
$m .= "options:\n";
$m .= "-v -verbose verbose mode\n";
$m .= "-n -dryrun dryrun mode\n";
$m .= "-h -help show help and quit\n";
$m .= "-a -analysis enable analysis output\n";
$m .= "-c -canonical enable canonical output\n";
$m .= "-r -reserve mark all routes as requiring reservation\n";
$m .= "-t -test test only, no output\n";
$m .= "-V -version show version and quit\n";
$m .= "commands:\n\n";
$m .= "import <outdir> [indir] import gtfs into tripover\n";
$m .= "merge <outdir> [indirs] merge canonical gtfs feeds\n";
$m .= "air2gtfs <mode> <outdir> <indir> convert pdf timetables to gtfs\n";
$m .= 'tt2gtfs <mode> <outdir> <indir> convert generic timetables to gtfs';
basemsg($m);
}
sub max($$) {
my ($a,$b) = @_;
return ($a > $b ? $a : $b);
}
sub min($$) {
my ($a,$b) = @_;
return ($a < $b ? $a : $b);
}
sub trimws($) {
my ($s) = @_;
$s =~ s/[ \t\r\n]+/ /g;
$s =~ s/^ //;
$s =~ s/ $//;
return $s;
}
my $hhmmpat = qr'^([0-9]+):([0-9]+):([0-9]+)$';
sub time2yyyymmdd($$$) {
my ($nixsec,$local,$res) = @_;
my ($fmt,$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = ($local ? localtime($nixsec) : gmtime($nixsec));
return sprintf('%04u-%02u-%02u %02u:%02u:%02u',($year + 1900,$mon + 1,$mday,$hour,$min,$sec)) if $res eq 'sec';
return sprintf('%04u-%02u-%02u %02u:%02u',($year + 1900,$mon + 1,$mday,$hour,$min)) if $res eq 'min';
return sprintf('%04u-%02u-%02u %02u:%02u',($year + 1900,$mon + 1,$mday));
}
# these are written as options to ext output
my $hisid = 0;
my $nosid = 0xffff;
my $hirstopid = 0;
my $dowstart = 'monday';
# sync with netio
my $fmt_prvsid = 1;
my $fmt_diftid = 2;
my $fmt_difdep = 4;
my $fmt_difarr = 8;
my $fmt_prvdep = 16;
my $fmt_prvarr = 32;
my $fmt_rep = 64;
# sync with tripover
my $opt_childstop = 1;
my $opt_parentstop = 2;
my $opt_geostop = 4;
# store field results here
# defaults
my $agencyname;
my $agencyid;
my $feedpublisher = '';
my %agencies;
my %agencyids;
my %agencyrids;
my %stopids; # ext to int
my %stop_ids; # int to ext
# my %stops2ignore;
my (%stopnames,%stopinames);
my %stops;
my @rstops;
my (@stopdeps,@stoparrs);
my (%stop_deps,%stop_arrs);
my $stopcmt = '';
my %parents;
my %parentids;
my %parent_ids;
my %parentbyid;
my $parentcnt = 0;
my $parentdupcnt = 0;
my %stop2parent;
my %uniparents;
my %parentdups;
my (%rstops,%rstopids,%rstopbyid,%stopmerge,%rstopbyname);
my @calendar;
my @caldates;
my %serviceids;
my %noserviceids;
my %servicedow;
my %servicet0;
my %servicet1;
my %serviceadd;
my %servicesub;
my $sidcnt = 0;
my %sidrefs;
#my %sids2ignore;
my %tripids;
my %trip_ids;
my %orgtrip_ids;
my %tripseq;
my %tripseq1;
my %trippseq;
my %tripref;
my $hitripid = 0;
my @uniqids;
my %triproutes;
my %tripsids;
my %tripnos;
my @trips;
my %tripfreqs;
my @stop_times;
my %tripservice;
my @hops;
my @routes;
my %routeids;
my %norouteids;
my %routebyids;
my %routeagency;
my %rtypes;
my %agency_ids;
my %routeres;
my %routetrips;
my %routestarts;
my %routeends;
my %xfersets;
my $hirrid = 0;
my $sumtimes = 0;
my @trip_ids;
my @dep_times;
my @arr_times;
my @frequencies;
my $agencycnt = 0;
my $stopcnt = 0;
my $ttcnt = 0;
my $hopcnt = 0;
my $tripcnt = 0;
my $routecnt = 0;
my $xfercnt = 0;
my $noroutecnt = 0;
# overall date range : coded decimal yyyymmdd
my $mint0 = 20200101;
my $maxt1 = 0;
my $nondigits = qr'[^0-9]';
my $unquoter = qr'/""/"/g';
sub inarray($@) {
my ($a,@arr) = @_;
foreach my $b (@arr) { return 1 if $a eq $b; }
return 0;
}
sub showvers($)
{
my ($full) = @_;
basemsg("Tripover gtfs tool version $version_maj.$version_min");
basemsg("last changed $lastchanged") if $full;
basemsg("$copyright\n");
}
sub rdwatches()
{
my ($fh,$line,$linno);
my ($var,$val,$fmt);
info("inspecting $watchfile");
return unless -r $watchfile and -s $watchfile;
open($fh,"<",$watchfile) or return warning("cannot open $watchfile: $!");
my $varxval = qr?^([a-z_.]+)\s+([x,\*])([0-9a-f]*)?;
$linno = 0;
while($line = readline $fh) {
$linno++;
next if index($line,'#') == 0;
next if length $line < 2;
($var,$fmt,$val) = ($line =~ $varxval);
next unless defined $var and defined $fmt;
info("$var $fmt $val");
if ($fmt eq 'x' and defined $val) { $val = hex($val); }
elsif ($fmt eq '*') { $val = '*'; }
elsif ($fmt eq ',') { }
printf("%s %x.%u\n",$var,$val,$val) unless $fmt eq '*';
$rsids2watch{$val} = 1 if $var eq 'rsid';
}
}
sub min2hhmm($$)
{
my ($t,$fln) = @_;
my $h = int($t / 60);
my $m = int ($t % 60);
return sprintf("%02u%02u",$h,$m);
}
sub hhmm2min($$)
{
my ($s,$fln) = @_;
my ($thh,$tmm) = ($s =~ '^([0-9]+):([0-9]+)$');
error_exit("$fln: unrecognised time '$s'") unless defined $thh and defined $tmm;
my $tmin = int(($thh * 60) + $tmm);
return $tmin;
}
sub hhmm2sec($$)
{
my ($s,$fln) = @_;
my ($thh,$tmm,$tss) = ($s =~ $hhmmpat);
error_exit("$fln: unrecognised time $s") unless defined $thh and defined $tmm;
$tss = 0 unless defined $tss;
my $tsec = ($thh * 3600) + ($tmm * 60) + $tss;
return $tsec;
}
my (%tzofs);
my $dston_def = '0329';
my $dstof_def = '1025';
sub init($)
{
my $progdir = shift;
my $tzname = $progdir . '/tzdb.txt';
my $tzfile;
my $col = 0;
my ($line,$len,$tzstr,$tz,$ofs,$dstofs,$dston,$dstof,$a,$b,$c,$alt,$alias);
my @tzs;
my $tzcol = 3;
my $ofscol = 5;
my $dstofscol = 6;
rdwatches() if $anaout;
my $tznampat = qr'^[A-Z][a-z]+/[A-Z][-A-Za-z_/]+$';
my $tznampat1 = qr'^[A-Z0-9]+$';
# Africa/Windhoek 1:00 0904 0403
my $zonepat = qr'^([-+A-Z0-9a-z/_]+)\t([-0-9:]+)\t([-0-9:]+)\t([0-9]+)\t([0-9]+)$';
my $linkpat = qr'^([A-Za-z/_]+)\t([-+A-Za-z0-9/_]+)$';
my $fln = '';
open($tzfile,'<',$tzname) or return warning("cannot open $tzname:$!");
my %tzlines;
my @lines = readline($tzfile);
close($tzfile);
my $linno = 0;
for $line (@lines) {
$linno++;
next unless length($line);
next if index($line,'#') == 0;
$c = chop $line;
$fln = "$tzname.$linno: ";
warning("$fln: unterminated line '$line'") if $c ne "\n";
$len = length($line);
next unless $len;
$glinno = $linno;
($tzstr,$ofs,$dstofs,$dston,$dstof) = ($line =~ $zonepat);
if (defined $tzstr and length($tzstr)) {
return error("$fln unrecognised line '$line'") unless defined $dstof and length $dstof;
return error("$fln $tzstr already defined at line $tzlines{$tzstr}") if exists $tzofs{$tzstr};
$tzofs{$tzstr} = join(' ',$ofs,$dstofs,$dston,$dstof);
$tzlines{$tzstr} = $linno;
# info("new tz $tzstr $ofs $dstofs");
next;
}
($tzstr,$alias) = ($line =~ $linkpat);
if (defined($alias) and length($alias)) {
return error("$fln $alias to itself") if $alias eq $tzstr;
return error("$fln $alias already defined") if exists $tzofs{$alias};
return error("$fln unknown zone $tzstr for $alias") unless exists $tzofs{$tzstr};
$tz = $tzofs{$tzstr};
# info("new alias $alias for $tzstr $tz");
$tzofs{$alias} = $tz;
next;
}
return error("$fln unrecognised line $line");
}
return 1;
# read timezone info from wikipedia export, in turn from iana TZ database
open($tzfile,'<:encoding(UTF-8)',$tzname) or return warning("cannot open $tzname:$!");
@lines = readline($tzfile);
close($tzfile);
$linno = 0;
for $line (@lines) {
$linno++;
next unless length($line);
next if index($line,'#') == 0;
$len = length($line);
@tzs = ();
if (index($line,'|-') == 0) {
$col = 0;
} elsif (index($line,'|') == 0) {
$line = substr($line,1);
$col++;
}
if ($col == $tzcol) {
$a = index($line,'[[');
$b = index($line,']]');
$tzstr = substr($line,$a+2,$b-3);
} elsif ($col == $ofscol) {
$a = index($line,'[[');
$c = index($line,'|');
$ofs = substr($line,$a+5,$c-6);
if (substr($ofs,0,1) eq "\x{2212}") { $ofs = '-' . substr($ofs,1); } # math minus
return error("tzinfo.$linno: $tzstr: expected hh:mm, found $ofs") unless $ofs =~ qr'[-+][01][0-9]:[0134][05]';
} elsif ($col == $dstofscol) {
$a = index($line,'[[');
$c = index($line,'|');
$dston = $dston_def;
$dstof = $dstof_def;
if ($a < 0) { info("$tzname:$linno:$tzstr:no dstofs for $tz"); $dstofs = $ofs; }
else {
$dstofs = substr($line,$a+5,$c-6);
$a = index($line,'from ');
$dston = substr($line,$a+5,4) if $a > 0;
$b = index($line,'to ');
$dstof = substr($line,$b+3,4) if $b > 0;
return error("tzinfo.$linno:$tzstr: expected dst start mmdd, found $dston") unless $dston =~ qr'[01][0-9][0-3][0-9]';
return error("tzinfo.$linno:$tzstr: expected dst end mmdd, found $dstof") unless $dstof =~ qr'[01][0-9][0-3][0-9]';
}
if (substr($dstofs,0,1) eq "\x{2212}") { $dstofs = '-' . substr($dstofs,1); }
return error("tzinfo.$linno:$tzstr: expected hh:mm, found $dstofs") unless $dstofs =~ qr'[-+][01][0-9]:[0134][05]';
} elsif ($col > $dstofscol) { # aliases
while (length($tzstr)) {
$alt = index($tzstr,'|');
if ($alt < 0) {
push @tzs,$tzstr;
$tzstr = '';
} else {
$tz = substr($tzstr,0,$alt);
push @tzs,$tz;
$tzstr = substr($tzstr,$alt + 1);
}
}
foreach $tz (@tzs) {
$tzofs{$tz} = join(' ',$ofs,$dstofs,$dston,$dstof) unless defined $tzofs{$tz};
}
$col = 0;
}
}
return 1;
}
# Europe/Paris or +02:30 to hh,mm,dstondstof 01,00,03291025
sub tz2ofs($$)
{
my ($str,$fln) = @_;
my ($a,$hh,$mm,$dhh,$dmm,$ofs,$dstofs,$dston,$dstof);
if ($str =~ qr'^[-+][0-9]+:[0-5]+$') { $ofs = $dstofs = $str; }
elsif ($str =~ qr'^[+]?[0-9]+$') {
$hh = int($str / 100);
$mm = int($str % 100);
$ofs = $dstofs = sprintf("%02u:%02u",$hh,$mm);
} elsif ($str =~ qr'^-[0-9]+$') {
$str = substr($str,1);
$hh = int($str / 100);
$mm = int($str % 100);
$ofs = $dstofs = sprintf("-%02u:%02u",$hh,$mm);
} else {
error_exit("$fln: no timezone info for $str") unless exists($tzofs{$str});
($ofs,$dstofs,$dston,$dstof) = split(' ',$tzofs{$str});
}
$a = index($ofs,':');
$hh = $dhh = substr($ofs,0,$a);
$mm = $dmm = substr($ofs,$a+1);
$dston = '' unless defined $dston;
$dstof = '' unless defined $dstof;
if ($dstofs eq $ofs) {
$dston = ''; $dstof = '';
} elsif ($dston eq '') {
warn("tz $str at $fln has no dst dates");
} else {
$a = index($dstofs,':');
$dhh = substr($dstofs,0,$a);
$dmm = substr($dstofs,$a+1);
}
vrb("utc offset for $str hh $hh mm $mm $dston $dstof");
return ($hh,$mm,$dhh,$dmm,$dston . $dstof);
}
sub indst($$$)
{
my ($date,$dston,$dstof) = @_;
$date %= 10000;
# info("$date $dston $dstof");
if ($dston < $dstof) {
if ($date >= $dston && $date < $dstof) { return 1; }
} else {
if ($date < $dstof || $date >= $dston) { return 1; }
}
return 0;
}
# Europe/Paris,0502 or -4:30 to minutes east from utc, includes dst
sub tz2dutcofs($$$$)
{
my ($tz,$date,$fln,$dbg) = @_;
error_exit("$fln: empty tz") unless length $tz;
my ($hh,$mm,$dhh,$dmm,$dston,$dstof,$dstonof);
($hh,$mm,$dhh,$dmm,$dstonof) = tz2ofs($tz,$fln);
vrb0("'$tz' @ $date -> $hh:$mm dst '$dstonof'") if $dbg;
if (length($dstonof)) {
$dston = int($dstonof / 10000);
$dstof = int($dstonof % 10000);
if (indst($date,$dston,$dstof)) {
info("$fln '$tz' @ $date $hh:$mm = $dhh:$dmm dst '$dstonof'") if $dbg;
$hh = $dhh;
$mm = $dmm;
}
}
if ($hh < 0) { return ($hh * 60 - $mm); }
return ($hh * 60 + $mm);
}
sub mksid($)
{
my $service_id = shift;
my ($dow,$utcofs,$t0,$t1) = split(' ',$service_id);
error_exit("invalid date $t0") unless $t0 =~ qr'^[0-9]+$';
error_exit("invalid date $t1") unless $t1 =~ qr'^[0-9]+$';
my ($mon,$tue,$wed,$thu,$fri,$sat,$sun);
if (index($dow,'1') < 0) { $mon = 0; } else { $mon = 1; }
if (index($dow,'2') < 0) { $tue = 0; } else { $tue = 1; }
if (index($dow,'3') < 0) { $wed = 0; } else { $wed = 1; }
if (index($dow,'4') < 0) { $thu = 0; } else { $thu = 1; }
if (index($dow,'5') < 0) { $fri = 0; } else { $fri = 1; }
if (index($dow,'6') < 0) { $sat = 0; } else { $sat = 1; }
if (index($dow,'7') < 0) { $sun = 0; } else { $sun = 1; }
push @calendar,join("\t",$service_id,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$t0,$t1);
}
sub sun2mon($)
{
my $sdow = shift;
my $dow = '';
$dow .= '1' if index($sdow,'2') >= 0;
$dow .= '2' if index($sdow,'3') >= 0;
$dow .= '3' if index($sdow,'4') >= 0;
$dow .= '4' if index($sdow,'5') >= 0;
$dow .= '5' if index($sdow,'6') >= 0;
$dow .= '6' if index($sdow,'7') >= 0;
$dow .= '7' if index($sdow,'1') >= 0;
return $dow;
}
# 3 Feb to 0203
sub ddmmm($$$)
{
my ($day,$mon,$linno) = @_;
my $cdday = $day;
if ($mon eq 'Jan') { $cdday += 100; }
elsif ($mon eq 'Feb') { $cdday += 200; }
elsif ($mon eq 'Mar') { $cdday += 300; }
elsif ($mon eq 'Apr') { $cdday += 400; }
elsif ($mon eq 'May') { $cdday += 500; }
elsif ($mon eq 'Jun') { $cdday += 600; }
elsif ($mon eq 'Jul') { $cdday += 700; }
elsif ($mon eq 'Aug') { $cdday += 800; }
elsif ($mon eq 'Sep') { $cdday += 900; }
elsif ($mon eq 'Oct') { $cdday += 1000; }
elsif ($mon eq 'Nov') { $cdday += 1100; }
elsif ($mon eq 'Dec') { $cdday += 1200; }
else { error("line $linno unrecognised month $mon"); }
return sprintf('%04u',$cdday);
}
# Februari 21 2015 to 150221
sub mkyymmdd($$$)
{
my ($mmm,$dd,$yy4) = @_;
my $mmdd = ddmmm($dd,substr($mmm,0,3),0);
my $yy = substr($yy4,2);
return $yy . $mmdd;
}
# 12Nov to 1112
sub ddmmm2mmdd($)
{
my $pat = shift;
my ($d,$m) = ($pat =~ '([0-9]+)([A-Za-z]{3})');
error("unrecognised date $pat") unless defined $m;
return ddmmm($d,$m,0);
}
# 0328+6 to 0404
sub nextdate($$$)
{
my ($mmdd,$year,$plusday) = @_;
my $yy = int($year - 1900);
my $mm = int( ($mmdd / 100) - 1);
my $dd = int($mmdd % 100);
my $nixsec = POSIX::mktime(0,0,0,$dd,$mm,$yy);
$nixsec += ($plusday * 3600 * 24);
my ($sec,$min,$hour,$mday,$mon,$yr,$wday,$yday,$isdst) = localtime($nixsec);
my $mmdd6 = sprintf("%02u%02u",$mon+1,$mday);
return $mmdd6;
}
# 20170516 to secs utc 70
sub yymmdd2sec($)
{
my ($yymmdd) = @_;
my $yy = int($yymmdd / 10000);
error_exit("invalid year $yy from $yymmdd") if $yy < 1900;
$yy -= 1900;
my $mm = int( ($yymmdd / 100) % 100);
error_exit("invalid month $mm from $yymmdd") if $mm == 0 or $mm > 12;
$mm--;
my $dd = int($yymmdd % 100);
my $nixsec = POSIX::mktime(0,0,0,$dd,$mm,$yy);
return $nixsec;
}
# 20160128 + 6 to 20160203
sub nextyymmdd($$)
{
my ($yymmdd,$plusday) = @_;
my $nixsec = yymmdd2sec($yymmdd);
$nixsec += ($plusday * 3600 * 24);
my ($sec,$min,$hour,$mday,$mon,$yr,$wday,$yday,$isdst) = localtime($nixsec);
my $mmdd6 = sprintf("%04u%02u%02u",$yr + 1900,$mon+1,$mday);
return $mmdd6;
}
# day of week for yyyymmdd, mon = 6 sun = 0
sub getdow($)
{
my ($yymmdd) = @_;
my $nixsec = yymmdd2sec($yymmdd);
my ($sec,$min,$hour,$mday,$mon,$yr,$wday,$yday,$isdst) = gmtime($nixsec);
return 0 if $wday == 0; # sun
return 6 if $wday == 1; # mon
return 5 if $wday == 2; # mon
return 4 if $wday == 3; # mon
return 3 if $wday == 4; # mon
return 2 if $wday == 5; # mon
return 1 if $wday == 6; # mon
}
sub patch($$)
{
my ($section,$indir) = @_;
my ($c,$i,$line,$fline,$match,$nomatch,$pat,$rep,$mp);
my $modname = $indir . '/' . $section . '.patch';
my $srcname = $indir . '/' . $section . '.txt';
info("patching $srcname with $modname");
return 1 unless -f $modname;
info("patching $section");
open(my $updfh,'<:encoding(UTF-8)',$modname) or return error("cannot open $modname: $!");
my @modlines = readline($updfh);
close $updfh;
return 1 unless @modlines > 0;
open(my $srcfh,'<:encoding(UTF-8)',$srcname) or return error("cannot open $srcname: $!");
my @srclines = readline($srcfh);
close $srcfh;
my $linecnt = scalar(@srclines);
return 1 unless $linecnt > 0;
my $change = 0;
my %patches;
my $linno = 0;
foreach my $line (@modlines) {
next unless length($line);
$c = chop $line;
$line .= $c if $c ne "\n";
$linno++;
next unless length($line);
$c = substr($line,0,1);
next if $c eq '#';
if ($c eq '\\') {
($pat,$rep) = split("\t",substr($line,1));
next unless defined $pat and length $pat;
next unless defined $rep and length $rep;
$match = $pat;
$rep = "$rep:$pat";
} else {
($match,$pat,$rep,$nomatch) = split("\t",$line);
$rep = '' unless defined $rep;
next unless defined $pat;
$match = $rep unless defined $match and length $match;
}
$mp = "$match\t$pat";
return error("line $linno: pattern $match $pat defined on line $patches{$mp}") if exists $patches{$mp};
$patches{$mp} = $linno;
for $i (0 .. $linecnt-1) {
$fline = $srclines[$i];
next if index($fline,$match) < 0;
next if index($fline,$pat) < 0;
next if defined $nomatch and length $nomatch and index($fline,$nomatch) >= 0;
$fline =~ s/$pat/$rep/;
$change++ if $fline ne $srclines[$i];
$srclines[$i] = $fline;
}
}
return 1 if $change == 0;
info("$change lines patched in $section");
my $nsrcname = $indir . '/' . $section . '.txt.new';