當前位置:首頁 » 編程語言 » c語言設置時區

c語言設置時區

發布時間: 2023-08-12 11:26:00

A. 使用c語言描述夏時令期間

c 語言獲取現在時間用 time(NULL);
無論你在哪個時區,那個國家,time(NULL) 返回值 是 一模一樣的,因為它等於
UTC 時間,從 1970年1月1日0時起到現在的 秒數。
當地時間的計算,涉及到時區。中國用東八區,當地時間偏移量 是 UTC+8 小時。
每年3月的最後一個星期天是幾號,與年有關。
每年10月最後一個星期天是幾號,與年有關。
算得 起始 月日,結束 月日。
當你的時間 介於 起始結束之間 就是 夏令時,設 key=1.
=======
下面給你 提示,算出 每年起始截止 月日,換算到 JD ( 該年的第幾天),
你自己 得到 現在時間,調 YMD_2_JD 得到 JD_now,
if (JD_now < jd2 && JD_now > jd) key =1;else key=0;
=========
#include<stdio.h>
#include<math.h>
#include<time.h>
int YMD_2_WeekDay(int Y, int M, int D){
int offset,jd,weekD;
offset = ((Y-1)+(Y-1)/4-(Y-1)/100+(Y-1)/400) % 7 ;
jd = YMD_2_JD(Y,M,D);
weekD = (jd + offset) % 7;
return weekD;
}
int YMD_2_JD(int Y, int M, int D){
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int JD,i;
JD=D;
for (i=0;i<M;i++) JD+=MonthDay[i];
if (((Y%4==0)&&(Y%100!=0)||(Y%400==0)) && (M>2)) JD++;
return JD;
}
int main()
{
int Y=2015,M=3,D=31,M2=10,D2=31;
int wd,wd2,jd,jd2;
wd = YMD_2_WeekDay(Y,M,D);
wd2 = YMD_2_WeekDay(Y,M2,D2);
D=D-wd;
D2=D2-wd2;
printf("date: %d %d\n",D,D2);
jd=YMD_2_JD(Y,M,D);
jd2 = YMD_2_JD(Y,M2,D2);
printf("J day: %d %d\n",jd,jd2);
return 0;
}

/*
time(NULL); gets time
elapsed since 00:00 hours, Jan 1, 1970 UTC
*/

B. 編寫一個簡單的C語言程序,在屏幕上顯示一行時間(包含小時、分鍾和秒鍾)的信息

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main()
{
time_t timep,Tim;
struct tm *p;
time(&timep);
p = localtime(&timep); //此函數獲得的tm結構體的時間,是已經進行過時區轉化為本地時間
//p = gmtime(&timep); //把日期和時間轉換為格林威治(GMT)時間的函數
int Year = 1900 + p->tm_year;
int Month = 1 + p->tm_mon;
int Day = p->tm_mday;
int Hour = p->tm_hour;
int Minute = p->tm_min;
int Second = p->tm_sec;
char year[20];
char month[20];
char day[20];
char hour[20];
char minute[20];
char second[20];
printf("hour=%d\n", Hour);
printf("minute=%d\n", Minute);
printf("second=%d\n", Second);
return 0;
}
編譯一次就會顯示這一刻的系統時間;這個程序我也不太懂,我學長講了半天,數據結構都整出來了,也沒把我整明白,我再研究研究;希望能幫到你吧;

C. Linux系統中C語言如何修改時區

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

D. C語言顯示系統時間

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));

return0;
}

time_t//時間類型(time.h定義)
structtm{//時間結構,time.h定義如下:
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
}
time(&rawtime);//獲取時間,以秒計,從1970年1月一日起算,存於rawtime
localtime(&rawtime);//轉為當地時間,tm時間結構
asctime()//轉為標准ASCII時間格式:
//就是直接列印tm,tm_year從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1

E. c語言時間處理函數

C語言的標准庫函數包括一系列日期和時間處理函數,它們都在頭文件中說明。

在頭文件中定義了三種類型:time_t,struct tm和clock_t。

下面列出了這些函數。

time_t time(time_t *timer);

double difftime(time_t time1,time_t time2);

struct tm *gmtime(const time_t *timer);

struct tm *localtime(const time_t *timer);

char *asctime(const struct tm *timeptr);

char *ctime(const time_t *timer);

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

time_t mktime(struct tm *timeptr);

clock_t clock(void);

【具體應用舉例】

asctime(將時間和日期以字元串格式表示)
相關函數
time,ctime,gmtime,localtime
表頭文件
#i nclude
定義函數
char * asctime(const struct tm * timeptr);
函數說明
asctime()將參數timeptr所指的tm結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字元串形態返回。
此函數已經由時區轉換成當地時間,字元串格式為:"Wed Jun 30 21:49:08 1993\n"
返回值
若再調用相關的時間日期函數,此字元串可能會被破壞。此函數與ctime不同處在於傳入的參數是不同的結構。
附加說明
返回一字元串表示目前當地的時間日期。
範例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",asctime(gmtime(&timep)));
}
執行
Sat Oct 28 02:10:06 2000


ctime(將時間和日期以字元串格式表示)
相關函數
time,asctime,gmtime,localtime
表頭文件
#i nclude
定義函數
char *ctime(const time_t *timep);
函數說明
ctime ()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字元串形態返回。
此函數已經由時區轉換成當地時間,字元串格式為"Wed Jun 30 21 :49 :08 1993\n"。若再調用相關的時間日期函數,此字元串可能會被破壞。
返回值
返回一字元串表示目前當地的時間日期。
範例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",ctime(&timep));
}
執行
Sat Oct 28 10 : 12 : 05 2000


gettimeofday(取得目前的時間)
相關函數
time,ctime,ftime,settimeofday
表頭文件
#i nclude
#i nclude
定義函數
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函數說明
gettimeofday()會把目前的時間有tv所指的結構返回,當地時區的信息則放到tz所指的結構中。
timeval結構定義為:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 結構定義為:
struct timezone{
int tz_minuteswest; /*和Greenwich 時間差了多少分鍾*/
int tz_dsttime; /*日光節約時間的狀態*/
};
上述兩個結構都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀態如下
DST_NONE /*不使用*/
DST_USA /*美國*/
DST_AUST /*澳洲*/
DST_WET /*西歐*/
DST_MET /*中歐*/
DST_EET /*東歐*/
DST_CAN /*加拿大*/
DST_GB /*大不列顛*/
DST_RUM /*羅馬尼亞*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以後)*/
返回值
成功則返回0,失敗返回-1,錯誤代碼存於errno。附加說明EFAULT指針tv和tz所指的內存空間超出存取許可權。
範例
#i nclude
#i nclude
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf("tv_sec; %d\n", tv,.tv_sec) ;
printf("tv_usec; %d\n",tv.tv_usec);
printf("tz_minuteswest; %d\n", tz.tz_minuteswest);
printf("tz_dsttime, %d\n",tz.tz_dsttime);
}
執行
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0


gmtime(取得目前時間和日期)
相關函數
time,asctime,ctime,localtime
表頭文件
#i nclude
定義函數
struct tm*gmtime(const time_t*timep);
函數說明
gmtime()將參數timep 所指的time_t 結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。
結構tm的定義為
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒數,正常范圍為0-59,但允許至61秒
int tm_min 代表目前分數,范圍0-59
int tm_hour 從午夜算起的時數,范圍為0-23
int tm_mday 目前月份的日數,范圍01-31
int tm_mon 代表目前月份,從一月算起,范圍從0-11
int tm_year 從1900 年算起至今的年數
int tm_wday 一星期的日數,從星期一算起,范圍為0-6
int tm_yday 從今年1月1日算起至今的天數,范圍為0-365
int tm_isdst 日光節約時間的旗標
此函數返回的時間日期未經時區轉換,而是UTC時間。
返回值
返回結構tm代表目前UTC 時間
範例
#i nclude
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf("%d%d%d",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf("%s%d;%d;%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
執行
2000/10/28 Sat 8:15:38


localtime(取得當地目前時間和日期)
相關函數
time, asctime, ctime, gmtime
表頭文件
#i nclude
定義函數
struct tm *localtime(const time_t * timep);
函數說明
localtime()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。
結構tm的定義請參考gmtime()。此函
數返回的時間日期已經轉換成當地時區。
返回值
返回結構tm代表目前的當地時間。
範例
#i nclude
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得當地時間*/
printf ("%d%d%d ", (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf("%s%d:%d:%d\n", wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
執行
2000/10/28 Sat 11:12:22


mktime(將時間結構數據轉換成經過的秒數)
相關函數
time,asctime,gmtime,localtime
表頭文件
#i nclude
定義函數
time_t mktime(strcut tm * timeptr);
函數說明
mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。
返回值
返回經過的秒數。
範例
/* 用time()取得時間(秒數),利用localtime()
轉換成struct tm 再利用mktine()將struct tm轉換成原來的秒數*/
#i nclude
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf("time() : %d \n",timep);
p=localtime(&timep);
timep = mktime(p);
printf("time()->localtime()->mktime():%d\n",timep);
}
執行
time():974943297
time()->localtime()->mktime():974943297


settimeofday(設置目前時間)
相關函數
time,ctime,ftime,gettimeofday
表頭文件
#i nclude
#i nclude
定義函數
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函數說明
settimeofday()會把目前時間設成由tv所指的結構信息,當地時區信息則設成tz所指的結構。詳細的說明請參考gettimeofday()。
注意,只有root許可權才能使用此函數修改時間。
返回值
成功則返回0,失敗返回-1,錯誤代碼存於errno。
錯誤代碼
EPERM 並非由root許可權調用settimeofday(),許可權不夠。
EINVAL 時區或某個數據是不正確的,無法正確設置時間。


time(取得目前的時間)
相關函數
ctime,ftime,gettimeofday
表頭文件
#i nclude
定義函數
time_t time(time_t *t);
函數說明
此函數會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現在所經過的秒數。如果t 並非空指針的話,
此函數也會將返回值存到t指針所指的內存。
返回值
成功則返回秒數,失敗則返回((time_t)-1)值,錯誤原因存於errno中。
範例
#i nclude
mian()
{
int seconds= time((time_t*)NULL);
printf("%d\n",seconds);
}

Date and Time Functions: <time.h>
The header <time.h> declares types and functions for manipulating date and time. Some functions process local time,
which may differ from calendar time, for example because of time zone. clock_t and time_t are arithmetic types
representing times, and struct tm holds the components
of a calendar time:
int tm_sec; seconds after the minute (0,61)
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 Saving Time flag

tm_isdst is positive if Daylight Saving Time is in effect, zero if not, and negative if the information is not available.

clock_t clock(void)
clock returns the processor time used by the program since the beginning of execution, or -1 if unavailable.
clock()/CLK_PER_SEC is a time in
seconds.

time_t time(time_t *tp)
time returns the current calendar time or -1 if the time is not available. If tp is not NULL,
the return value is also assigned to *tp.

double difftime(time_t time2, time_t time1)
difftime returns time2-time1 expressed in seconds.

time_t mktime(struct tm *tp)
mktime converts the local time in the structure *tp into calendar time in the same representation used by time.
The components will have values
in the ranges shown. mktime returns the calendar time or -1 if it cannot be represented.
The next four functions return pointers to static objects that may be overwritten by other calls.
char *asctime(const struct tm *tp)
asctime*tp into a string of the form
Sun Jan 3 15:14:13 1988\n\0

char *ctime(const time_t *tp)
ctime converts the calendar time *tp to local time; it is equivalent to
asctime(localtime(tp))

struct tm *gmtime(const time_t *tp)
gmtime converts the calendar time *tp into Coordinated Universal Time (UTC). It returns NULL if UTC is not available.
The name gmtime has historical significance.

struct tm *localtime(const time_t *tp)
localtime converts the calendar time *tp into local time.

size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)
strftime formats date and time information from *tp into s according to fmt, which is analogous to a printf format.
Ordinary characters (including the terminating '\0') are copied into s. Each %c is replaced as described below,
using values appropriate for the local environment.
No more than smax characters are placed into s. strftime returns the number of characters, excluding the '\0',
or zero if more than smax characters were proced.
%a abbreviated weekday name.
%A full weekday name.
%b abbreviated month name.
%B full month name.
%c local date and time representation.
%d day of the month (01-31).
%H hour (24-hour clock) (00-23).
%I hour (12-hour clock) (01-12).
%j day of the year (001-366).
%m month (01-12).
%M minute (00-59).
%p local equivalent of AM or PM.
%S second (00-61).
%U week number of the year (Sunday as 1st day of week) (00-53).
%w weekday (0-6, Sunday is 0).
%W week number of the year (Monday as 1st day of week) (00-53).
%x local date representation.
%X local time representation.
%y year without century (00-99).
%Y year with century.
%Z time zone name, if any.
%% %

F. 用C語言獲取本地的時區

getlocaltime裡面是沒有時區信息的。

你可以這樣:

time_ttime_utc=0;
structtm*p_tm_time;
inttime_zone=0;

p_tm_time=localtime(&time_utc);//轉成當地時間
time_zone=(p_tm_time->tm_hour>12)?(p_tm_time->tm_hour-=24):p_tm_time->tm_hour;

把0時間轉為當地時間,得到的是帶時區的結果。

G. C語言中如何獲取當前系統時間的小時

程序主要通過當前系統日歷的struct tm結構體獲得,主要代碼如下,
#include <stdio.h>
#include <time.h>
//程序功能輸出當前時間在24H下的小時數
int main(int argc, char *argv[])
{
struct tm *ptr;
time_t lt;
time(<);//當前系統時間
ptr=localtime(<);//獲取本地日歷時間指針
printf("hour=%d(24H )\n",ptr->tm_hour);//輸出24H下的小時數
return 0;
}

結構體tm定義如下,
struct tm {
int tm_sec; /* 秒–取值區間為[0,59] */
int tm_min; /* 分 - 取值區間為[0,59] */
int tm_hour; /* 時 - 取值區間為[0,23] */
int tm_mday; /* 一個月中的日期 - 取值區間為[1,31] */
int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區間為[0,11] */
int tm_year; /* 年份,其值從1900開始 */
int tm_wday; /* 星期–取值區間為[0,6],其中0代表星期天,1代表星期一,以此類推 */
int tm_yday; /* 從每年的1月1日開始的天數–取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/
long int tm_gmtoff; /*指定了日期變更線東面時區中UTC東部時區正秒數或UTC西部時區的負秒數*/
const char *tm_zone; /*當前時區的名字(與環境變數TZ有關)*/
};

H. C語言中取得當地時間范圍問題

#include <time.h>
#include <stdio.h>
int main( void )
{
time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );
puts( tmp );
return 0;
}
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
根據格式字元串生成字元串。
struct tm *localtime(const time_t *timer);
取得當地時間,localtime獲取的結果由結構tm返回
返回的字元串可以依下列的格式而定:
%a 星期幾的縮寫。Eg:Tue
%A 星期幾的全名。 Eg: Tuesday
%b 月份名稱的縮寫。
%B 月份名稱的全名。
%c 本地端日期時間較佳表示字元串。
%d 用數字表示本月的第幾天 (范圍為 00 至 31)。日期
%H 用 24 小時制數字表示小時數 (范圍為 00 至 23)。
%I 用 12 小時制數字表示小時數 (范圍為 01 至 12)。
%j 以數字表示當年度的第幾天 (范圍為 001 至 366)。
%m 月份的數字 (范圍由 1 至 12)。
%M 分鍾。
%p 以 ''AM'' 或 ''PM'' 表示本地端時間。
%S 秒數。
%U 數字表示為本年度的第幾周,第一個星期由第一個周日開始。
%W 數字表示為本年度的第幾周,第一個星期由第一個周一開始。
%w 用數字表示本周的第幾天 ( 0 為周日)。
%x 不含時間的日期表示法。
%X 不含日期的時間表示法。 Eg: 15:26:30
%y 二位數字表示年份 (范圍由 00 至 99)。
%Y 完整的年份數字表示,即四位數。 Eg:2008
%Z(%z) 時區或名稱縮寫。Eg:中國標准時間
%% % 字元。

//方案二 優點:能精確到毫秒級;缺點:使用了windows API
#include <windows.h>
#include <stdio.h>
int main( void )
{
SYSTEMTIME sys;
GetLocalTime( &sys );
printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
return 0;
}

//方案三,優點:利用系統函數,還能修改系統時間
//此文件必須是c++文件
#include<stdlib.h>
#include<iostream>
using namespace std;
void main()
{
system("time");
}

//方案四,將當前時間折算為秒級,再通過相應的時間換算即可
//此文件必須是c++文件
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t now_time;
now_time = time(NULL);
cout<<now_time;
return 0;
}

I. C語言 time()

1 是把一個正整數放進t所在的地址里,這個正整數是1970年1月1日00:00:00(UTC)開始,到目前為止經過的秒數。

2 因為time以兩種方式返回結果。一種是你第一道題的,給他一個地址,他把結果寫進那個地址。第二種直接返回一個time_t。你可以用這種方法接:time_t t = time(NULL)。這里給他一個空指針就是告訴他不需要以第一種方法返回結果,所以當然他也不會把結果寫進空指針,他只是不管這個參數而已。當然只要你樂意你也可以兩種一起用。

熱點內容
編程一首詩 發布:2025-02-06 06:45:04 瀏覽:528
驚聲尖笑5下載ftp 發布:2025-02-06 06:33:16 瀏覽:528
共享文件夾讓輸入密碼 發布:2025-02-06 06:32:28 瀏覽:970
收銀伺服器響應出錯什麼意思 發布:2025-02-06 06:24:43 瀏覽:607
sql用戶授權 發布:2025-02-06 06:24:42 瀏覽:677
蘋果手機相冊顯示正在上傳 發布:2025-02-06 06:05:43 瀏覽:542
hadoop下載文件夾 發布:2025-02-06 06:05:08 瀏覽:187
鎧最強配置是哪些 發布:2025-02-06 06:04:22 瀏覽:360
編譯器的製作環境 發布:2025-02-06 05:54:34 瀏覽:829
學車網源碼 發布:2025-02-06 05:47:40 瀏覽:386