Linux時間操作(time、gettimeofday)
阿新 • • 發佈:2019-01-09
一、time函式
- #include <time.h>
- time_t time(time_t *calptr);
一旦取得這種以秒計的很大的時間值後,通常要呼叫另一個時間函式將其變換為人們可讀的時間和日期
#include <time.h>
//calendar time into a broken-down time expressed as UTC
struct tm *gmtime(const time_t *calptr);
//converts the calendar time to the local time, taking into account the local time zone and
//daylight saving time flag
struct tm *localtime(const time_t *calptr);
//converts it into a time_t value
time_t mktime(struct tm *tmptr);
struct tm { /* a broken-down time */
int tm_sec; /* seconds after the minute: [0 - 60] */
int tm_min; /* minutes after the hour: [0 - 59] */
int tm_hour; /* hours after midnight: [0 - 23] */
int tm_mday; /* day of the month: [1 - 31] */
int tm_mon; /* months since January: [0 - 11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday: [0 - 6] */
int tm_yday; /* days since January 1: [0 - 365] */
int tm_isdst; /* daylight saving time flag: <0, 0, >0 */
};
- char *
- char *ctime(const time_t *calptr);
- asctime()和ctime()函式產生形式的26位元組字串,這與date命令的系統預設輸出形式類似:
Tue Feb 10 18:27:38 2004/n/0
二、gettimeofday函式得到更精確的時間
- #include <sys/time.h>
- int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
- 第二個形參是基於平臺實現的,使用的時候最好用NULL
time_t tv_sec; /*** second ***/
susecond_t tv_usec; /*** microsecond 微妙***/
}
1秒=1000毫秒,
1毫秒=1000微秒,
1微妙=1000納秒,
1納秒=1000皮秒。
秒用s表現,毫秒用ms,微秒用μs表示,納秒用ns表示,皮秒用ps表示。
三、核心時間
核心有兩個重要的全域性變數:
unsigned long jiffies;
timeval xtime ;
jiffies 是記錄著從電腦開機到現在總共的"時鐘中斷"的次數。
檔案linux-2.6.24/kernel/timer.c
void do_timer(unsigned long ticks)
{
jiffies_64 += ticks;
update_times(ticks);
}
xtime 是從cmos電路或rtc晶片中取得的時間,一般是從某一歷史時刻開始到現在的時間。
這個就是所謂的"牆上時鐘walltimer",通過它可計算得出作業系統需要的日期時間,它的精確度是微秒。
xtime第一次賦值是在系統啟動時呼叫timekeeping_init或time_init進行的
再呼叫read_persistent_clock進一步呼叫get_rtc_time得到的
PS:在/proc/uptime裡面的兩個數字分別表示:
the uptime of the system(seconds),
and the amount of time spent in idle process(seconds).
四、程式碼示例
“UTC時間字串”與 “time函式返回值”互換
- int64_t TimeToUTC(char *time)
- {
- struct tm temp1;
- int rc;
- int year;
- int mon;
- int day;
- int hour;
- int min;
- int sec;
- rc = sscanf(time, "%4d-%2d-%2d %2d:%2d:%2d",
- &(temp1.tm_year),
- &(temp1.tm_mon),
- &(temp1.tm_mday),
- &(temp1.tm_hour),
- &(temp1.tm_min),
- &(temp1.tm_sec));
- sscanf(time, "%4d-%2d-%2d %2d:%2d:%2d",
- &year,
- &mon,
- &day,
- &hour,
- &min,
- &sec);
- if((rc<6)
- || (temp1.tm_year<1900) || (temp1.tm_year>2100)
- || (temp1.tm_mon<1) || (temp1.tm_mon>12)
- || (temp1.tm_mday<1) || (temp1.tm_mday>31)
- || (temp1.tm_hour<0) || (temp1.tm_hour>23)
- || (temp1.tm_min<0) || (temp1.tm_min>59)
- || (temp1.tm_sec<0) || (temp1.tm_sec>59))
- {
- return -1;
- }
- temp1.tm_mon -= 1;
- temp1.tm_year -= 1900;
- struct tm temp3;
- time_t temp2 = mktime(&temp1);
- if (temp2 == -1){
- return -1;
- }
- else{
- localtime_r(&temp2, &temp3);
- if (!((mon == (temp3.tm_mon+1))
- && (day == temp3.tm_mday)
- &&(year == (temp3.tm_year+1900)))){
- return -1;
- }
- else{
- return temp2*1000000LL;
- }
- }
- }
- void UTCToTime(int64_t utc, char * clock)
- {
- struct tm temp1;
- time_t sec;
- float msec;
- int rc;
- sec = utc/1000000;
- msec = utc/1000000.0-sec;
- localtime_r(&sec, &temp1);
- temp1.tm_year += 1900;
- temp1.tm_mon += 1;
- rc = sprintf(clock, "%04d-%02d-%02d %02d:%02d:%02d",
- temp1.tm_year,
- temp1.tm_mon,
- temp1.tm_mday,
- temp1.tm_hour,
- temp1.tm_min,
- temp1.tm_sec);
- clock[rc] = '/0';
- }
生成Date
- static char * g_weekstr[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
- static char * g_monthstr[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
- /*generate the current date string*/
- time_t now = time(NULL);
- struct tm tt;
- gmtime_r( &now, &tt );
- char timebuf[64];
- snprintf( timebuf, sizeof(timebuf),
- "%s, %02d %s %d %02d:%02d:%02d GMT",
- g_weekstr[tt.tm_wday], tt.tm_mday,
- g_monthstr[tt.tm_mon], tt.tm_year + 1900,
- tt.tm_hour, tt.tm_min, tt.tm_sec );
------------------------------ 華麗的分割線 ------------------------------------
關於scanf的返回值
Both scanf and wscanf return the number of fields successfully converted
and assigned; the return value does not include fields that were read but
not assigned. A return value of 0 indicates that no fields were assigned.
The return value is EOF for an error or if the end-of-file character or the
end-of-string character is nocountered in the first attempt to read a character.
如:scanf("%d%d", &a, &b);
如果a和b都被成功讀入,那麼scanf的返回值就是2
如果只有a被成功讀入,返回值為1
如果a和b都未被成功讀入,返回值為0
如果遇到錯誤或遇到end of file,返回值為EOF。
- void main()
- {
- int a;
- int b;
- int c;
- int x;
- printf("請輸入三個整數:");
- x=scanf("%d%d%d",&a,&b,&c);
- printf("%d/n%d/n",a,x);
- }
- 輸入三個整數:5 6 7則x的值為3;
- 輸入5 6 d(即給c 賦值不正確)則x的值為2;
- 輸入5 t d(即給b和c 賦值不正確)則x的值為1;
scanf()的返回值對我們來說也很有用的,例如可使用if(scanf("%d,%d",&a,&b)==2)這樣語句來判斷是否正確的給所有的變數賦值了,正確的話才能使用這個變數與運算,這樣才能提高我們程式碼的安全性。