]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: Fixes for timezone handling and 'date -sfo'
authorChris Phillips <chrisp@hp.com>
Tue, 19 Aug 2014 23:05:44 +0000 (23:05 +0000)
committerjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 19 Aug 2014 23:05:44 +0000 (23:05 +0000)
- Update 'date -sfo' format to match UEFI Shell 2.1 spec
- Fixes to correctly initialize Second
- Set correct sign when setting timezone with the 'time -tz' command.  Now matches UEFI spec calculation of "Localtime = UTC - TimeZone"
- Display "LOCAL" when TimeZone == EFI_UNSPECIFIED_TIMEZONE
- Allow a timezone of '_local' to be provided by user
- Better invalid command line checking

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Chris Phillips <chrisp@hp.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15840 6f19259b-4bc3-4df7-8a09-765794883524

ShellPkg/Library/UefiShellLevel2CommandsLib/TimeDate.c
ShellPkg/Library/UefiShellLevel2CommandsLib/UefiShellLevel2CommandsLib.uni

index b8535139dbd4b03d8da5b49c89ee0128732c226c..24dcae46825e0d7d896025f0d5a77d7531ca2b11 100644 (file)
@@ -1,6 +1,7 @@
 /** @file\r
   Main file for time, timezone, and date shell level 2 and shell level 3 functions.\r
 \r
+  (C) Copyright 2012-2014, Hewlett-Packard Development Company, L.P.\r
   Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
@@ -230,7 +231,13 @@ ShellCommandRunDate (
         // ShellPrintEx the date in SFO or regular format\r
         //\r
         if (ShellCommandLineGetFlag(Package, L"-sfo")) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DATE_SFO_FORMAT), gShellLevel2HiiHandle, TheTime.Month, TheTime.Day, TheTime.Year);\r
+          //\r
+          // Match UEFI Shell spec:\r
+          // ShellCommand,"date"\r
+          // Date,"DD","MM","YYYY"\r
+          //\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_SFO_HEADER), gShellLevel2HiiHandle, L"date");\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DATE_SFO_FORMAT), gShellLevel2HiiHandle, TheTime.Day, TheTime.Month, TheTime.Year);\r
         } else {\r
           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DATE_FORMAT), gShellLevel2HiiHandle, TheTime.Month, TheTime.Day, TheTime.Year);\r
         }\r
@@ -338,21 +345,35 @@ CheckAndSetTime (
     Walker2          = Walker1!=NULL?StrStr(Walker1, L":"):NULL;\r
     if (Walker2 != NULL && *Walker2 == L':') {\r
       *Walker2 = CHAR_NULL;\r
+      TheTime.Second = (UINT8)0;\r
+    }\r
+    else if (Walker2 == NULL) {\r
+      TheTime.Second = (UINT8)0;\r
     }\r
     if (Walker1 != NULL && Walker1[0] != CHAR_NULL) {\r
       TheTime.Minute = (UINT8)ShellStrToUintn (Walker1);\r
       if (Walker2 != NULL) {\r
         Walker1 = Walker2 + 1;\r
-      }\r
-      if (Walker1 != NULL && Walker1[0] != CHAR_NULL) {\r
-        TheTime.Second = (UINT8)ShellStrToUintn (Walker1);\r
+        if (Walker1 != NULL && Walker1[0] != CHAR_NULL) {\r
+          TheTime.Second = (UINT8)ShellStrToUintn (Walker1);\r
+        }\r
       }\r
     }\r
     SHELL_FREE_NON_NULL(TimeStringCopy);\r
   }\r
 \r
 \r
-  if ((Tz >= -1440 && Tz <= 1440)||(Tz == 0x7FF)) {\r
+  if (Tz >= -1440 && Tz <= 1440) {\r
+    //\r
+    // EFI_TIME TimeZone is stored to meet the following calculation (see UEFI Spec):\r
+    // Localtime = UTC - TimeZone\r
+    // This means the sign must be changed for the user provided Tz.\r
+    // EX: User wants to set TimeZone to Pacific Standard Time, so runs\r
+    // time -tz -480 # set to UTC-08:00\r
+    // To meet the calculation, the sign must be changed.\r
+    //\r
+    TheTime.TimeZone = -Tz;\r
+  } else if (Tz == EFI_UNSPECIFIED_TIMEZONE) {\r
     TheTime.TimeZone = Tz;\r
   }\r
 \r
@@ -452,40 +473,61 @@ ShellCommandRunTime (
           TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
         }\r
 \r
-        ShellPrintHiiEx (\r
-          -1,\r
-          -1,\r
-          NULL,\r
-          STRING_TOKEN (STR_TIME_FORMAT),\r
-          gShellLevel2HiiHandle,\r
-          TheTime.Hour,\r
-          TheTime.Minute,\r
-          TheTime.Second,\r
-          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?L" ":(TheTime.TimeZone > 0?L"-":L"+"),\r
-          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
-          TzMinutes\r
-         );\r
-         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), gShellLevel2HiiHandle);\r
+        if (TheTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r
+          ShellPrintHiiEx (\r
+            -1,\r
+            -1,\r
+            NULL,\r
+            STRING_TOKEN (STR_TIME_FORMAT),\r
+            gShellLevel2HiiHandle,\r
+            TheTime.Hour,\r
+            TheTime.Minute,\r
+            TheTime.Second,\r
+            (TheTime.TimeZone > 0?L"-":L"+"),\r
+            ((ABS(TheTime.TimeZone)) / 60),\r
+            TzMinutes\r
+            );\r
+        } else {\r
+          ShellPrintHiiEx (\r
+            -1,\r
+            -1,\r
+            NULL,\r
+            STRING_TOKEN (STR_TIME_FORMAT_LOCAL),\r
+            gShellLevel2HiiHandle,\r
+            TheTime.Hour,\r
+            TheTime.Minute,\r
+            TheTime.Second\r
+            );\r
+        }\r
+        ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), gShellLevel2HiiHandle);\r
       } else if (ShellCommandLineGetFlag(Package, L"-d") && ShellCommandLineGetValue(Package, L"-d") == NULL) {\r
         if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
-          TzMinutes = 0;\r
+          ShellPrintHiiEx (\r
+            -1,\r
+            -1,\r
+            NULL,\r
+            STRING_TOKEN (STR_TIME_FORMAT_LOCAL),\r
+            gShellLevel2HiiHandle,\r
+            TheTime.Hour,\r
+            TheTime.Minute,\r
+            TheTime.Second\r
+            );\r
         } else {\r
           TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
+          ShellPrintHiiEx (\r
+            -1,\r
+            -1,\r
+            NULL,\r
+            STRING_TOKEN (STR_TIME_FORMAT),\r
+            gShellLevel2HiiHandle,\r
+            TheTime.Hour,\r
+            TheTime.Minute,\r
+            TheTime.Second,\r
+            (TheTime.TimeZone > 0?L"-":L"+"),\r
+            ((ABS(TheTime.TimeZone)) / 60),\r
+            TzMinutes\r
+           );\r
         }\r
-\r
-        ShellPrintHiiEx (\r
-          -1,\r
-          -1,\r
-          NULL,\r
-          STRING_TOKEN (STR_TIME_FORMAT),\r
-          gShellLevel2HiiHandle,\r
-          TheTime.Hour,\r
-          TheTime.Minute,\r
-          TheTime.Second,\r
-          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?L" ":(TheTime.TimeZone > 0?L"-":L"+"),\r
-          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
-          TzMinutes\r
-         );\r
           switch (TheTime.Daylight) {\r
             case 0:\r
               ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TIME_DST0), gShellLevel2HiiHandle);\r
@@ -511,10 +553,32 @@ ShellCommandRunTime (
           // perform level 3 operation here.\r
           //\r
           if ((TempLocation = ShellCommandLineGetValue(Package, L"-tz")) != NULL) {\r
-            if (TempLocation[0] == L'-') {\r
-              Tz = (INT16)(0 - ShellStrToUintn(++TempLocation));\r
+            if (StrniCmp (TempLocation, L"_local", StrLen (TempLocation)) == NULL) {\r
+              Tz = EFI_UNSPECIFIED_TIMEZONE;\r
+            } else if (TempLocation[0] == L'-') {\r
+\r
+              Tz = (INT16) ShellStrToUintn (++TempLocation);\r
+              //\r
+              // When the argument of "time [-tz tz]" is not numeric, ShellStrToUintn() returns "-1".\r
+              // Here we can detect the argument error by checking the return of ShellStrToUintn().\r
+              //\r
+              if (Tz == -1) {\r
+                Tz = 1441; //make it to be out of bounds value\r
+              } else {\r
+                Tz *= (-1); //sign convert\r
+              }\r
             } else {\r
-              Tz = (INT16)ShellStrToUintn(TempLocation);\r
+              if (TempLocation[0] == L'+') {\r
+                Tz = (INT16)ShellStrToUintn (++TempLocation);\r
+              } else {\r
+                Tz = (INT16)ShellStrToUintn (TempLocation);\r
+              }\r
+              //\r
+              // Detect the return of ShellStrToUintn() to make sure the argument is valid.\r
+              //\r
+              if (Tz == -1) {\r
+                Tz = 1441; //make it to be out of bounds value\r
+              }\r
             }\r
             if (!(Tz >= -1440 && Tz <= 1440) && Tz != EFI_UNSPECIFIED_TIMEZONE) {\r
               ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"-tz");\r
@@ -529,6 +593,14 @@ ShellCommandRunTime (
           TempLocation = ShellCommandLineGetValue(Package, L"-d");\r
           if (TempLocation != NULL) {\r
             Daylight = (UINT8)ShellStrToUintn(TempLocation);\r
+            //\r
+            // The argument of "time [-d dl]" is unsigned, if the first character is '-',\r
+            // the argument is incorrect.  That's because ShellStrToUintn() will skip past\r
+            // any '-' sign and convert what's next, forgetting the sign is here.\r
+            //\r
+            if (TempLocation[0] == '-') {\r
+              Daylight = 0xff; //make it invalid = will not use\r
+            }\r
             if (Daylight != 0 && Daylight != 1 && Daylight != 3) {\r
               ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"-d");\r
               ShellStatus = SHELL_INVALID_PARAMETER;\r
@@ -614,7 +686,8 @@ STATIC CONST SHELL_PARAM_ITEM TimeZoneParamList3[] = {
     {-660 , STRING_TOKEN (STR_TIMEZONE_P11)},\r
     {-720 , STRING_TOKEN (STR_TIMEZONE_P12)},\r
     {-780 , STRING_TOKEN (STR_TIMEZONE_P13)},\r
-    {-840 , STRING_TOKEN (STR_TIMEZONE_P14)}\r
+    {-840 , STRING_TOKEN (STR_TIMEZONE_P14)},\r
+    {EFI_UNSPECIFIED_TIMEZONE, STRING_TOKEN (STR_TIMEZONE_LOCAL)}\r
 };\r
 \r
 /**\r
@@ -644,6 +717,20 @@ CheckAndSetTimeZone (
     return (SHELL_INVALID_PARAMETER);\r
   }\r
 \r
+  if (StrniCmp (TimeZoneString, L"_local", StrLen (TimeZoneString)) == NULL) {\r
+    Status = gRT->GetTime (&TheTime, NULL);\r
+    if (EFI_ERROR (Status)) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);\r
+      return (SHELL_DEVICE_ERROR);\r
+    }\r
+\r
+    TheTime.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    Status = gRT->SetTime (&TheTime);\r
+    if (!EFI_ERROR(Status)){\r
+      return (SHELL_SUCCESS);\r
+    }\r
+    return (SHELL_INVALID_PARAMETER);\r
+  }\r
   if (TimeZoneString != NULL && !InternalIsTimeLikeString(TimeZoneString, L':', 1, 1, TRUE)) {\r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
@@ -817,11 +904,7 @@ ShellCommandRunTimeZone (
               //\r
               // Print basic info only\r
               //\r
-              if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
-                TzMinutes = 0;\r
-              } else {\r
-                TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
-              }\r
+              TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
 \r
               ShellPrintHiiEx (\r
                 -1,\r
@@ -829,8 +912,8 @@ ShellCommandRunTimeZone (
                 NULL,\r
                 STRING_TOKEN(STR_TIMEZONE_SIMPLE),\r
                 gShellLevel2HiiHandle,\r
-                TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(TheTime.TimeZone > 0?L"-":L"+"),\r
-                TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
+                (TheTime.TimeZone > 0?L"-":L"+"),\r
+                (ABS(TheTime.TimeZone)) / 60,\r
                 TzMinutes);\r
             }\r
             Found = TRUE;\r
@@ -841,28 +924,45 @@ ShellCommandRunTimeZone (
           //\r
           // Print basic info only\r
           //\r
-          if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
-            TzMinutes = 0;\r
-          } else {\r
-            TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
-          }\r
+          TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
+\r
           ShellPrintHiiEx (\r
             -1,\r
             -1,\r
             NULL,\r
             STRING_TOKEN(STR_TIMEZONE_SIMPLE),\r
             gShellLevel2HiiHandle,\r
-            TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(TheTime.TimeZone > 0?L"-":L"+"),\r
-            TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
+            (TheTime.TimeZone > 0?L"-":L"+"),\r
+            (ABS(TheTime.TimeZone)) / 60,\r
             TzMinutes);\r
+\r
           if (ShellCommandLineGetFlag(Package, L"-f")) {\r
             ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_TIMEZONE_NI), gShellLevel2HiiHandle);\r
           }\r
         }\r
       } else {\r
         //\r
-        // TimeZone was EFI_UNSPECIFIED_TIMEZONE (unknown) from GetTime()\r
+        // TimeZone was EFI_UNSPECIFIED_TIMEZONE (local) from GetTime()\r
         //\r
+        if (ShellCommandLineGetFlag (Package, L"-f")) {\r
+          for ( LoopVar = 0\r
+              ; LoopVar < sizeof (TimeZoneList) / sizeof (TimeZoneList[0])\r
+              ; LoopVar++\r
+             ){\r
+            if (TheTime.TimeZone == TimeZoneList[LoopVar].TimeZone) {\r
+              //\r
+              //  Print all info about current time zone\r
+              //\r
+              ShellPrintHiiEx (-1, -1, NULL, TimeZoneList[LoopVar].StringId, gShellLevel2HiiHandle);\r
+              break;\r
+            } \r
+          }\r
+        } else {\r
+          //\r
+          // Print basic info only\r
+          //\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_TIMEZONE_SIMPLE_LOCAL), gShellLevel2HiiHandle);\r
+        }\r
       }\r
     }\r
   }\r
index 51ec7e12670005e92fa4722f367a9470df4e3ccb..bd0738d94f051e27e0201ecf2c9c2d777351b70e 100644 (file)
Binary files a/ShellPkg/Library/UefiShellLevel2CommandsLib/UefiShellLevel2CommandsLib.uni and b/ShellPkg/Library/UefiShellLevel2CommandsLib/UefiShellLevel2CommandsLib.uni differ