/*
* 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 = -lrtall:$(TARGET) @echo "Build OK!"$(TARGET):$(OBJECT) $(CC) $^ -o $@ $(LIB)%.o:%.c $(CC) -g $< -c -o $@ .PHONY:cleanclean: rm -rf $(TARGET) rm -rf $(OBJECT) @echo "Clean up!"