Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
 
5
use DateTimeZone;
6
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
7
 
8
class TimeZone
9
{
10
    /**
11
     * Default Timezone used for date/time conversions.
12
     */
13
    protected static string $timezone = 'UTC';
14
 
15
    /**
16
     * Validate a Timezone name.
17
     *
18
     * @param string $timezoneName Time zone (e.g. 'Europe/London')
19
     *
20
     * @return bool Success or failure
21
     */
22
    private static function validateTimeZone(string $timezoneName): bool
23
    {
24
        return in_array($timezoneName, DateTimeZone::listIdentifiers(DateTimeZone::ALL_WITH_BC), true);
25
    }
26
 
27
    /**
28
     * Set the Default Timezone used for date/time conversions.
29
     *
30
     * @param string $timezoneName Time zone (e.g. 'Europe/London')
31
     *
32
     * @return bool Success or failure
33
     */
34
    public static function setTimeZone(string $timezoneName): bool
35
    {
36
        if (self::validateTimeZone($timezoneName)) {
37
            self::$timezone = $timezoneName;
38
 
39
            return true;
40
        }
41
 
42
        return false;
43
    }
44
 
45
    /**
46
     * Return the Default Timezone used for date/time conversions.
47
     *
48
     * @return string Timezone (e.g. 'Europe/London')
49
     */
50
    public static function getTimeZone(): string
51
    {
52
        return self::$timezone;
53
    }
54
 
55
    /**
56
     *    Return the Timezone offset used for date/time conversions to/from UST
57
     * This requires both the timezone and the calculated date/time to allow for local DST.
58
     *
59
     * @param ?string $timezoneName The timezone for finding the adjustment to UST
60
     * @param float|int $timestamp PHP date/time value
61
     *
62
     * @return int Number of seconds for timezone adjustment
63
     */
64
    public static function getTimeZoneAdjustment(?string $timezoneName, $timestamp): int
65
    {
66
        $timezoneName = $timezoneName ?? self::$timezone;
67
        $dtobj = Date::dateTimeFromTimestamp("$timestamp");
68
        if (!self::validateTimeZone($timezoneName)) {
69
            throw new PhpSpreadsheetException("Invalid timezone $timezoneName");
70
        }
71
        $dtobj->setTimeZone(new DateTimeZone($timezoneName));
72
 
73
        return $dtobj->getOffset();
74
    }
75
}