栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

MySQL为什么不支持毫秒/微秒精度?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MySQL为什么不支持毫秒/微秒精度?

我找到了解决方法!它非常干净,不需要更改任何应用程序代码。这适用于Doctrine,也可以应用于其他ORM。

基本上,将时间戳存储为字符串。

如果日期字符串格式正确,则比较和排序有效。当传递日期字符串时,MySQL时间函数将截断微秒部分。如果不需要微秒精度,则可以

date_diff

SELECT DATEDIFF('2010-04-04 17:24:42.000000','2010-04-04 17:24:42.999999');> 0SELECT microsecond('2010-04-04 17:24:42.021343');> 21343

我最终写了一个

MicroTimestampable
实现此目标的类。我只是
actAs:MicroTimestampable
用MySQL和Doctrine来注释我的字段,等等,时间精度。

Doctrine_Template_MicroTimestampable

class Doctrine_Template_MicroTimestampable extends Doctrine_Template_Timestampable{        protected $_options = array('created' =>  array('name'          =>  'created_at',        'alias'         =>  null,        'type'          =>  'string(30)',        'format'        =>  'Y-m-d H:i:s',        'disabled'      =>  false,        'expression'    =>  false,        'options'       =>  array('notnull' => true)),          'updated' =>  array('name'          =>  'updated_at',        'alias'         =>  null,        'type'          =>  'string(30)',        'format'        =>  'Y-m-d H:i:s',        'disabled'      =>  false,        'expression'    =>  false,        'onInsert'      =>  true,        'options'       =>  array('notnull' => true)));        public function setTableDefinition()    {        if ( ! $this->_options['created']['disabled']) { $name = $this->_options['created']['name']; if ($this->_options['created']['alias']) {     $name .= ' as ' . $this->_options['created']['alias']; } $this->hasColumn($name, $this->_options['created']['type'], null, $this->_options['created']['options']);        }        if ( ! $this->_options['updated']['disabled']) { $name = $this->_options['updated']['name']; if ($this->_options['updated']['alias']) {     $name .= ' as ' . $this->_options['updated']['alias']; } $this->hasColumn($name, $this->_options['updated']['type'], null, $this->_options['updated']['options']);        }        $this->addListener(new Doctrine_Template_Listener_MicroTimestampable($this->_options));    }}

Doctrine_Template_Listener_MicroTimestampable

class Doctrine_Template_Listener_MicroTimestampable extends Doctrine_Template_Listener_Timestampable{    protected $_options = array();        public function __construct(array $options)    {        $this->_options = $options;    }        public function getTimestamp($type, $conn = null)    {        $options = $this->_options[$type];        if ($options['expression'] !== false && is_string($options['expression'])) { return new Doctrine_expression($options['expression'], $conn);        } else { if ($options['type'] == 'date') {     return date($options['format'], time().".".microtime()); } else if ($options['type'] == 'timestamp') {     return date($options['format'], time().".".microtime()); } else {     return time().".".microtime(); }        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/408152.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号