手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

转:因闰秒造成的误差

转帖前的话:这个问题我没有遇到过,事实上,如果不是看到这篇博客,我根本不知道原来还有闰秒这个问题。先看看这篇文章吧。

 

项目中碰到 PHP 和数据库之间,计算存在时间计算误差。大致的情况为根据段时间字符串,例如:

XML/HTML代码
  1. 2012-12-14 00:00:00 UTC  

使用 MySQL 的 UNIX_TIMESTAMP 函数以及 PHP 的 strtotime 计算得出的时间戳,大概有半分钟(差不多有28秒)的误差。

同时,比较‘诡异’的是直接使用当前时间(MySQL 中 UNIX_TIMESTAMP 不带参数,同时 PHP 直接使用 time 函数),却不存在误差(测试脚本)。

排除了 PHP 和 MySQL 之间因为时区设置造成的时间误差 -- 根据经验,如果是时区设置造成的时间误差,应该有几个小时不会那么少。

搜索解决问题期间扫了下这篇帖子,觉得应该是‘闰秒’这玩意造成的问题。搜索 PHP 闰秒相关的配置似乎没有相关的,不过在这里似乎找到了些答案

XML/HTML代码
  1. You also can experience this behavior if your system timezone is with leap seconds. To avoid the problem in this case please run query UPDATE mysql.time_zone SET Use_leap_seconds='N' and restart the server. Please inform us if this helps.  

按照上述的步骤执行,解决了问题。

回过头来,我在工作机(Windows)上测试,发现并不起作用。研究了下,原来闰秒也需要操作系统的支持

XML/HTML代码
  1. 1、对于大多数新的 Linux 内核,在设计时它们都是支持闰秒的,这一点在 REHL4/5 的 2.6.x 内核中得到肯定。   
  2. 2、如果 Linux 系统没有配种某种时间同步机制(比如NTP),那么和闰秒无关,唯一导致的结果只是系统时间会比 UTC时间快一秒。   
  3. 3、Window Time Service 不支持闰秒,包括服务器和客户端。  
回过头来考虑项目中碰到的这种情况,直接使用时间戳存储时间点会更精确些。最后,提供下相关的测试脚本,看看你的环境是否也会有类似的问题。

Tags: leep, 闰秒

常用类NSingleton

常用的一个小工具类吧,为已经使用过的PHP类建立一个实例,也可以防止代码重复加载。

PHP代码
  1. class NSingleton  
  2. {  
  3.     static $instance;  
  4.   
  5.     static public function get($className$autoInit = true) {  
  6.         if (!$className || !is_string($className)) {  
  7.             require_once ('Empty.php');  
  8.             return new NEmpty(); // or return false;  
  9.         }  
  10.         self::set($className$autoInit);  
  11.         return self::$instance;  
  12.     }  
  13.   
  14.     static protected function set($className$autoInit = true) {  
  15.         if (isset(self::$instance[$key]) && self::$instance[$key] != false) {  
  16.             return;  
  17.         }  
  18.         if (!class_exists($className)) {  
  19.             try {  
  20.                 $classFile = sprintf('%s.php',$className);  
  21.                 require_oncesubstr($classFile, 1) ); //remove 'N'  
  22.             } catch (Exception $e) {  
  23.                 trigger_error('class file not exists');  
  24.                 self::$instance[$key] = false;  
  25.             }  
  26.         }  
  27.         $instance = new $className;  
  28.         if ($auoInit && method_exists($instance'init')) {  
  29.             $instance->init();  
  30.         }  
  31.         self::$instance[$key] = $instance;  
  32.     }  
  33. }  
代码写的其实没什么,主要是简化一些操作和代码的加载。。。(用了这个方法加载类,很有可能无法在IDE被识别)

Tags: singleton, instance

Installing Memcached for PHP 5.3 on Windows 7

说出来让我郁闷,memcached -d install。。。一直无法安装成服务,所以,我每次在运行的时候都是memcached -d,窗口不会关闭。。。很丑。

看看老外怎么说,它来自http://shikii.net/blog/installing-memcached-for-php-5-3-on-windows-7/:

Updated:

First off, all credits go to this guy. I’m just listing the steps on how I did it in Windows 7 with PHP 5.3. Also, I tested this using WampServer but I believe it should work on any PHP install.

Install memcached

  1. Download the Memcached Win32 library here: http://code.jellycan.com/memcached. Just get the Win32 binary (direct link). Extract the downloaded archive file in a directory (e.g. c:\memcached). There should be a memcached.exe in there.
  2. Run a command prompt as an administrator. Some info on how to do that here. 【可能就是因为这个,所以我无法装成服务???】
  3. Install memcached as a service. Go to the memcached directory, type and run:

    memcached -d install

    If you get an error saying “MSVCP71.dll is missing”, see this page for a solution.

  4. Start the memcached service by running:

    memcached -d start
  5. You can verify if memcached is running by executing this in the command line:

    wmic process get description, executablepath | findstr memcached.exe

    You should see a result list showing memcached.exe and its full path.

Install PHP Memcache extension (php_memcache.dll)

  1. Chances are you don’t have php_memcache.dll in your PHP extensions yet. You can download a build of it here. Basu has noted in the comments that VC6 builds are no longer available from that link. You can download the correct build here.
  2. The archive should contain php_memcache.dll. Extract the archive to your php extensions directory. On my system (WampServer), this was C:\wamp\bin\php\php5.3.0\ext.
  3. Edit php.ini, add this line to enable the extension:

    extension=php_memcache.dll

    Or if you’re using WampServer, restart it and enable the extension through the WampServer system tray menu.

Test

Test the installation using the sample PHP code here: http://www.php.net/manual/en/memcache.examples-overview.php.

Tags: memcached

学习HTML中的意外发现:xmp,plaintext

最近被人劝唆,准备捡起HTML了。。想从事一下DIV CSS的工程,准备转行做前端吧。。。说说而已,能力太差。
早上借了强哥的书在看,随手一翻,居然翻到了一页,说是在HTML页面里把HTML标记正常显示出来,这个标记好象以前都没有看过。以前只注意了把HTML内容格式化输出。

这两个标签是xmp和plaintext。

例如<xmp><input type="text" /></xmp><plaintext><input type="text" />

这两个标签里的内容,在firebug里可以看出来,被转成类似htmlspecialchar处理过的代码,所以,它们就直接显示在页面上了,也就是说,它里面的内容,如果想再转为标准的DOM结构,还得再作处理,怪不得很多大网站在做DOM转换的时候,都是情愿存存储到textarea里,毕竟在文本框里面,HTML都不用做转义,那是多么开心的事情啊。。。

PS:plaintext是单标签,所以,在它之处的所有内容全部变成了HTML输出了。。。小心使用啊。

白激动了。

Tags: html, xmp, plaintext

UCHOME开发中遇到的问题

最近在做uchome二次开发的时候遇到一个小问题:强制登录。
什么是强制登录,即,我们在任何 时候,只要点击登录,就显示登录框,同时清除原来所有的登录状态信息。为什么呢,是因为担心在同步登录的时候,如果有某个频道没有同步成功,在点击登录的时候,会显示当前用户已登录,而不会触发登录了。所以必须强制清除登录状态。

于是根据这个需求,简单的在do_login里进行了cleancookie处理。然后而是加了一句:unset($_SGLOBAL['supe_uid']);

结果,几个FORM框里生成的formhash()值都不一样了。导致直接在提交的时候说来路不正常。

检查了很久(不过这次不是我解决的),发现居然是那个unset($_SGLOBAL['supe_uid']);导致的,但是看了formhash这个函数,里面是针对$_SGLOBAL['formhash']变量,没有和supe_uid有关。。。

所以感觉有点妖异。包括把$_SGLOBAL['supe_uid']设为0,设为null都没有关系,只是unset后就出错了。。。

又是一个没有时间解决的问题,先知道解决方法就成了。。。。记录一下。

Tags: uchome, unset, sglobal