Step to UEFI (13) ----- EADK struct tm

EADK 中的时间,还有一种定义(更准确的说是C标准库中的定义),可以在 \EadkPkg_A2\StdLib\Include\time.h 看到

/** A structure holding the components of a calendar time, called the
    broken-down time.  The first nine (9) members are as mandated by the
    C95 standard.  Additional fields have been added for EFI support.
**/
struct tm {
  int     tm_year;      // years since 1900
  int     tm_mon;       // months since January ?[0, 11]
  int     tm_mday;      // day of the month ?[1, 31]
  int     tm_hour;      // hours since midnight ?[0, 23]
  int     tm_min;       // minutes after the hour ?[0, 59]
  int     tm_sec;       // seconds after the minute ?[0, 60]
  int     tm_wday;      // days since Sunday ?[0, 6]
  int     tm_yday;      // days since January 1 ?[0, 365]
  int     tm_isdst;     // Daylight Saving Time flag
  int     tm_zoneoff;   // EFI TimeZone offset, -1440 to 1440 or 2047
  int     tm_daylight;  // EFI Daylight flags
  UINT32  tm_Nano;      // EFI Nanosecond value
};

 

有两个函数 localtime 和 mktime 可以将时间在 time_t 和 struct tm之间进行转换。

/** @file
    A simple, basic, application showing how the Hello application could be
    built using the "Standard C Libraries" from StdLib.

    Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
    This program and the accompanying materials
    are licensed and made available under the terms and conditions of the BSD License
    which accompanies this distribution. The full text of the license may be found at
    http://opensource.org/licenses/bsd-license.

    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
//#include  <Uefi.h>
//#include  <Library/UefiLib.h>
//#include  <Library/ShellCEntryLib.h>

#include  <stdio.h>
#include  <time.h>

/***
  Demonstrates basic workings of the main() function by displaying a
  welcoming message.

  Note that the UEFI command line is composed of 16-bit UCS2 wide characters.
  The easiest way to access the command line parameters is to cast Argv as:
      wchar_t **wArgv = (wchar_t **)Argv;

  @param[in]  Argc    Number of argument tokens pointed to by Argv.
  @param[in]  Argv    Array of Argc pointers to command line tokens.

  @retval  0         The application exited normally.
  @retval  Other     An error occurred.
***/
int
EFIAPI
main (
  IN int Argc,
  IN char **Argv
  )
{

  time_t t,y;
  struct tm *tb;
  
  time(&t);

  printf("%ld\n",t);

  tb=localtime(&t);
  
  printf("localtime: %s",asctime(tb));

  y=mktime(tb); 
  
  printf("mktime   : %s",ctime(&y));
  
  return 0;
}

 

运行结果

time2

参考:

1.http://ganquan.info/standard-c/function/localtime
2.http://ganquan.info/standard-c/function/mktime

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注