tm 使用示例 [英] Example of tm use

查看:29
本文介绍了tm 使用示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能举一个使用tm(我不知道如何初始化那个struct)的例子,其中当前日期以这种格式写入y/m/d?

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?

推荐答案

如何使用tm结构

  1. 调用 time() 以获取当前日期/时间,作为自 1970 年 1 月 1 日以来的秒数.
  2. 调用localtime()获取struct tm指针.如果你想要格林威治标准时间,他们调用 gmtime() 而不是 localtime().

  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().

使用 sprintf()strftime() 将 struct tm 转换为您想要的任何格式的字符串.

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

示例

#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;
}

示例输出

Now it's 12/10/24

参考:

  • struct tm
  • strftime
  • 这篇关于tm 使用示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆