亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

linux時(shí)間函數(shù)與時(shí)間格式與字符串之間的轉(zhuǎn)化方法

 更新時(shí)間:2022年01月10日 09:23:00   作者:很菜很菜的鳥  
下面小編就為大家分享一篇linux時(shí)間函數(shù)與時(shí)間格式與字符串之間的轉(zhuǎn)化方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

我們可以認(rèn)為格林威治時(shí)間就是時(shí)間協(xié)調(diào)時(shí)間(GMT=UTC)

GMT : 格林威治時(shí)間

UTC : 時(shí)間協(xié)調(diào)時(shí)間

1、time_t

time_t time(time_t *t);

取得從1970年1月1日至今的秒數(shù)。

time_t類型,這本質(zhì)上是一個(gè)長(zhǎng)整數(shù)( long ),表示從1970-01-01 00:00:00到目前計(jì)時(shí)時(shí)間的秒數(shù),timeval則精確到毫秒

2、timeval

timeval類型,這是一個(gè)結(jié)構(gòu)體類型,struct timeval 頭文件為 time.h

struct timeval
{
time_t tv_sec;    /* Seconds. */
//秒
suseconds_t tv_usec; /* Microseconds. */
//微秒
};

使用:

struct timeval tv;
gettimeofday(&tv, NULL);
printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);

3、timezone

struct timezone
{ 
  int tz_minuteswest;   /* minutes west of Greenwich */ 
  int tz_dsttime;     /* type of DST correction */ 
};

4、struct tm

tm結(jié)構(gòu),這本質(zhì)上是一個(gè)結(jié)構(gòu)體,里面包含了各時(shí)間字段

struct tm { 
    int tm_sec;   /* seconds after the minute - [0,59] */ 
    int tm_min;   /* minutes after the hour - [0,59] */ 
    int tm_hour;  /* hours since 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 savings time flag */ 
    };

其中tm_year表示從1900年到目前計(jì)時(shí)時(shí)間間隔多少年,如果是手動(dòng)設(shè)置值的話,tm_isdst通常取值-1。

功能:

tm結(jié)構(gòu)體包含,年、月、日,時(shí)、分、秒

使用:

time_t t_time;
  struct tm *tmp_ptr = NULL;
  time(&t_time);
  printf("t_time = %d.\n", t_time);  
  tmp_ptr = gmtime(&t_time);

  printf("after gmtime, the time is: \
      \n yday = %d \
      \n wday = %d \
      \n year = %d \
      \n mon = %d \
      \n mday = %d \
      \n hour = %d \
      \n min = %d \
      \n sec = %d \
      \n isdst =%d.\n", 
      tmp_ptr->tm_yday,
      tmp_ptr->tm_wday,
      tmp_ptr->tm_year,
      tmp_ptr->tm_mon,
      tmp_ptr->tm_mday,
      tmp_ptr->tm_hour, 
      tmp_ptr->tm_min, 
      tmp_ptr->tm_sec,
      tmp_ptr->tm_isdst
      );

打?。?/strong>

t_time = 1513841065.
after gmtime, the time is:       
yday = 354       
wday = 4       
year = 117       
mon = 11       
mday = 21       
hour = 7       
min = 24       
sec = 25       
isdst =0.

5、ctime/asctime

char *ctime(const time_t *timep);

將timep轉(zhuǎn)換為真是世界的時(shí)間,以字符串顯示,它和asctime不同就在于傳入的參數(shù)形式不一樣。

char *asctime(const struct tm* timeptr);

將結(jié)構(gòu)中的信息轉(zhuǎn)換為真實(shí)世界的時(shí)間,以字符串的形式顯示。

char *ctime(const time_t *timer) 返回一個(gè)表示當(dāng)?shù)貢r(shí)間的字符串,當(dāng)?shù)貢r(shí)間是基于參數(shù) timer。

返回的字符串格式:Thu Dec 21 13:59:57 2017

使用:

time_t curtime;
struct tm *tm_ptr = NULL;
time(&curtime);
tm_ptr = localtime(&curtime);
printf("ctime轉(zhuǎn)換的當(dāng)前時(shí)間 = %s", ctime(&curtime));
printf("asctime轉(zhuǎn)換的當(dāng)前時(shí)間 = %s", asctime(tm_ptr));

打?。?/strong>

ctime轉(zhuǎn)換的當(dāng)前時(shí)間 = Thu Dec 21 13:59:57 2017

asctime轉(zhuǎn)換的當(dāng)前時(shí)間 = Thu Dec 21 13:59:57 2017

6、gmtime/localtime

struct tm* gmtime(const time_t *timep);

將time_t表示的時(shí)間轉(zhuǎn)換為沒有經(jīng)過時(shí)區(qū)轉(zhuǎn)換的UTC時(shí)間,是一個(gè)struct tm結(jié)構(gòu)指針。

stuct tm* localtime(const time_t *timep);

和gmtime類似,但是它是經(jīng)過時(shí)區(qū)轉(zhuǎn)換的時(shí)間。

time_t curtime;

gmtime 函數(shù)將 curtime 轉(zhuǎn)換為struct tm結(jié)構(gòu)的格林威治時(shí)間,基本的意思是,gmtime轉(zhuǎn)出來的是0時(shí)區(qū)的標(biāo)準(zhǔn)時(shí)間

localtime 函數(shù)將 curtime 轉(zhuǎn)換為struct tm結(jié)構(gòu)的本地時(shí)間,localtime是將時(shí)區(qū)考慮在內(nèi)了,轉(zhuǎn)出的當(dāng)前時(shí)區(qū)的時(shí)間。

舉個(gè)例子:

time_t t_time;
  struct tm *tmp_ptr = NULL;
  time(&t_time);
  printf("t_time = %d.\n", t_time);  
  tmp_ptr = gmtime(&t_time);

  printf("after gmtime, the time is: \
      \n yday = %d \
      \n wday = %d \
      \n year = %d \
      \n mon = %d \
      \n mday = %d \
      \n hour = %d \
      \n min = %d \
      \n sec = %d \
      \n isdst =%d.\n", 
      tmp_ptr->tm_yday,
      tmp_ptr->tm_wday,
      tmp_ptr->tm_year,
      tmp_ptr->tm_mon,
      tmp_ptr->tm_mday,
      tmp_ptr->tm_hour, 
      tmp_ptr->tm_min, 
      tmp_ptr->tm_sec,
      tmp_ptr->tm_isdst
      );
  
  tmp_ptr = localtime(&t_time);

  printf("after localtime, the time is: \
      \n yday = %d \
      \n wday = %d \
      \n year = %d \
      \n mon = %d \
      \n mday = %d \
      \n hour = %d \
      \n min = %d \
      \n sec = %d \
      \n isdst =%d.\n", 
      tmp_ptr->tm_yday,
      tmp_ptr->tm_wday,
      tmp_ptr->tm_year,
      tmp_ptr->tm_mon,
      tmp_ptr->tm_mday,
      tmp_ptr->tm_hour, 
      tmp_ptr->tm_min, 
      tmp_ptr->tm_sec,
      tmp_ptr->tm_isdst
      );
  return 0;

打?。?/strong>

t_time = 1513841065.
after gmtime, the time is:       
yday = 354       
wday = 4       
year = 117       
mon = 11       
mday = 21       
hour = 7       
min = 24       
sec = 25       
isdst =0.
after localtime, the time is:       
yday = 354       
wday = 4       
year = 117       
mon = 11       
mday = 21       
hour = 15       
min = 24       
sec = 25       
isdst =0.

7、mktime

time_t mktime(struct tm* timeptr);

將struct tm 結(jié)構(gòu)的時(shí)間轉(zhuǎn)換為從1970年至今的秒數(shù)。

mktime 與 gmtime/localtime 功能相反,gmtime/localtime 將time_t轉(zhuǎn)換為struct tm結(jié)構(gòu)體數(shù)據(jù),mktime將struct tm重新轉(zhuǎn)換為time_t類型的UTC時(shí)間

使用例子:

time_t t_time;
  struct tm *tm_ptr = NULL;
  time(&t_time);
  printf("time_t first time value = %d.\n", t_time);

  tm_ptr = gmtime(&t_time);
  printf("time_t switch gmtime type, the time is: \
      \n yday = %d \
      \n wday = %d \
      \n year = %d \
      \n mon = %d \
      \n mday = %d \
      \n hour = %d \
      \n min = %d \
      \n sec = %d \
      \n isdst =%d.\n", 
      tm_ptr->tm_yday,
      tm_ptr->tm_wday,
      tm_ptr->tm_year,
      tm_ptr->tm_mon,
      tm_ptr->tm_mday,
      tm_ptr->tm_hour, 
      tm_ptr->tm_min, 
      tm_ptr->tm_sec,
      tm_ptr->tm_isdst
      );
  
  t_time = mktime(tm_ptr); /*重新轉(zhuǎn)換為time_t類型的UTC時(shí)間,這里有一個(gè)時(shí)區(qū)的轉(zhuǎn)換, 時(shí)間為0區(qū)的時(shí)間, 所以一下使用的時(shí)間都為0區(qū)的時(shí)間*/  
  printf("gmtime type switch time_t second time = %d.\n", t_time); 

打?。?/strong>

time_t first time value = 1513842674.
time_t switch gmtime type, the time is:       
yday = 354       
wday = 4       
year = 117       
mon = 11       
mday = 21       
hour = 7       
min = 51       
sec = 14       
isdst =0.
gmtime type switch time_t second time = 1513813874.

8、gettimeofday

int gettimeofday(struct timeval *tv, struct timezone *tz);

返回當(dāng)前距離1970年的秒數(shù)和微妙數(shù),后面的tz是時(shí)區(qū),一般不用。

使用例子:

struct timeval time_val;

gettimeofday(&time_val, NULL); //gettimeofday(&start,&tz);結(jié)果一樣

printf("時(shí)間秒.tv_sec: %d.\n",time_val.tv_sec);
printf("時(shí)間微秒.tv_usec: %d.\n",time_val.tv_usec);

打?。?/strong>

時(shí)間秒.tv_sec:1513843633
時(shí)間微秒.tv_usec:689374

9、difftime

double difftime(time_t time1, time_t time2);

返回兩個(gè)時(shí)間相差的秒數(shù)

使用例子:

//獲得時(shí)間差
  time_t t_start;
  time_t t_end;
  time(&t_start);
  sleep(5);
  time(&t_end);
  printf("間隔時(shí)間= %f.\n", difftime(t_end, t_start));

打?。?/strong>

間隔時(shí)間= 5.000000.

10、strftime

size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);

使用strftime()函數(shù)將時(shí)間格式化為我們想要的格式。

(
%a 星期幾的簡(jiǎn)寫
%A 星期幾的全稱
%b 月分的簡(jiǎn)寫
%B 月份的全稱
%c 標(biāo)準(zhǔn)的日期的時(shí)間串
%C 年份的后兩位數(shù)字
%d 十進(jìn)制表示的每月的第幾天
%D 月/天/年
%e 在兩字符域中,十進(jìn)制表示的每月的第幾天
%F 年-月-日
%g 年份的后兩位數(shù)字,使用基于周的年
%G 年分,使用基于周的年
%h 簡(jiǎn)寫的月份名
%H 24小時(shí)制的小時(shí)
%I 12小時(shí)制的小時(shí)
%j 十進(jìn)制表示的每年的第幾天
%m 十進(jìn)制表示的月份
%M 十時(shí)制表示的分鐘數(shù)
%n 新行符
%p 本地的AM或PM的等價(jià)顯示
%r 12小時(shí)的時(shí)間
%R 顯示小時(shí)和分鐘:hh:mm
%S 十進(jìn)制的秒數(shù)
%t 水平制表符
%T 顯示時(shí)分秒:hh:mm:ss
%u 每周的第幾天,星期一為第一天 (值從0到6,星期一為0)
%U 第年的第幾周,把星期日做為第一天(值從0到53)
%V 每年的第幾周,使用基于周的年
%w 十進(jìn)制表示的星期幾(值從0到6,星期天為0)
%W 每年的第幾周,把星期一做為第一天(值從0到53)
%x 標(biāo)準(zhǔn)的日期串
%X 標(biāo)準(zhǔn)的時(shí)間串
%y 不帶世紀(jì)的十進(jìn)制年份(值從0到99)
%Y 帶世紀(jì)部分的十進(jìn)制年份
%z,%Z 時(shí)區(qū)名稱,如果不能得到時(shí)區(qū)名稱則返回空字符。
%% 百分號(hào)
)

使用例子:

time_t t_time;
  char buf[128];
  struct tm* tm_ptr = NULL;
  time(&t_time);
  tm_ptr = localtime(&t_time);
  //2017-12-21 18:53:58
  strftime(buf, 64, "%Y-%m-%d %H:%M:%S", tm_ptr);
  strftime(buf, 64, "%Y-%m-%d --- %H:%M:%S", tm_ptr);
  printf("formatTimeString = %s.\n", buf);

打印:

formatTimeString = 2017-12-21 18:53:58.
formatTimeString = 2017-12-21 --- 18:54:46.

11、strptime

功能和 strftime 功能相反, 將字符串格式化為一個(gè)tm結(jié)構(gòu)。

size_t strftime(char *s,size_t maxsize,char *format,const struct tm *timeptr);

使用例子:

char buf[] = "2017-12-21 --- 18:54:46";
  struct tm tm_ptr;
  //2017-12-21 18:53:58
  strptime(buf, "%Y-%m-%d --- %H:%M:%S", &tm_ptr);
  printf("----strptime-----, the time is: \
      \n yday = %d \
      \n wday = %d \
      \n year = %d \
      \n mon = %d \
      \n mday = %d \
      \n hour = %d \
      \n min = %d \
      \n sec = %d.\n", 
      tm_ptr.tm_yday,
      tm_ptr.tm_wday,
      tm_ptr.tm_year,
      tm_ptr.tm_mon,
      tm_ptr.tm_mday,
      tm_ptr.tm_hour, 
      tm_ptr.tm_min, 
      tm_ptr.tm_sec
     );

打?。?/strong>

----strptime-----, the time is:       
yday = 354       
wday = 4       
year = 117       
mon = 11       
mday = 21       
hour = 18       
min = 54       
sec = 46.

以上這篇linux時(shí)間函數(shù)與時(shí)間格式與字符串之間的轉(zhuǎn)化方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Linux查看ip的實(shí)例方法

    Linux查看ip的實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于Linux查看ip的實(shí)例方法,對(duì)此有需要的朋友們可以學(xué)習(xí)下。
    2020-02-02
  • 使Apache實(shí)現(xiàn)gzip壓縮配置

    使Apache實(shí)現(xiàn)gzip壓縮配置

    Apache默認(rèn)的http.conf配置文件中沒有開啟gzip壓縮,apache1.3.x可以用mod_gzip進(jìn)行優(yōu)化網(wǎng)頁瀏覽的速度,在apache2中也嘗試用mod_gzip,但是配置后確發(fā)現(xiàn)網(wǎng)頁不能正確顯示(空白頁),所以改換為mod_deflate
    2014-08-08
  • Centos6 網(wǎng)絡(luò)配置的實(shí)例詳解

    Centos6 網(wǎng)絡(luò)配置的實(shí)例詳解

    這篇文章主要介紹了Centos6 網(wǎng)絡(luò)配置的實(shí)例詳解的相關(guān)資料,希望通過本文大家能夠掌握配置網(wǎng)絡(luò)的知識(shí),需要的朋友可以參考下
    2017-09-09
  • 在Linux中如何查看服務(wù)器整體情況

    在Linux中如何查看服務(wù)器整體情況

    這篇文章主要介紹了在Linux中如何查看服務(wù)器整體情況問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 覆蓋原先的PATH導(dǎo)致命令失效提示command not found的解決方法

    覆蓋原先的PATH導(dǎo)致命令失效提示command not found的解決方法

    今天小編就為大家分享一篇關(guān)于覆蓋原先的PATH導(dǎo)致命令失效提示command not found的解決方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Linux上啟動(dòng)和停止jar的方法示例

    Linux上啟動(dòng)和停止jar的方法示例

    在Linux系統(tǒng)中,要想讓jar包在后臺(tái)運(yùn)行,可以使用nohup命令和&符號(hào),nohup命令可以使進(jìn)程在后臺(tái)不受掛起信號(hào)影響的執(zhí)行,而&符號(hào)則是將任務(wù)放入后臺(tái)執(zhí)行,本文介紹了Linux上啟動(dòng)和停止jar的方法示例,需要的朋友可以參考下
    2024-07-07
  • Linux高并發(fā)踩過的坑及性能優(yōu)化介紹

    Linux高并發(fā)踩過的坑及性能優(yōu)化介紹

    大家好,本篇文章主要講的是Linux高并發(fā)踩過的坑及性能優(yōu)化介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • centos下fail2ban安裝與配置詳解

    centos下fail2ban安裝與配置詳解

    這篇文章主要介紹了centos下fail2ban安裝與配置實(shí)例,fail2ban是一個(gè)實(shí)用、強(qiáng)大的Linux安全軟件,可以監(jiān)控大多數(shù)常用服務(wù)器軟件,需要的朋友可以參考下
    2014-04-04
  • ubuntu 系統(tǒng)上為php加上redis 擴(kuò)展的實(shí)現(xiàn)方法

    ubuntu 系統(tǒng)上為php加上redis 擴(kuò)展的實(shí)現(xiàn)方法

    這篇文章主要介紹了ubuntu 系統(tǒng)上為php加上redis 擴(kuò)展的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-10-10
  • Apache 安全配置方法

    Apache 安全配置方法

    在本文中,筆者將為你提供10個(gè)技巧,借此你可以保護(hù)自己的Apache Web服務(wù)器免于受到許多攻擊。不過,必須謹(jǐn)記,你需要仔細(xì)地評(píng)估每一個(gè)技巧,以確保其適合于你的組織。
    2010-12-12

最新評(píng)論