定时器

sleep()函数

C++中头文件<windows.h>下的函数

  作用:延时,程序暂停若干时间。

  时间,就是他的参数,单位是毫秒。

  例如:

  Sleep (500) ; //注意第一个字母是大写。

  就是到这里停半秒,然后继续向下执行。

  ——————————————————

  在Linux C语言中 sleep的单位是秒

  sleep(5); //停5秒

  包含在 <unistd.h>头文件中

  ——————————————————

  • 实现延时的代码

#include "stdafx.h"
#include <iostream>
#include <ctime>  //或者c风格 #include <time.h>

#include <windows.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 time_t t1; //
 time_t t2;
 
 time(&t1);
 time(&t2);

 while (t1 - t2 < 0.0000001)
 {
  cout<<"dddd"<<endl;
  Sleep(100); //window.h下面的函数,单位毫秒
  time(&t1);
 }

}

关于time_t的问题:

C语言中time()函数

函数简介

  函数名: time

  头文件:time.h

  函数原型:time_t time(time_t * timer)

  功能: 获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。然后调用localtime将time_t所表示的CUT时间转换为本地时间(我们是+8区,比CUT多8个小时)并转成struct tm类型,该类型的各数据成员分别表示年月日时分秒。

  补充说明:time函数的原型也可以理解为 long time(long *tloc),即返回一个long型整数。因为在time.h这个头文件中time_t实际上就是:

  #ifndef _TIME_T_DEFINED

  typedef long time_t; /* time value */

  #define _TIME_T_DEFINED /* avoid multiple defines of time_t */

  #endif

  即long。

关于localtime函数:

函数名: localtime

简介

  功 能: 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为日历时间 。

  说明:此函数获得的tm结构体的时间,是已经进行过时区转化为本地时间。

  用 法: struct tm *localtime(const time_t *clock);

  返回值:返回指向tm 结构体的指针.tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体.


#include "stdafx.h"
#include <iostream>
#include <ctime>  //或者c风格 #include <time.h>

#include <windows.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 struct tm *tblock;
 time_t timer;
 timer = time(NULL);
 tblock = localtime(&timer);
 //cout<<*tblock<<endl; //这个有问题wanshang处理,现在忙
 cout<<"Local time is:"<<asctime(tblock)<<endl;
}

  • 关于asctime,将时间结构体转换为字符串类型输出的标准输出cout

asctime

  函数名: asctime
  功 能: 转换日期和时间为ASCII码
  用 法: char *asctime(const struct tm *tblock);
  程序例:
  #include <stdio.h>
  #include <string.h>
  #include <time.h>
  int main(void)
  {
  struct tm t;
  char str[80];
  /* sample loading of tm structure */
  t.tm_sec = 1; /* Seconds */
  t.tm_min = 30; /* Minutes */
  t.tm_hour = 9; /* Hour */
  t.tm_mday = 22; /* Day of the Month */
  t.tm_mon = 11; /* Month */
  t.tm_year = 56; /* Year - does not include century */
  t.tm_wday = 4; /* Day of the week */
  t.tm_yday = 0; /* Does not show in asctime */
  t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */
  /* converts structure to null terminated string */
  strcpy(str, asctime(&t));
  printf("%s\n", str);
  return 0;
  }
  英文详解:Synopsis
  #include <time.h>
  char *asctime(const struct tm *timeptr);
  Description
  The asctime function converts the broken-down time in the structure pointed to by timeptr into a string in the form
  Sun Sep 16 01:03:52 1973\n\0
  using the equivalent of the following algorithm.
  char *asctime(const struct tm *timeptr)
  {
  static const char wday_name[7][3] = {
  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  };
  static const char mon_name[12][3] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  static char result[26];
  sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
  wday_name[timeptr->tm_wday],
  mon_name[timeptr->tm_mon],
  timeptr->tm_mday, timeptr->tm_hour,
  timeptr->tm_min, timeptr->tm_sec,
  1900 + timeptr->tm_year
  );
  return result;
  }
  Returns
  The asctime function returns a pointer to the string.
  Example :
  #include <stdio.h>
  #include <time.h>
  void main(void)
  {
  struct tm *newtime;
  time_t ltime;
  time (&ltime); /* Get the time in seconds */
  newtime = localtime(&ltime); /* convert it to the structure tm */
  printf("The current time and date are %s", asctime(newtime));/* print the local time as a string */
  }
  The current time and date are Mon Dec 28 12:33:50 1998

posted @ 2011-08-19 13:07  wanglc_work  Views(354)  Comments(0Edit  收藏  举报