[pve-devel] applied: [PATCH common] fix #1682: handle relative years absolutely

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Mar 7 11:49:37 CET 2018


applied

On 2/28/18 10:42 AM, Fabian Grünbichler wrote:
> the timegm(gmtime()) and timelocal(localtime(()) constructs are
> problematic in the following case: - $last is such that $year gets set
> to a two-digit value (e.g., the referred to timestamp is somewhere in
> the range of 1900-1999) - the current date is such that the value of
> $year gets interpreted wrongly (e.g., anything other than 1950).
> 
> the exact breakage depends on the actual current date AND value of
> $last, since localtime/gmtime will interpret two-digit years as (perldoc
> Time::Local):
>     [...] shorthand for years in the rolling "current century," defined
>     as 50 years on either side of the current year. Thus, today, in
>     1999, 0 would refer to 2000, and 45 to 2045, but 55 would refer to
>     1955.  Twenty years from now, 55 would instead refer to 2055.
> 
> fix it by adding 1900 to force 4-digit $year values, as the localtime
> documentation suggests.
> 
> Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
> ---
>  src/PVE/CalendarEvent.pm | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/src/PVE/CalendarEvent.pm b/src/PVE/CalendarEvent.pm
> index 77b6008..3c08eb0 100644
> --- a/src/PVE/CalendarEvent.pm
> +++ b/src/PVE/CalendarEvent.pm
> @@ -177,9 +177,13 @@ sub compute_next_event {
>  
>  	if ($utc) {
>  	    (undef, $min, $hour, $mday, $mon, $year, $wday) = gmtime($last);
> +	    # gmtime and timegm interpret two-digit years differently
> +	    $year += 1900;
>  	    $startofday = timegm(0, 0, 0, $mday, $mon, $year);
>  	} else {
>  	    (undef, $min, $hour, $mday, $mon, $year, $wday) = localtime($last);
> +	    # localtime and timelocal interpret two-digit years differently
> +	    $year += 1900;
>  	    $startofday = timelocal(0, 0, 0, $mday, $mon, $year);
>  	}
>  
> 






More information about the pve-devel mailing list