Example of tm use

孤人 提交于 2019-12-12 08:41:33

问题


Can you give an example of use of tm (I don't know how to initialize that struct) where the current date is written in this format y/m/d?


回答1:


How to use tm structure

  1. call time() to get current date/time as number of seconds since 1 Jan 1970.
  2. call localtime() to get struct tm pointer. If you want GMT them call gmtime() instead of localtime().

  3. Use sprintf() or strftime() to convert the struct tm to a string in any format you want.

Example

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

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

  strftime (buffer,80,"Now it's %y/%m/%d.",timeinfo);
  puts (buffer);

  return 0;
}

Example Output

Now it's 12/10/24

References:

  • struct tm
  • strftime


来源:https://stackoverflow.com/questions/13658756/example-of-tm-use

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!