用C获得当前系统时间(转)
#include <stdio.h>
#include <time.h>
void main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\007The current date/time is: %s", asctime (timeinfo) );
exit(0);
}
=================
#include <time.h> -- 必须的时间函数头文件
time_t -- 时间类型(time.h 定义)
struct tm -- 时间结构,time.h 定义如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
time ( &rawtime ); -- 获取时间,以秒计,从1970年1月一日起算,存于rawtime
localtime ( &rawtime ); -- 转为当地时间,tm 时间结构
asctime ()-- 转为标准ASCII时间格式:
星期 月 日 时:分:秒 年
=========================================
你要的格式可这样输出:
printf ( "%4d-%02d-%02d %02d:%02d:%02d\n",1900+timeinfo->tm_year, 1+timeinfo->tm_mon,
timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
就是直接打印tm,tm_year 从1900年计算,所以要加1900,
月tm_mon,从0计算,所以要加1
Example
/* TIMES.C illustrates various time and date functions including:
* time _ftime ctime asctime
* localtime gmtime mktime _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();
/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n",
asctime( today ) + 11, ampm );
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:\t%u\n",
tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
printf( "Daylight savings:\t\t\t%s\n",
tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}
Output
OS time: 21:51:03
OS date: 05/03/94
Time in seconds since UTC 1/1/70: 768027063
UNIX time and date: Tue May 03 21:51:03 1994
Coordinated universal time: Wed May 04 04:51:03 1994
12-hour time: 09:51:03 PM
Plus milliseconds: 279
Zone difference in seconds from UTC: 480
Time zone name:
Daylight savings: YES
Christmas Sat Dec 25 12:00:00 1993
Today is Tuesday, day 03 of May in the year 1994.
用C获得当前系统时间(转)的更多相关文章
- MySQL设置字段的默认值为当前系统时间
问题产生: 当我们在对某个字段进行设置时间默认值,该默认值必须是的当前记录的插入时间,那么就将当前系统时间作为该记录创建的时间. 应用场景: 1.在数据表中,要记录每条数据是什么时候创建的,应该由数据 ...
- 【转】PHP中获取当前系统时间、时间戳
今天写下otime($time, $now)为将时间格式转为时间戳,$time为必填.清楚了这个,想了解更多,请继续往下看. 3. date($format)用法比如:echo date('Y-m-d ...
- C语言系统时间读取
1 读出系统时间(每隔一秒)#include#includeint main(){ while(1) { time_t t; t= time(0); struct tm *p; ...
- 使用AIDL调用远程服务设置系统时间
在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的.于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间 ...
- python有超时的windows系统时间设置代码
手边的笔记本用久了,cmos电池可能又没电了.每次开机时间都不对,导致访问一些有https的网页会出现警告信息. 于是找了找通过python脚本设置系统时间的方法,发现了两种,其一是调用socket直 ...
- JAVA中获取当前系统时间及格式转换
JAVA中获取当前系统时间 一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...
- 如何查看、修改Linux的系统时间
如题: 如何修改Linux的系统时间? date -s 05/27/2011 date -s 10:24:00 clock -w 就这三条命令就OK了! 查看/修改Linux时区和时间 一.时区 1. ...
- crontab执行时间和系统时间不一致
最近发现一个非常奇怪的问题,症状如下: crontab 定时任务 配置时间 是 10 5 * * * (每日凌晨5点10分执行) 运行脚本时间却是 18:10左右 Dec 24 05:10:01 ht ...
- freebsd 系统时间
http://blog.csdn.net/wowoto/article/details/5557810 https://www.douban.com/note/150233427/ date #查看当 ...
- ubuntu下的时间设定(硬件时间,系统时间,本地时间)
问题的来由是在这里: 在cron里设定任务是在凌晨6点执行,检查日志时发现时间总是不对,是在22点左右的时间执行的.研究发现,任务是在本地时间的6点执行了,但不知为什么syslog中的时间都是为utc ...
随机推荐
- jQuery Plugin Docs
遮罩:BlockUI 对话框:Dialog 输入框:Mask Plugin 下拉框: 1.自定义的实现思路:将下拉框的选项定义为全局变量(数组),在html中定义点击事件并将数组变量转给响应函数,每次 ...
- poj 3126
一道搜索的水题,其实搜索相对其他的来说好掌握一点,因为有个固定的模板可以用去套 题目大意就是数字的变化,一个数字只可以变化到它最相邻的一个素数上去,意思是只变化一位数字,求最短的变化方案 #inclu ...
- CEF3开发者系列之类和接口
CEF3基本的框架包含C/C++程 序接口,通过本地库的接口来实现,而这个库则会隔离宿主程序和 Chromium&Webkit的操作细节.它在浏览器控件和宿主程序之间提供紧密的整合,它支持用户 ...
- suse linux 10 下配置vpn服务器(pptp)
一.安装所需的软件包: pptpd-*.rpm ppp-*.rpm pptp-*.rpm 一般情况下系统已经将pptp和ppp包安装好了,所以只需安装pptpd ...
- java基础学习05(面向对象基础01)
面向对象基础01 1.理解面向对象的概念 2.掌握类与对象的概念3.掌握类的封装性4.掌握类构造方法的使用 实现的目标 1.类与对象的关系.定义.使用 2.对象的创建格式,可以创建多个对象3.对象的内 ...
- 【Python】range和xrange区别
转自:http://www.cnblogs.com/zhangjing0502/archive/2012/05/16/2503880.html range 函数说明:range([start,] ...
- CCF 节日
问题描述 有一类节日的日期并不是固定的,而是以"a月的第b个星期c"的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日. 现在,给你a,b,c和y1, y2(1850 ≤ ...
- 编写一个程序,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值
编写一个程序,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值 1 #import <Foundation/Foundation.h> 2 3 int main( ...
- iOS中UITableView的一些设置
不可滑动: ? 1 tableView.userInteractionEnabled = NO; 也可以在storyboard中的userInteractionEnable属性设置 显示导向箭头: ? ...
- 【SSO单点系列】(1):CAS4.0 环境的搭建
一.概述 今天开始写CAS相关的第一篇文章,这篇文章主要是关于CAS环境的搭配,提供给刚刚接触CAS的一个入门指南,并演示一个CAS的最简单的实例 二.环境要求 博主的环境如下: win8.1 64 ...