日期格式化字符串:DATE_FORMAT = "%Y-%m-%d"

日期时间格式字符串:DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"

日期时间格式字符串(包含毫秒):DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S.%f"

OpenERP对象中字段赋值为当前日期(字符串):fields.date.context_today,fields.date.context_today(self, cr, uid, context=context),fields.date.today()

OpenERP对象中字段赋值为当前时间(字符串):fields.datetime.now(),fields.datetime.context_timestamp(cr, uid, datetime.now(), context=context)

OpenERP官方建议 date/datetime 的默认值的写法是fields.date.context_today,fields.datetime.now()

字符串转换为日期时间:datetime.datetime.strptime(sale.date, DATE_FORMAT)

日期时间转换为字符串:datetime.datetime.strftime(datetime.date.today(), DATE_FORMAT)

python中获取当前日期:datetime.date.today()

python中获取当前时间:datetime.datetime.now()

OpenERP fields 单元中对 date/datetime 类中方法定义如下所示:

class date(_column):
_type = 'date' @staticmethod
def today(*args):
""" Returns the current date in a format fit for being a
default value to a ``date`` field. This method should be provided as is to the _defaults dict, it
should not be called.
"""
return DT.date.today().strftime(
tools.DEFAULT_SERVER_DATE_FORMAT) @staticmethod
def context_today(model, cr, uid, context=None, timestamp=None):
"""Returns the current date as seen in the client's timezone
in a format fit for date fields.
This method may be passed as value to initialize _defaults. :param Model model: model (osv) for which the date value is being
computed - automatically passed when used in
_defaults.
:param datetime timestamp: optional datetime value to use instead of
the current date and time (must be a
datetime, regular dates can't be converted
between timezones.)
:param dict context: the 'tz' key in the context should give the
name of the User/Client timezone (otherwise
UTC is used)
:rtype: str
"""
today = timestamp or DT.datetime.now()
context_today = None
if context and context.get('tz'):
tz_name = context['tz']
else:
tz_name = model.pool.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz']
if tz_name:
try:
utc = pytz.timezone('UTC')
context_tz = pytz.timezone(tz_name)
utc_today = utc.localize(today, is_dst=False) # UTC = no DST
context_today = utc_today.astimezone(context_tz)
except Exception:
_logger.debug("failed to compute context/client-specific today date, "
"using the UTC value for `today`",
exc_info=True)
return (context_today or today).strftime(tools.DEFAULT_SERVER_DATE_FORMAT) class datetime(_column):
_type = 'datetime'
@staticmethod
def now(*args):
""" Returns the current datetime in a format fit for being a
default value to a ``datetime`` field. This method should be provided as is to the _defaults dict, it
should not be called.
"""
return DT.datetime.now().strftime(
tools.DEFAULT_SERVER_DATETIME_FORMAT) @staticmethod
def context_timestamp(cr, uid, timestamp, context=None):
"""Returns the given timestamp converted to the client's timezone.
This method is *not* meant for use as a _defaults initializer,
because datetime fields are automatically converted upon
display on client side. For _defaults you :meth:`fields.datetime.now`
should be used instead. :param datetime timestamp: naive datetime value (expressed in UTC)
to be converted to the client timezone
:param dict context: the 'tz' key in the context should give the
name of the User/Client timezone (otherwise
UTC is used)
:rtype: datetime
:return: timestamp converted to timezone-aware datetime in context
timezone
"""
assert isinstance(timestamp, DT.datetime), 'Datetime instance expected'
if context and context.get('tz'):
tz_name = context['tz']
else:
registry = openerp.modules.registry.RegistryManager.get(cr.dbname)
tz_name = registry.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz']
if tz_name:
try:
utc = pytz.timezone('UTC')
context_tz = pytz.timezone(tz_name)
utc_timestamp = utc.localize(timestamp, is_dst=False) # UTC = no DST
return utc_timestamp.astimezone(context_tz)
except Exception:
_logger.debug("failed to compute context/client-specific timestamp, "
"using the UTC value",
exc_info=True)
return timestamp

应用示例代码: 

    #自动获取日期对应的月份并保存

    def _get_month(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        if context is None:
            context = {}
        DATETIME_FORMAT = "%Y-%m-%d"
        for sale in self.browse(cr, uid, ids, context=context):
            saledate = datetime.datetime.strptime(sale.date, DATETIME_FORMAT)
            res[sale.id] = saledate.strftime('%Y') + '-' + saledate.strftime('%m')
        return res
    
    _columns={
        'name':fields.char(u'单号', size=64, select=True, required=True, readonly=True),
        'date':fields.date(u'日期', select=True, required=True, readonly=True),
        'month':fields.function(_get_month, method=True, type='char', size=10, string = u'月份', store=True, invisible=True),
    }

_defaults={
        'name': lambda obj, cr, uid, context: '/',
        'date':fields.date.context_today,
        #'employee_id':_employee_get,
        'state':'draft'
    }

    #自动计算到期日期,按开卡日期加 年数*365 天

    def _get_due_date(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        if context is None:
            context = {}
        DATE_FORMAT = "%Y-%m-%d"
        for rec in self.browse(cr, uid, ids, context=context):
            category = rec.category
            if category:
                remaining_times=category.times_limit
                if rec.active_date:
                    res[rec.id]=(datetime.datetime.strptime(rec.active_date, DATE_FORMAT) +  datetime.timedelta(days=category.age_limit*365)).strftime(DATE_FORMAT)
                else:
                    res[rec.id]=(datetime.date.today()+  datetime.timedelta(days=category.age_limit*365)).strftime(DATE_FORMAT)
        return res
    
    _columns={
        "name":fields.char("卡号",size=64, required=True, readonly=True, states={'0':[('readonly',False)]}),
        "category":fields.many2one("dispatch.service_card_category","服务卡类型", required=True, readonly=True, states={'0':[('readonly',False)]}),
        'age_limit':fields.related('category', 'age_limit', string=u'年限', type='float', readonly=True, store=True),
        "times_limit":fields.integer(u"初始次数", readonly=True),
        "customer":fields.many2one("dispatch.customer","客户",required=True, select=True, readonly=True, states={'0':[('readonly',False)]}),
        "remaining_times":fields.integer("剩余次数",required=True, readonly=True, states={'0':[('readonly',False)]}),
        "active_date":fields.date("开卡日期",required=True, readonly=True, states={'0':[('readonly',False)]}),
        'due_date':fields.function(_get_due_date, method=True, type='date', string = u'到期日期', store=True),
        'state': fields.selection([('0', u'未开卡'),('1', u'已开卡'),('2', u'已用完'),('3', u'已过期'),('4', u'已锁定')], u'状态',required=True, readonly=True),
        'lock_note': fields.char(u'锁定原因', size=200, invisible=False, readonly=True, states={'1':[('readonly',False)], '4':[('readonly',False)]}),
    }

# TODO: can be improved using resource calendar method
    #计算日期间隔对应的天数
    def _get_number_of_days(self, date_from, date_to):
        """Returns a float equals to the timedelta between two dates given as string."""

DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
        from_dt = datetime.datetime.strptime(date_from, DATETIME_FORMAT)
        to_dt = datetime.datetime.strptime(date_to, DATETIME_FORMAT)
        timedelta = to_dt - from_dt
        diff_day = timedelta.days + float(timedelta.seconds) / 86400
        return diff_day
    
    #对象字段
    _columns = {
            'date_from': fields.datetime(u'起始日期',required=True, readonly=True, states={'draft':[('readonly',False)]}, select=True),
            'date_to': fields.datetime(u'结束日期', readonly=True, states={'draft':[('readonly',False)]}),
            'days': fields.float(u'天数', digits=(8, 2), readonly=True, states={'draft':[('readonly',False)]}),
          }
        
    #更改起始日期,自动计算请假天数
    def onchange_date_from(self, cr, uid, ids, date_to, date_from):
        """
        If there are no date set for date_to, automatically set one 8 hours later than
        the date_from.
        Also update the number_of_days.
        """
        # date_to has to be greater than date_from
        if (date_from and date_to) and (date_from > date_to):
            raise osv.except_osv(_(u'警告!'),_(u'开始日期必须小于结束日期.'))

result = {'value': {}}

# No date_to set so far: automatically compute one 8 hours later
        if date_from and not date_to:
            date_to_with_delta = datetime.datetime.strptime(date_from, tools.DEFAULT_SERVER_DATETIME_FORMAT) + datetime.timedelta(hours=8)
            result['value']['date_to'] = str(date_to_with_delta)

# Compute and update the number of days
        if (date_to and date_from) and (date_from <= date_to):
            diff_day = self._get_number_of_days(date_from, date_to)
            result['value']['days'] = round(math.floor(diff_day))+1
        else:
            result['value']['days'] = 0

return result
    
    #更改结束日期,自动计算请假天数
    def onchange_date_to(self, cr, uid, ids, date_to, date_from):
        """
        Update the number_of_days.
        """
        # date_to has to be greater than date_from
        if (date_from and date_to) and (date_from > date_to):
            raise osv.except_osv(_(u'警告!'),_(u'开始日期必须小于结束日期.'))

result = {'value': {}}

# Compute and update the number of days
        if (date_to and date_from) and (date_from <= date_to):
            diff_day = self._get_number_of_days(date_from, date_to)
            result['value']['days'] = round(math.floor(diff_day))+1
        else:
            result['value']['days'] = 0

return result

odoo开发笔记-日期时间相关操作的更多相关文章

  1. odoo开发笔记--日期or时间字段给定默认值

    开发中经常有这样的场景,需要给某个日期或者时间的字段默认值: 例如: 日期,默认今天 时间,默认当前时间 可以在odoo模型定义中进行设置, 如下样例提供参考: test_data = fields. ...

  2. openerp学习笔记 日期时间相关操作

    日期格式化字符串:DATE_FORMAT = "%Y-%m-%d" 日期时间格式字符串:DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" ...

  3. odoo开发笔记 -- div标签代替odoo button写法

    odoo开发笔记 -- div标签代替odoo button写法 并调用自定义js <footer> <div id="confirm_request_cloud_repo ...

  4. odoo开发笔记 -- 搜索视图继承扩展

    odoo开发笔记 -- 搜索视图继承扩展

  5. odoo开发笔记 -- 后台日志输出及分析

    odoo开发笔记 -- 后台日志输出及分析 附:日志分析软件

  6. odoo开发笔记 -- self详解

    python中一切皆对象! odoo基于python开发,那么odoo中也可以理解成一切皆对象. 我们在python中定义类的时候,一般会用到self,用来表示当前对象自己. 那么odoo中的self ...

  7. odoo开发笔记--字段追踪,消息通知机制

    odoo有着强大的消息记录.通知机制: 实际开发中,常常会有客户的需求,页面上form视图中的某些字段不允许反复修改, 假如有的用户修改了,恶意搞坏,往往容易给公司利益造成损失,或破坏,那么如何有效的 ...

  8. odoo开发笔记:Server+Action服务器动作自动触发执行

           Odoo的市场定位是SME(中小型企业),这个市场的ERP产品,多如牛毛,产品各具特色.不过,Odoo的自动化处理机制,可以睥睨天下,无人能及.包括一些大型国产软件,如用友.金蝶也不具备 ...

  9. odoo开发笔记 -- 用户配置界面如何增加模块访问权限

    在odoo设置界面,点击用户,进入用户配置界面,会看到: 访问权 | 个人资料菜单 在访问权 page菜单界面,可以看到系统预制的一些模块都会显示在这里, 那么,我们自己开发的模块如何显示在这块呢,从 ...

随机推荐

  1. 第29章:MongoDB-索引--全文索引

    ①全文索引 全文索引是用于对长文本检索来使用的,是用正则表达式只能对字符串类型的值进行检索.注意:创建索引是一件比较耗时耗费资源的事情,而全文索引更是耗时更厉害,如果对索引键的内容比较长,需要对内容进 ...

  2. 写了十年JS却不知道模块化为何物?

    作者:肖光宇 野狗科技联合创始人,先后在猫扑.百度.搜狗任职,爱折腾的前端工程师. 野狗官博:https://blog.wilddog.com/ 野狗官网:https://www.wilddog.co ...

  3. 信息安全技术PGP实验 20155205 20155218

    信息安全技术PGP实验 一.实验过程 PGP的安装与设置(Mac版) 访问GPGTools网站,下载GPG Suite for OS X,下载完毕,双击dmg文件,运行安装操作. 创建PGP密钥(一定 ...

  4. Redis集群命令行部署工具

    使用之前准备工作: 1)配置好与端口无关的公共redis.conf文件,和工具放在同一目录下 2)配置好与端口相关的模板redis-PORT.conf文件,也和工具放在同一目录下(部署时PORT会被替 ...

  5. C#重点内容之:委托(delegate)

    为了记忆方便,提取了重点. 委托类似于指针,可以理解为函数指针的升级版,这是理解委托最关键的地方. Action和Func 系统自带的两种委托: Action Func Action型委托要求委托的目 ...

  6. 批量插入,批量修改的sql

    sql 1  批量插入 <insert id="batchInsert" useGeneratedKeys="true" parameterType=&q ...

  7. js 判断字符串中是否包含某个字符串(转载)

    from : https://www.cnblogs.com/ooo0/p/7741651.html String对象的方法 方法一: indexOf()   (推荐) var str = " ...

  8. 最大m段子段和

    hdu1024 最大m子序列和 给定你一个序列,让你求取m个子段(不想交的子段)并求取这m个子段和的最大值 从二维开始来看dp[i][j]表示取第j个数作为第i个子段的元素所得到的前i个子段和的最大值 ...

  9. nodes 验证码

    一个常见的需求. 知乎上得讨论:http://www.zhihu.com/question/32156977 node-canvas 在mac上安装有问题,可能是我没有sudo 使用了ccap:挺不错 ...

  10. js-图片轮播

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...