当前位置:首页 » 编程语言 » 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 05:54:34 浏览:828
学车网源码 发布:2025-02-06 05:47:40 浏览:385
搭建局域网影院服务器 发布:2025-02-06 05:47:38 浏览:759
广东一区cf刷经验房密码都是什么 发布:2025-02-06 05:46:20 浏览:633
加密工具源码 发布:2025-02-06 05:44:52 浏览:13
笔记本可用存储多大 发布:2025-02-06 05:44:48 浏览:157
ftp登录后没有反应 发布:2025-02-06 05:29:41 浏览:175
linux建目录命令 发布:2025-02-06 05:29:06 浏览:724
安卓所有照片在哪里 发布:2025-02-06 05:16:32 浏览:972
l2缓存作用 发布:2025-02-06 05:15:02 浏览:291