You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: utils/cs/datetime.texy
+14Lines changed: 14 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,9 @@ Datum a čas
4
4
.[perex]
5
5
[api:Nette\Utils\DateTime] je třída, která rozšiřuje nativní [php:DateTime] o další funkce.
6
6
7
+
Oproti svému předchůdci je striktní. Zatímco PHP **tiše akceptuje** nesmyslná data jako `0000-00-00` (převede na `-0001-11-30`) nebo `2024-02-31` (převede na `2024-03-02`), `Nette\Utils\DateTime` v takových případech vyhodí výjimku.
8
+
9
+
Zároveň **opravuje chování** při přechodu na letní/zimní čas, kdy v nativním PHP může přičtení relativního času (např. `+100 minutes`) vést k dřívějšímu výslednému času než přičtení kratšího úseku (např. `+50 minutes`). Nette zajišťuje, že aritmetika funguje intuitivně a `+100 minutes` je vždy více než `+50 minutes`.
Tato metoda je užitečná v situacích, kdy potřebujete vícekrát projít stejnou sadu dat, ale původní iterátor neumožňuje opakovanou iteraci nebo by opakované procházení bylo nákladné (např. při čtení dat z databáze nebo souboru).
Umožňuje opakovanou iteraci objektů, které to běžně nedovolují, typicky [PHP generátorů |https://www.php.net/manual/en/language.generators.overview.php]. Metoda `repeatable()` tento problém elegantně řeší: místo samotného iterátoru jí předáte funkci, která iterátor vytváří. Tato továrna je pak zavolána automaticky při každém novém průchodu cyklem.
147
+
148
+
```php
149
+
// Běžný generátor, který nelze projít dvakrát
150
+
$generator = function () {
151
+
yield 'A';
152
+
yield 'B';
153
+
};
154
+
155
+
$iterator = Iterables::repeatable($generator);
156
+
157
+
foreach ($iterator as $v) echo $v; // Vypíše: AB
158
+
foreach ($iterator as $v) echo $v; // Vypíše: AB (generátor se spustil znovu)
159
+
```
160
+
161
+
Tato metoda je alternativou k [#memoize()] v situacích, kdy pracujete s **velkým objemem dat**.
162
+
- `memoize()` si data ukládá do paměti (rychlé pro druhé čtení, ale náročné na RAM).
163
+
- `repeatable()` data neukládá, ale při každém průchodu je generuje znovu (šetří RAM, ale může být náročnější na procesor).
Vrací objekt Type, který akceptuje jak původní typ, tak i nově přidaný. Vytváří tzv. union type.
225
+
226
+
Metoda je chytrá a typy zbytečně nezdvojuje. Pokud přidáte typ, který je již obsažen, nebo je nadmnožinou stávajícího typu (např. přidání `mixed` k `string`), vrátí se zjednodušený výsledek.
Copy file name to clipboardExpand all lines: utils/en/datetime.texy
+14Lines changed: 14 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,9 @@ Date and Time
4
4
.[perex]
5
5
[api:Nette\Utils\DateTime] is a class that extends the native [php:DateTime] with additional useful features.
6
6
7
+
Compared to the native class, it is strict. While PHP **silently accepts** invalid dates like `0000-00-00` (converts to `-0001-11-30`) or `2024-02-31` (converts to `2024-03-02`), `Nette\Utils\DateTime` throws an exception in such cases.
8
+
9
+
It also **fixes the behavior** during Daylight Saving Time (DST) transitions, where in native PHP adding a relative time (e.g., `+100 minutes`) can result in an earlier time than adding a shorter period (e.g., `+50 minutes`). Nette ensures that arithmetic works intuitively and `+100 minutes` is always more than `+50 minutes`.
7
10
8
11
Installation:
9
12
@@ -46,6 +49,17 @@ DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London'); // create wi
46
49
```
47
50
48
51
52
+
static relativeToSeconds(string $str): int .[method]{data-version:4.0.7}
Splits a PHP class name into a namespace and a short class name. Returns an array of two strings where the first is the namespace and the second is the class name.
This method is useful in situations where you need to iterate over the same dataset multiple times, but the original iterator doesn't allow repeated iteration, or re-traversing would be costly (e.g., reading data from a database or file).
Allows repeated iteration of objects that otherwise do not support it, typically [PHP generators |https://www.php.net/manual/en/language.generators.overview.php]. The `repeatable()` method solves this problem elegantly: instead of passing the iterator itself, you pass a function that creates it. This factory is then called automatically during each iteration loop.
147
+
148
+
```php
149
+
// A standard generator that cannot be iterated twice
150
+
$generator = function () {
151
+
yield 'A';
152
+
yield 'B';
153
+
};
154
+
155
+
$iterator = Iterables::repeatable($generator);
156
+
157
+
foreach ($iterator as $v) echo $v; // Prints: AB
158
+
foreach ($iterator as $v) echo $v; // Prints: AB (generator ran again)
159
+
```
160
+
161
+
This method is an alternative to [#memoize()] in situations where you are working with **large datasets**.
162
+
- `memoize()` caches data in memory (fast for subsequent iteration, but memory-intensive).
163
+
- `repeatable()` does not cache data, but generates it again during each iteration (saves RAM, but can be more CPU-intensive).
For resources, it returns `mixed`, as PHP does not support the `resource` type. For anonymous classes, it returns the name of the nearest ancestor or `object`.
The `allows()` method checks type compatibility. For example, it can determine if a value of a certain type could be passed as a parameter to a function expecting this type.
Returns a Type object that accepts both the original type and the one being added. It creates a so-called union type.
225
+
226
+
The method is clever and does not duplicate types unnecessarily. If you add a type that is already present, or is a superset of the current type (e.g., adding `mixed` to `string`), the result is simplified.
0 commit comments