所有时间测试函数
/*
* timeTest.c
*
* Created on: Aug 28, 2013
* Author: zsf
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc,char *argv[]){
#if 0
//获取时间
{
//取得S级的时间
time_t time_s=0;
struct tm *nowtm=NULL;
char buf[64]={0};
char tmbuf[64] = {0};
if(-1==time (&time_s)){
perror("time error"); //获取当前时间
}
printf ("time is [%d]\n", (int)time_s);
printf ( "The current local time: %s", ctime(&time_s));
if(NULL==ctime_r(&time_s,tmbuf)){
perror("asctime error!");
}
printf ( "The current local time: %s",tmbuf);
printf("----------------------------------------------------------------------\n");
//0时区时间
struct tm lasttm;
bzero(&lasttm,sizeof(struct tm));
nowtm=gmtime(&time_s);
//用户自定义格式输出时间
strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",nowtm);
printf ( "The current local time: %s(s)\n",buf);
if(NULL==gmtime_r(&time_s,&lasttm)){
perror("asctime error!");
}
printf ( "The current local time: %s",asctime(&lasttm));
bzero(&lasttm,sizeof(struct tm));
strptime(buf,"%Y-%m-%d %H:%M:%S",&lasttm);
printf ( "The current local time: %s", asctime(&lasttm));
printf("----------------------------------------------------------------------\n");
//当前时区时间,内核时区未设置时亦取0时区时间
nowtm=NULL;
nowtm=localtime(&time_s);
//用户自定义格式输出时间
strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",nowtm);
printf ( "The current local time: %s(s)\n",buf);
bzero(&lasttm,sizeof(struct tm));
strptime(buf,"%Y-%m-%d %H:%M:%S",&lasttm);
printf ( "The current local time: %s", asctime(&lasttm));
printf("----------------------------------------------------------------------\n");
//取得us级的时间
time_t time_ms=0;
struct timeval tv;
memset(&tmbuf,sizeof(tmbuf),0);
memset(&buf,sizeof(buf),0);
bzero(&tv,0);
if(gettimeofday(&tv,NULL)){
perror("gettimeofday error!");
}
printf ("time 秒 is [%d]\n", (int)tv.tv_sec);
printf ("time 微秒 is [%d]\n", (int)tv.tv_usec);
#if 0
long int ts;
ts = tv.tv_sec*1000000+ tv.tv_usec;
ts /=1000000;
printf ( "The current local time: %s",ctime(&ts));
#endif
printf ( "The current local time: %s",ctime(&tv.tv_sec));
nowtm = localtime(&tv.tv_sec);
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
//将毫秒值追加到 strftime 转换的字符串末尾
snprintf(buf, sizeof(buf), "%s.%06d", tmbuf, (int)tv.tv_usec);
printf("The current local time is %s(s)\n",tmbuf);
printf("The current local time is %s(us)\n",buf);
//取得纳秒级的时间
struct timespec tp;
bzero(&tp,sizeof(tp));
//系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他改变,则对应的时间相应改变
if(clock_gettime(CLOCK_REALTIME, &tp)){
perror("clock_gettime error!");
}
nowtm = NULL;
nowtm = localtime(&tp.tv_sec);
memset(&tmbuf,sizeof(tmbuf),0);
memset(&buf,sizeof(buf),0);
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
snprintf(buf, sizeof(buf), "%s.%06d", tmbuf, (int)tp.tv_nsec);
printf("The current local time is %s(s)\n",tmbuf);
printf("The current local time is %s(ns)\n",buf);
//CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
bzero(&tp,sizeof(tp));
if(clock_gettime(CLOCK_MONOTONIC, &tp)){
perror("clock_gettime error!");
}
nowtm = NULL;
nowtm = localtime(&tp.tv_sec);
memset(&tmbuf,sizeof(tmbuf),0);
memset(&buf,sizeof(buf),0);
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
snprintf(buf, sizeof(buf), "%s.%06d", tmbuf,(int) tp.tv_nsec);
printf("The current local time is %s(s)\n",tmbuf);
printf("The current local time is %s(ns)\n",buf);
}
#endif
#if 0
CLOCK_REALTIME - can jump - can slew - if ntp is running this clock is always kept close to GMT. even if hardware is not 100% correct, ntp will correct everything over time.
CLOCK_MONOTONIC - cannot jump - can slew !!! (because of ntp) - it is not kept in sync with GMT. but the "speed" of seconds is kept in sync with GMT by varying it constantly by ntp.
CLOCK_MONOTONIC_RAW - cannot jump - cannot slew ! - the speed of seconds is not the same as the speed of GMT seconds since the hardware timer is never 100% exact and ntp daemon does NOT have influence here
#endif
#if 1
{
#if 0
if(argc<7){
printf("USage Year,Month,Day,Hour,Minute,Second\n");
printf("Example: ./test 2013 8 23 12 30 30\n");
printf("this example time is 2013-8-23 12:30:30\n");
}
struct tm nowtm;
time_t tp;
bzero(&nowtm,sizeof(struct tm));
nowtm.tm_year = atoi(argv[1]);
nowtm.tm_mon = atoi(argv[2]);
nowtm.tm_mday = atoi(argv[3]);
nowtm.tm_hour = atoi(argv[4]);
nowtm.tm_min = atoi(argv[5]);
nowtm.tm_sec = atoi(argv[6]);
#endif
struct tm nowtm;
time_t tp;
#if 1
//获取当前时间到struct tm中
bzero(&nowtm,sizeof(struct tm));
if(-1==time(&tp)){
perror("time error!");
}
if(NULL==localtime_r(&tp,&nowtm)){
perror("localtime error!");
}
printf ( "The current local time: %s", asctime(&nowtm));
#endif
#if 0
//用户指定时间设置
char tmbuf[64] = {"2013-5-22 13:47:50"};
bzero(&nowtm,sizeof(struct tm));
if(NULL==strptime(tmbuf,"%Y-%m-%d %H:%M:%S",&nowtm)){
perror("strftime error!\n");
}
printf("day is %d\n",nowtm.tm_mday);
#endif
#if 1
tp = mktime(&nowtm);
if(-1==tp){
perror("mktime error!");
}
//取得系统设置时间的权限比如sudo 或是切换到root用户
if(-1==stime(&tp)){
perror("stime error!");
}
if(-1==time(&tp)){
perror("time error!");
}
printf ( "The current local time: %s", ctime(&tp));
#endif
#if 1
struct timeval tv;
bzero(&tv,sizeof(struct timeval));
tv.tv_sec=tp;
//取得系统设置时间的权限比如sudo 或是切换到root用户
if((-1==settimeofday(&tv,NULL))){
perror("settimeofday error!\n");
}
if(-1==time(&tp)){
perror("time error!");
}
printf ( "The current local time: %s", ctime(&tp));
#endif
#if 0
//以硬件时间为准,设置系统时间
system("hwclock -s");
//以系统时间为准,设置硬件时间
system("hwclock -w");
//显示硬件时间
system("hwclock --show");
return 0;
#endif
}
#endif
}
* timeTest.c
*
* Created on: Aug 28, 2013
* Author: zsf
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc,char *argv[]){
#if 0
//获取时间
{
//取得S级的时间
time_t time_s=0;
struct tm *nowtm=NULL;
char buf[64]={0};
char tmbuf[64] = {0};
if(-1==time (&time_s)){
perror("time error"); //获取当前时间
}
printf ("time is [%d]\n", (int)time_s);
printf ( "The current local time: %s", ctime(&time_s));
if(NULL==ctime_r(&time_s,tmbuf)){
perror("asctime error!");
}
printf ( "The current local time: %s",tmbuf);
printf("----------------------------------------------------------------------\n");
//0时区时间
struct tm lasttm;
bzero(&lasttm,sizeof(struct tm));
nowtm=gmtime(&time_s);
//用户自定义格式输出时间
strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",nowtm);
printf ( "The current local time: %s(s)\n",buf);
if(NULL==gmtime_r(&time_s,&lasttm)){
perror("asctime error!");
}
printf ( "The current local time: %s",asctime(&lasttm));
bzero(&lasttm,sizeof(struct tm));
strptime(buf,"%Y-%m-%d %H:%M:%S",&lasttm);
printf ( "The current local time: %s", asctime(&lasttm));
printf("----------------------------------------------------------------------\n");
//当前时区时间,内核时区未设置时亦取0时区时间
nowtm=NULL;
nowtm=localtime(&time_s);
//用户自定义格式输出时间
strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",nowtm);
printf ( "The current local time: %s(s)\n",buf);
bzero(&lasttm,sizeof(struct tm));
strptime(buf,"%Y-%m-%d %H:%M:%S",&lasttm);
printf ( "The current local time: %s", asctime(&lasttm));
printf("----------------------------------------------------------------------\n");
//取得us级的时间
time_t time_ms=0;
struct timeval tv;
memset(&tmbuf,sizeof(tmbuf),0);
memset(&buf,sizeof(buf),0);
bzero(&tv,0);
if(gettimeofday(&tv,NULL)){
perror("gettimeofday error!");
}
printf ("time 秒 is [%d]\n", (int)tv.tv_sec);
printf ("time 微秒 is [%d]\n", (int)tv.tv_usec);
#if 0
long int ts;
ts = tv.tv_sec*1000000+ tv.tv_usec;
ts /=1000000;
printf ( "The current local time: %s",ctime(&ts));
#endif
printf ( "The current local time: %s",ctime(&tv.tv_sec));
nowtm = localtime(&tv.tv_sec);
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
//将毫秒值追加到 strftime 转换的字符串末尾
snprintf(buf, sizeof(buf), "%s.%06d", tmbuf, (int)tv.tv_usec);
printf("The current local time is %s(s)\n",tmbuf);
printf("The current local time is %s(us)\n",buf);
//取得纳秒级的时间
struct timespec tp;
bzero(&tp,sizeof(tp));
//系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他改变,则对应的时间相应改变
if(clock_gettime(CLOCK_REALTIME, &tp)){
perror("clock_gettime error!");
}
nowtm = NULL;
nowtm = localtime(&tp.tv_sec);
memset(&tmbuf,sizeof(tmbuf),0);
memset(&buf,sizeof(buf),0);
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
snprintf(buf, sizeof(buf), "%s.%06d", tmbuf, (int)tp.tv_nsec);
printf("The current local time is %s(s)\n",tmbuf);
printf("The current local time is %s(ns)\n",buf);
//CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
bzero(&tp,sizeof(tp));
if(clock_gettime(CLOCK_MONOTONIC, &tp)){
perror("clock_gettime error!");
}
nowtm = NULL;
nowtm = localtime(&tp.tv_sec);
memset(&tmbuf,sizeof(tmbuf),0);
memset(&buf,sizeof(buf),0);
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
snprintf(buf, sizeof(buf), "%s.%06d", tmbuf,(int) tp.tv_nsec);
printf("The current local time is %s(s)\n",tmbuf);
printf("The current local time is %s(ns)\n",buf);
}
#endif
#if 0
CLOCK_REALTIME - can jump - can slew - if ntp is running this clock is always kept close to GMT. even if hardware is not 100% correct, ntp will correct everything over time.
CLOCK_MONOTONIC - cannot jump - can slew !!! (because of ntp) - it is not kept in sync with GMT. but the "speed" of seconds is kept in sync with GMT by varying it constantly by ntp.
CLOCK_MONOTONIC_RAW - cannot jump - cannot slew ! - the speed of seconds is not the same as the speed of GMT seconds since the hardware timer is never 100% exact and ntp daemon does NOT have influence here
#endif
#if 1
{
#if 0
if(argc<7){
printf("USage Year,Month,Day,Hour,Minute,Second\n");
printf("Example: ./test 2013 8 23 12 30 30\n");
printf("this example time is 2013-8-23 12:30:30\n");
}
struct tm nowtm;
time_t tp;
bzero(&nowtm,sizeof(struct tm));
nowtm.tm_year = atoi(argv[1]);
nowtm.tm_mon = atoi(argv[2]);
nowtm.tm_mday = atoi(argv[3]);
nowtm.tm_hour = atoi(argv[4]);
nowtm.tm_min = atoi(argv[5]);
nowtm.tm_sec = atoi(argv[6]);
#endif
struct tm nowtm;
time_t tp;
#if 1
//获取当前时间到struct tm中
bzero(&nowtm,sizeof(struct tm));
if(-1==time(&tp)){
perror("time error!");
}
if(NULL==localtime_r(&tp,&nowtm)){
perror("localtime error!");
}
printf ( "The current local time: %s", asctime(&nowtm));
#endif
#if 0
//用户指定时间设置
char tmbuf[64] = {"2013-5-22 13:47:50"};
bzero(&nowtm,sizeof(struct tm));
if(NULL==strptime(tmbuf,"%Y-%m-%d %H:%M:%S",&nowtm)){
perror("strftime error!\n");
}
printf("day is %d\n",nowtm.tm_mday);
#endif
#if 1
tp = mktime(&nowtm);
if(-1==tp){
perror("mktime error!");
}
//取得系统设置时间的权限比如sudo 或是切换到root用户
if(-1==stime(&tp)){
perror("stime error!");
}
if(-1==time(&tp)){
perror("time error!");
}
printf ( "The current local time: %s", ctime(&tp));
#endif
#if 1
struct timeval tv;
bzero(&tv,sizeof(struct timeval));
tv.tv_sec=tp;
//取得系统设置时间的权限比如sudo 或是切换到root用户
if((-1==settimeofday(&tv,NULL))){
perror("settimeofday error!\n");
}
if(-1==time(&tp)){
perror("time error!");
}
printf ( "The current local time: %s", ctime(&tp));
#endif
#if 0
//以硬件时间为准,设置系统时间
system("hwclock -s");
//以系统时间为准,设置硬件时间
system("hwclock -w");
//显示硬件时间
system("hwclock --show");
return 0;
#endif
}
#endif
}
CC = gcc
AR =
LD =
TARGET = time
OBJECT :=$(patsubst %.c,%.o,$(wildcard ./*.c))
LIB = -lrt
all:$(TARGET)
@echo "Build OK!"
$(TARGET):$(OBJECT)
$(CC) $^ -o $@ $(LIB)
%.o:%.c
$(CC) -g $< -c -o $@
.PHONY:clean
clean:
rm -rf $(TARGET)
rm -rf $(OBJECT)
@echo "Clean up!"
AR =
LD =
TARGET = time
OBJECT :=$(patsubst %.c,%.o,$(wildcard ./*.c))
LIB = -lrt
all:$(TARGET)
@echo "Build OK!"
$(TARGET):$(OBJECT)
$(CC) $^ -o $@ $(LIB)
%.o:%.c
$(CC) -g $< -c -o $@
.PHONY:clean
clean:
rm -rf $(TARGET)
rm -rf $(OBJECT)
@echo "Clean up!"