Clean up Core/Date/Util (#3692)
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 3 Nov 2020 16:23:46 +0000 (17:23 +0100)
committerGitHub <noreply@github.com>
Tue, 3 Nov 2020 16:23:46 +0000 (17:23 +0100)
- Reduce scope of char and hours.
- Consistently use .padStart instead of `slice`
- Fix 'y' for years starting with 10000 AD

wcfsetup/install/files/js/WoltLabSuite/Core/Date/Util.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Date/Util.ts

index fe7821c0bfa9ea81f9ce4caba91c1ed476103474..43e94e1c091e183bdcb950597c8fe212348ece6c 100644 (file)
@@ -40,19 +40,18 @@ define(["require", "exports", "tslib", "../Language"], function (require, export
      * Formats a date using PHP's `date()` modifiers.
      */
     function format(date, format) {
-        let char;
-        let out = "";
         // ISO 8601 date, best recognition by PHP's strtotime()
         if (format === "c") {
             format = "Y-m-dTH:i:sP";
         }
+        let out = "";
         for (let i = 0, length = format.length; i < length; i++) {
-            let hours;
+            let char;
             switch (format[i]) {
                 // seconds
                 case "s":
                     // `00` through `59`
-                    char = ("0" + date.getSeconds().toString()).slice(-2);
+                    char = date.getSeconds().toString().padStart(2, "0");
                     break;
                 // minutes
                 case "i":
@@ -64,9 +63,9 @@ define(["require", "exports", "tslib", "../Language"], function (require, export
                     // `am` or `pm`
                     char = date.getHours() > 11 ? "pm" : "am";
                     break;
-                case "g":
+                case "g": {
                     // `1` through `12`
-                    hours = date.getHours();
+                    const hours = date.getHours();
                     if (hours === 0) {
                         char = "12";
                     }
@@ -77,9 +76,10 @@ define(["require", "exports", "tslib", "../Language"], function (require, export
                         char = hours.toString();
                     }
                     break;
-                case "h":
+                }
+                case "h": {
                     // `01` through `12`
-                    hours = date.getHours();
+                    const hours = date.getHours();
                     if (hours === 0) {
                         char = "12";
                     }
@@ -91,6 +91,7 @@ define(["require", "exports", "tslib", "../Language"], function (require, export
                     }
                     char = char.padStart(2, "0");
                     break;
+                }
                 case "A":
                     // `AM` or `PM`
                     char = date.getHours() > 11 ? "PM" : "AM";
@@ -144,7 +145,7 @@ define(["require", "exports", "tslib", "../Language"], function (require, export
                 // year
                 case "y":
                     // `00` through `99`
-                    char = date.getFullYear().toString().substr(2);
+                    char = date.getFullYear().toString().slice(-2);
                     break;
                 case "Y":
                     // Examples: `1988` or `2015`
@@ -155,9 +156,9 @@ define(["require", "exports", "tslib", "../Language"], function (require, export
                     let offset = date.getTimezoneOffset();
                     char = offset > 0 ? "-" : "+";
                     offset = Math.abs(offset);
-                    char += ("0" + (~~(offset / 60)).toString()).slice(-2);
+                    char += (~~(offset / 60)).toString().padStart(2, "0");
                     char += ":";
-                    char += ("0" + (offset % 60).toString()).slice(-2);
+                    char += (offset % 60).toString().padStart(2, "0");
                     break;
                 }
                 // specials
index 950b946a4858ba8c1211732a82831c86024115a6..84fdd6f2b47801a3751ca894d15383e76861c6b0 100644 (file)
@@ -39,22 +39,19 @@ export function formatDateTime(date: Date): string {
  * Formats a date using PHP's `date()` modifiers.
  */
 export function format(date: Date, format: string): string {
-  let char: string;
-  let out = "";
-
   // ISO 8601 date, best recognition by PHP's strtotime()
   if (format === "c") {
     format = "Y-m-dTH:i:sP";
   }
 
+  let out = "";
   for (let i = 0, length = format.length; i < length; i++) {
-    let hours: number;
-
+    let char: string;
     switch (format[i]) {
       // seconds
       case "s":
         // `00` through `59`
-        char = ("0" + date.getSeconds().toString()).slice(-2);
+        char = date.getSeconds().toString().padStart(2, "0");
         break;
 
       // minutes
@@ -68,9 +65,9 @@ export function format(date: Date, format: string): string {
         // `am` or `pm`
         char = date.getHours() > 11 ? "pm" : "am";
         break;
-      case "g":
+      case "g": {
         // `1` through `12`
-        hours = date.getHours();
+        const hours = date.getHours();
         if (hours === 0) {
           char = "12";
         } else if (hours > 12) {
@@ -78,10 +75,12 @@ export function format(date: Date, format: string): string {
         } else {
           char = hours.toString();
         }
+
         break;
-      case "h":
+      }
+      case "h": {
         // `01` through `12`
-        hours = date.getHours();
+        const hours = date.getHours();
         if (hours === 0) {
           char = "12";
         } else if (hours > 12) {
@@ -91,7 +90,9 @@ export function format(date: Date, format: string): string {
         }
 
         char = char.padStart(2, "0");
+
         break;
+      }
       case "A":
         // `AM` or `PM`
         char = date.getHours() > 11 ? "PM" : "AM";
@@ -148,7 +149,7 @@ export function format(date: Date, format: string): string {
       // year
       case "y":
         // `00` through `99`
-        char = date.getFullYear().toString().substr(2);
+        char = date.getFullYear().toString().slice(-2);
         break;
       case "Y":
         // Examples: `1988` or `2015`
@@ -162,9 +163,9 @@ export function format(date: Date, format: string): string {
 
         offset = Math.abs(offset);
 
-        char += ("0" + (~~(offset / 60)).toString()).slice(-2);
+        char += (~~(offset / 60)).toString().padStart(2, "0");
         char += ":";
-        char += ("0" + (offset % 60).toString()).slice(-2);
+        char += (offset % 60).toString().padStart(2, "0");
 
         break;
       }