打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
昨天因为unix时间闹心了好长时间,现在终于搞定
Tzset
设置时间环境变量。
void tzset(void);
例程 需要的头文件 兼容性
tzset <time.h> Win NT,Win 95
对于另外兼容性的信息,参见引言中的兼容性

LIBC.LIB 单线程静态库,零售版本
LIBCMT.LIB 多线程静态库,零售版本
MSVCRT.LIB MSVCRT.DLL的输入库,零售版本
返回值

说明
tzset函数使用环境变量TZ的当前设置把值赋给三个全局变量:
daylight,timezone和tzname。
这些变量由ftime和localtime函数使用校正格林威治(UTC)时间为本地时间,通过time函数从系统时间计算UTC,使用如下语法设置TZ环境变量:
set TZ=tzn[+|-]hh[:mm[:ss]][dzn]
tzn三字母时区名,例如PST,你必须指出本地时间距UTC的偏移量。
hhUTC与本地时间的时差,任选带符号的。
mm分钟,由冒号(:)与hh分隔开。
ss秒钟,用冒号(:)与mm分隔开。
dzn三字母夏令时时区例如PDT。如果夏令时没作用,设置TZ时不设置dzn的值。C运行库假设在计算夏令时(DST)时执行美国的规则。
例如,为了设置TZ环境为德国对应的当前时区,你可以使用如下语句之一:
set TZ=GST-1GDT
set TZ=GST+1GDT
这些字符串使用GST指出德国标准时间,假设德国是超前UTC一个小时,并假设夏令时起作用。
如果TZ值没设置,tzset试图使用操作系统指定的时区信息,在Windwos NT和Windows95之下,这个信息指定在控制面版的日期/时间应用中,如果tzset不能获取这些信息,它缺省使用PST8PDT,它指定为太平洋标准时间。
基于TZ环境变量的值,当调用tzset时把如下值赋给全局变量
daylight、timezone和tzname:
全局变量 说明 缺省值
daylight 如果在TZ设置中指定夏令时时区, 1
则为非0值;否则为0
timezone UTC和本地时间之间的时差,单位为秒 28800(28800秒等于8小时)
tzname[0]TZ环境变量的时区名称的字符串值;如果TZ未设置则为空 PST
tzname[1]夏令时时区的字符串值;如果TZ环境变量中忽略夏令时时区则为空PDT在上表中daylight和tzname数组的缺省值对应于"PST8PDT"。
如果从TZ环境变量忽略DST时区,daylight的值为0,ftime,gmtime和localtime函数对于它们的DST标志返回0。
例子/ *
TZSET.C: This program first sets up the time zone by*
placing the variable named TZ=EST5 in the environment*
table. It then uses tzset to set the global variables*
named daylight, timezone, and tzname.*/
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
void main( void )
{if( putenv( "TZ=EST5EDT" ) == -1 )
{printf( "Unable to set TZ\n" );exit( 1 );
}
else
{ tzset(); printf( "daylight = %d\n", daylight );
printf( "timezone = %ld\n", timezone );
printf( "tzname[0] = %s\n", tzname[0] );
}
exit( 0 );
}
输出
daylight = 1
timezone = 18000
tzname[0] = EST
参见
asctime,ftime,gmtime,localtime,time,utime
 
注:虽然介绍中说szset这个函数只支持window95,但是经过测试在dos下也可以使用。
源程序:(本程序在bc31中测试成功,操作系统环境是windows server 2003)
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "dos.h"
main()
{
 struct date dt1;
 struct time tm1;
 long lg1,lg2,lg3,lday,lsec;
 unsigned int year,month,day;
 unsigned int itmp1;
// scanf("%d %d %d",&dt1.da_year,&dt1.da_mon,&dt1.da_day);
// scanf("%d %d %d",&tm1.ti_hour,&tm1.ti_min,&tm1.ti_sec);
// putenv("TZ=CHN+08:00:00");
 putenv("TZ=EST0");
 tzset();
 dt1.da_year=1970;
 dt1.da_mon=11;
 dt1.da_day=1;
 tm1.ti_hour=1;
 tm1.ti_min=2;
 tm1.ti_sec=2;
 lg1=dostounix(&dt1,&tm1);
 year=dt1.da_year-1970;
 switch(dt1.da_mon)
 {
 case 1: itmp1=0; break;
 case 2: itmp1=31;break;
 case 3: itmp1=59;break;
 case 4: itmp1=90;break;
 case 5: itmp1=120;break;
 case 6: itmp1=151;break;
 case 7: itmp1=181;break;
 case 8: itmp1=212;break;
 case 9: itmp1=243;break;
 case 10:itmp1=273;break;
 case 11:itmp1=304;break;
 case 12:itmp1=334;break;
 default: return 0;
 }
 lday=year*365+itmp1+dt1.da_day-1;
 lday+=year/4;
 if(year%4>2)
 {
   lday+=1;
 }
 else
 if(dt1.da_year%4==0)
 {
   if(dt1.da_mon>2)
   lday+=1;
 }
 lg2=3600;
 lsec=(lday*24+tm1.ti_hour)*lg2;
 lg3=lsec+tm1.ti_min*60+tm1.ti_sec;
 delay(100);
 return 0;
}
 
 
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python工具模块介绍-time 时间访问和转换!
mktime和localtime
java编程实现获取时间和时区设置
5.8
彻底弄懂GMT、UTC、时区和夏令时
Linux C函数之时间函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服