1. 程式人生 > >solaris/unix/linux 獲取系統時間的方法--精確到年月日時分秒

solaris/unix/linux 獲取系統時間的方法--精確到年月日時分秒

solaris/unix/linux下獲取系統時間的c語言方法——精確到年月日時分秒,可以用localtime函式,該函式在windows上也是通用的。

localtime函式如下:
表頭檔案 #include<time.h>
定義函式 struct tm *localtime(const time_t * timep);
函式說明 localtime()將引數timep所指的time_t結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。結構tm的定義請參考gmtime()。此函式返回的時間日期已經轉換成當地時區。
返回值 返回結構tm代表目前的當地時間,可以以一定格式輸出。

例項:

執行環境是openindiana(solaris)

[email protected]:~/project_ha/test# cat localtime.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define DATE_FMT "%Y-%m-%d %H:%M:%S"
int main()
{
        char szTimeStampBuf[200] = "";
        int nMaxLength = 40;
        time_t  t = 0;
        struct tm *pTM = NULL;
        int nLength = 0;
       
        time(&t);
        pTM = localtime(&t);
        nLength =  strftime(szTimeStampBuf, nMaxLength, DATE_FMT, pTM);
        //nLength = safe_sprintf(szTimeStampBuf, nMaxLength, "%s", asctime(pTM));
        szTimeStampBuf[nLength] = '\0';
        printf("szTime: %s\n", szTimeStampBuf);
        return 0;
}

編譯連結:

[email protected]:~/project_ha/test# gcc -o localtime localtime.c

執行:

[email protected]:~/project_ha/test# ./localtime
szTime: 2011-10-24 14:45:27
 

相關推薦

solaris/unix/linux 獲取系統時間方法--精確年月日分秒

solaris/unix/linux下獲取系統時間的c語言方法——精確到年月日時分秒,可以用localtime函式,該函式在windows上也是通用的。 localtime函式如下: 表頭檔案 #include<time.h> 定義函式 struct tm *l

C/C++程式碼獲取當前時間的:年月日分秒

       直接上菜: #include <iostream> #include <string> using namespace std; static string

Android獲取系統時間方法的總結

flat led roi textview instance set 日期 create 技術 Android獲取系統時間方法的方法有很多種,常用的有Calendar、Date、currentTimeMills等方法。 (1)Calendar Calendar獲取系統時

linux 獲取系統時間

  通過以下函式可以統計裝置開機時間,關機時間,離線時間,距離1970.1.0.0.0的秒數。統計裝置的執行狀況! 1.獲取系統當前時間距1970.1.0.0.0的秒數。通過獲取的秒數方便計算嵌入式裝置的離線時間。 #include<time.h> void m

C語言獲取系統時間方法

需要利用C語言的時間函式time和localtime,具體說明如下: 一、函式介面介紹: 1、time函式。 形式為time_t time (time_t *__timer); 其中time_t為time.h定義的結構體,一般為長整型。 這個函式會獲取

linux 獲取系統時間(兩種辦法,兩種精度)

1.、寫時間到檔案裡    void get_time() {time_t now;struct tm *timenow;FILE *fp;char time_buffer[100] = {0};ti

mysql資料庫函式獲取時間中的年月日分秒

格式:SELECT DATE_FORMAT(value, format);  value:日期列 format:格式,取值如下:  “%y”:年 “%m”:月  “%d”:日 “%h”:12小時制,

JAVA獲取當前時間和將已有的long型別時間轉換為年月日分秒格式

程式碼如下: public class DateUtil { /** * 根據格式獲取當前格式化時間 * @param format 格式化方式,基礎格式為yyyy-MM-dd HH:mm:ss * @return 當前時間 */ publ

Linux和Window下生成以年月日分秒為名稱的檔案。

Windows BAT批處理檔案: 複製程式碼 程式碼如下: @echo off set time_hh=%time:~0,2% if /i %time_hh% LSS 10 (set time_hh=0%time:~1,1%) set filename=%date

C獲取系統時間方法(linux下)

asctime(將時間和日期以字串格式表示)  相關函式  time,ctime,gmtime,localtime 表頭檔案  #include<time.h> 定義函式  char * asctime(const struct tm * timeptr

linux下C獲取系統時間方法

asctime(將時間和日期以字串格式表示)   相關函式  time,ctime,gmtime,localtime   表頭檔案  #include<time.h>   定義函式  char * asctime(const struct tm * ti

VC++ 獲取系統時間、程式執行時間(精確到秒,毫秒)的五種方法

1.使用CTime類(獲取系統當前時間,精確到秒) CString str; //獲取系統時間 CTime tm; tm=CTime::GetCurrentTime();//獲取系統日期 str=tm.Format("現在時間是%Y年%m月%d日 %X"); MessageBox(str,NULL,MB_OK

Lua獲取系統時間時間格式化方法及格式化參數

strong int htm utf %x lsp pri last 顯示 一、系統當前時間對應的時間戳 復制代碼代碼如下: local ntime = os.timeprint(ntime) 二、格式化時間顯示,參考下表常用於設置header等 復制代碼代碼如下: ng

linux獲取系統啟動時間

pre 現在 red str family nds stderr 表達方式 space 1、前言   時間對操作系統來說非常重要,從內核級到應用層,時間的表達方式及精度各部相同。linux內核裏面用一個名為jiffes的常量來計算時間戳。應用層有time、getdayti

Java獲取系統時間的四種方法

min ont 時間格式 總結 int ava nth second 單獨 1、Date day=new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Sys

linux c獲取系統時間

#include<iostream> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <unistd.h> int main(){ struc

MFC中兩種方法獲取系統時間

void CMFC2Dlg::OnBnClickedButton1() { // TODO: 在此新增控制元件通知處理程式程式碼 CTime t = CTime::GetCurrentTime(); int nYear=t.GetYear(); int nMonth=t.GetMonth(

c++獲取系統時間精確到秒

#include "time.h" #include "stdio.h" #include "stdlib.h" #include <iostream> using namespace std; int main() {time_t now;struct tm *timenow;time(&

linux修改系統時間linux檢視時區、修改時區的方法

一、檢視和修改Linux的時區 1. 檢視當前時區 命令 : "date -R" 2. 修改設定Linux伺服器時區 方法 A 命令 : "tzselect" 方法 B 僅限於RedHat Linux 和 CentOS 命令 : "timeconfig" 方法 C 適用於

Windows下c++獲取系統時間的三種方法

1、CTime類的static介面GetCurrentTime() 包含的標頭檔案#include <atltime.h> CTime類的物件表示的時間是基於格林威治標準時間(GMT)的。 CTime表示的日期上限是3000年12月31日,下限是1970年1月1