[pve-devel] [PATCH common 1/5] jsonschema: register 'timezone' format and add verification method

Oguz Bektas o.bektas at proxmox.com
Wed Jun 17 14:34:33 CEST 2020


hi,

On Tue, Jun 16, 2020 at 04:28:05PM +0200, Thomas Lamprecht wrote:
> Am 6/16/20 um 3:36 PM schrieb Oguz Bektas:
> > /usr/share/zoneinfo/zone.tab has the valid list of time zones.
> > 
> > Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
> > ---
> >  src/PVE/JSONSchema.pm | 24 ++++++++++++++++++++++++
> >  1 file changed, 24 insertions(+)
> > 
> > diff --git a/src/PVE/JSONSchema.pm b/src/PVE/JSONSchema.pm
> > index 84fb694..ff97a3d 100644
> > --- a/src/PVE/JSONSchema.pm
> > +++ b/src/PVE/JSONSchema.pm
> > @@ -482,6 +482,30 @@ sub pve_verify_dns_name {
> >      return $name;
> >  }
> >  
> > +register_format('timezone', \&pve_verify_timezone);
> > +sub pve_verify_timezone {
> > +    my ($timezone, $noerr) = @_;
> > +
> > +    my $zonetab = "/usr/share/zoneinfo/zone.tab";
> > +    my @valid_tzlist;
> > +    push @valid_tzlist, 'host'; # host localtime
> 
> do not add that here, this isn't a timezone - filter that value out in pve-container API
> as it's just a special value there.


then in PVE/LXC/Config.pm
something like:

```
PVE::JSONSchema::register_format('pve-lxc-timezone', \&verify_ct_timezone);
sub verify_ct_timezone {
  my ($timezone) = @_;

  return if $timezone eq 'host';

  PVE::JSONSchema::verify_timezone($timezone);
}
```


and keep the 'verify_timezone' in pve-common intact for more general use??


> 
> > +    push @valid_tzlist, 'UTC'; # not in $zonetab
> 
> Don push unconditionally on array you just declared, rather:
> 
> my @valid_tzlist = ('UTC');
> 
> But actually that array isn't required at all:
> 
> Rather do:
> 
> return $timezone if $timezone eq 'UTC';
> 
> open(my $fh, "<", $zonetab);
> while(my $line = <$fh>) {
>     next if $line =~ /^#/;
>     chomp $line;
>     
>     return $timezone if $line eq $timezone; # found
> }
> close $fh;
> 
> die "invalid time zone '$timezone'\n";
> 
> Shorter and faster.
> 
> > +    open(my $fh, "<", $zonetab);
> > +    while(my $line = <$fh>) {
> > +	next if $line =~ /^#/;
> > +	chomp $line;
> > +	push @valid_tzlist, (split /\t/, $line)[2];
> > +    }
> > +    close $fh;
> > +
> > +    if (grep (/^$timezone$/, @valid_tzlist) eq 0) {
> > +	return undef if $noerr;
> > +	die "invalid time zone '$timezone'\n";
> > +    }
> > +
> > +    return $timezone;
> > +}
> > +
> >  # network interface name
> >  register_format('pve-iface', \&pve_verify_iface);
> >  sub pve_verify_iface {
> > 
> 




More information about the pve-devel mailing list