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\Calculation\DateTimeExcel;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
7
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
8
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
9
 
10
class DateParts
11
{
12
    use ArrayEnabled;
13
 
14
    /**
15
     * DAYOFMONTH.
16
     *
17
     * Returns the day of the month, for a specified date. The day is given as an integer
18
     * ranging from 1 to 31.
19
     *
20
     * Excel Function:
21
     *        DAY(dateValue)
22
     *
23
     * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
24
     *                                    PHP DateTime object, or a standard date string
25
     *                         Or can be an array of date values
26
     *
27
     * @return array|int|string Day of the month
28
     *         If an array of numbers is passed as the argument, then the returned result will also be an array
29
     *            with the same dimensions
30
     */
31
    public static function day(mixed $dateValue): array|int|string
32
    {
33
        if (is_array($dateValue)) {
34
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
35
        }
36
 
37
        $weirdResult = self::weirdCondition($dateValue);
38
        if ($weirdResult >= 0) {
39
            return $weirdResult;
40
        }
41
 
42
        try {
43
            $dateValue = Helpers::getDateValue($dateValue);
44
        } catch (Exception $e) {
45
            return $e->getMessage();
46
        }
47
 
48
        // Execute function
49
        $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
50
        SharedDateHelper::roundMicroseconds($PHPDateObject);
51
 
52
        return (int) $PHPDateObject->format('j');
53
    }
54
 
55
    /**
56
     * MONTHOFYEAR.
57
     *
58
     * Returns the month of a date represented by a serial number.
59
     * The month is given as an integer, ranging from 1 (January) to 12 (December).
60
     *
61
     * Excel Function:
62
     *        MONTH(dateValue)
63
     *
64
     * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
65
     *                                    PHP DateTime object, or a standard date string
66
     *                         Or can be an array of date values
67
     *
68
     * @return array|int|string Month of the year
69
     *         If an array of numbers is passed as the argument, then the returned result will also be an array
70
     *            with the same dimensions
71
     */
72
    public static function month(mixed $dateValue): array|string|int
73
    {
74
        if (is_array($dateValue)) {
75
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
76
        }
77
 
78
        try {
79
            $dateValue = Helpers::getDateValue($dateValue);
80
        } catch (Exception $e) {
81
            return $e->getMessage();
82
        }
83
        if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
84
            return 1;
85
        }
86
 
87
        // Execute function
88
        $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
89
        SharedDateHelper::roundMicroseconds($PHPDateObject);
90
 
91
        return (int) $PHPDateObject->format('n');
92
    }
93
 
94
    /**
95
     * YEAR.
96
     *
97
     * Returns the year corresponding to a date.
98
     * The year is returned as an integer in the range 1900-9999.
99
     *
100
     * Excel Function:
101
     *        YEAR(dateValue)
102
     *
103
     * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
104
     *                                    PHP DateTime object, or a standard date string
105
     *                         Or can be an array of date values
106
     *
107
     * @return array|int|string Year
108
     *         If an array of numbers is passed as the argument, then the returned result will also be an array
109
     *            with the same dimensions
110
     */
111
    public static function year(mixed $dateValue): array|string|int
112
    {
113
        if (is_array($dateValue)) {
114
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
115
        }
116
 
117
        try {
118
            $dateValue = Helpers::getDateValue($dateValue);
119
        } catch (Exception $e) {
120
            return $e->getMessage();
121
        }
122
 
123
        if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
124
            return 1900;
125
        }
126
        // Execute function
127
        $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
128
        SharedDateHelper::roundMicroseconds($PHPDateObject);
129
 
130
        return (int) $PHPDateObject->format('Y');
131
    }
132
 
133
    /**
134
     * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
135
     *                                    PHP DateTime object, or a standard date string
136
     */
137
    private static function weirdCondition(mixed $dateValue): int
138
    {
139
        // Excel does not treat 0 consistently for DAY vs. (MONTH or YEAR)
140
        if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900 && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
141
            if (is_bool($dateValue)) {
142
                return (int) $dateValue;
143
            }
144
            if ($dateValue === null) {
145
                return 0;
146
            }
147
            if (is_numeric($dateValue) && $dateValue < 1 && $dateValue >= 0) {
148
                return 0;
149
            }
150
        }
151
 
152
        return -1;
153
    }
154
}