这是一个启动计时器的脚本函数,因为要等cgi那边的数据 所以当开始计时时停止了一秒 想要在后面加上,当初不清楚 随意的就在 timeVal_sec处加1使得情况如下:
ServerDate.setUTCHours(
timeVal_hour,
timeVal_min,
timeVal_sec+1);
而这会使得ServerDate的值偏离我们的预期,而这样造成的结果就是 在计时时 第一次时间的跳动会变的不规律 而不是一秒一秒跳动。 切记不要对setUTCHours里的参数再运算。
在web前端修改时间的功能中,我将修改后的时间是用由gettime()返回的离1970.1.1到现在的毫秒数 再由服务器上的cgi程序读取, web端的源程序如下:
function savenetconfig()
{
var dateVal_year=netconfigfrm.Serverdateyear.value;
var dateVal_mon=netconfigfrm.Serverdatemon.value;
var dateVal_day=netconfigfrm.Serverdateday.value;
var timeVal_hour=netconfigfrm.Servertimehour.value;
var timeVal_min=netconfigfrm.Servertimemin.value;
var timeVal_sec=netconfigfrm.Servertimesec.value;
var timemodified=new Date();
timemodified.setFullYear(dateVal_year,dateVal_mon-1,dateVal_day);
timemodified.setHours(timeVal_hour,timeVal_min,timeVal_sec);
var timeServer = timemodified.getTime();
//var modifiedtime = parseInt(timeServer/1000);
var modifiedtime = timeServer%1000;
modifiedtime = (timeServer-modifiedtime)/1000;
netconfigfrm.TimeServer.value = modifiedtime;
var code=1007
netconfigfrm.actioncode.value = code;
netconfigfrm.submit();
}
cgi读取web前端的源程序如下:
static int set_dev_time()
{
int ret =0;
int timeserver=0;
struct timeval tv ;
struct timezone tz;
cgiFormInteger("TimeServer",×erver,0);
ret = gettimeofday(&tv,&tz);
//tv.tv_sec = 1221711;
tv.tv_sec = timeserver;
tv.tv_usec = 100000;
ret = settimeofday(&tv,NULL);
htmlHeader("The temp page");
htmlBody();
fprintf(cgiOut,"<SCRIPT LANGUAGE=\"JavaScript\">");
fprintf(cgiOut,"parent.document.getElementById(\"set_result\").innerHTML = \"保存成功\"; \r\n");
fprintf(cgiOut,"</SCRIPT>");
htmlFooter();
return ret;
}
接收值的变量是整形 timeserver,其最大值为2147483648。但是从前端传来的毫秒数很有可能大于这个值,当大于这个值的时候,timeserver得到的值只能是2147483648,从而造成了错误。
还有一种情况 如果不想很精确的话 为了使得毫秒变秒 可能会想到在web端把 所得毫秒数/1000 那么就得到秒了 ,但是情况却是cgi这边读到的秒数是 0 ,为什么呢?
是因为 所得毫秒数/1000所得的值是一个 浮点型 而cgi取的值是整形。
ok,那么我们就把它转换成整形 用parseInt()函数 将其转换为整形 但是还是不行,cgi这边读到的秒数还是 0,这是为什么呢 我就去看下 cgiFormInteger()函数的实现-
cgiFormResultType cgiFormInteger(
char *name, int *result, int defaultV) {
cgiFormEntry *e;
int ch;
e = cgiFormEntryFindFirst(name);
if (!e) {
*result = defaultV;
return cgiFormNotFound;
}
if (!strlen(e->value)) {
*result = defaultV;
return cgiFormEmpty;
}
ch = cgiFirstNonspaceChar(e->value);
if (!(isdigit(ch)) && (ch != '-') && (ch != '+')) {
*result = defaultV;
return cgiFormBadType;
} else {
*result = atoi(e->value);
return cgiFormSuccess;
}
}
发现 最后cgiFormInteger是用 atoi函数将字符串转换成的int型 所以web端的数据的值之所以传递失败 不是因为他是整形还是浮点型,是因为它不是gettime返回的类型,那么gettime返回什么类型呢 查了查 说是返回long型 而它是64位的。
但是为什么要传64的值才成功呢把它转换成int型就不行呢 我想可能是因为parseInt()函数里面放的是浮点型导致(但是从(parseInt函数得到的是值是正确的整数值,去掉了小数点后面的值了的),或者是64位转32位后破坏了数据 。而要弄清楚这,我觉得要就要弄清楚cgi和web端数据的传送了。
当然 为了后来程序能正常运行 我只能用苯方法了,用数学方法得到不带小数点符合long型的秒数
var modifiedtime = timeServer%1000;
modifiedtime = (timeServer-modifiedtime)/1000;