robot 源码解读4【时间相关】

  1. robot中的timestamp的概念

20210406-104142.897 字符串类型

  1. 源码


import time
import sys

IRONPYTHON = sys.platform == 'cli'


def is_number(item):
    return isinstance(item, (int, float))


# 传入秒时间格式, 返回带毫秒的时间元祖格式
# 1. time.time()                    # 1617677501.859291
def _get_timetuple(epoch_secs=None):
    if epoch_secs is None:  # can also be 0 (at least in unit tests)
        epoch_secs = time.time()
    secs, millis = _float_secs_to_secs_and_millis(epoch_secs)
    timetuple = time.localtime(secs)[:6]  # from year to secs   # (2021, 4, 6, 10, 54, 4)
    # print(timetuple)              # (2021, 4, 6, 10, 42, 31)
    # print((millis,))              # (719,)
    # print(timetuple + (millis,))  # (2021, 4, 6, 10, 42, 31, 719)
    return timetuple + (millis,)


def _float_secs_to_secs_and_millis(secs):
    isecs = int(secs)
    millis = roundup((secs - isecs) * 1000)
    return (isecs, millis) if millis < 1000 else (isecs+1, 0)


def roundup(number, ndigits=0, return_type=None):
    """Rounds number to the given number of digits.

    Numbers equally close to a certain precision are always rounded away from
    zero. By default return value is float when ``ndigits`` is positive and
    int otherwise, but that can be controlled with ``return_type``.

    With the built-in ``round()`` rounding equally close numbers as well as
    the return type depends on the Python version.
    """
    sign = 1 if number >= 0 else -1
    precision = 10 ** (-1 * ndigits)   # 10 ** (-1 * 1) =0.1 ;  10 ** (-1 * 2)=0.01
    if not return_type:
        return_type = float if ndigits > 0 else int
    quotient, remainder = divmod(abs(number), precision)
    # https://github.com/IronLanguages/main/issues/1236
    if (not (IRONPYTHON and (quotient * precision + remainder > abs(number)))
        and remainder >= precision / 2):
        quotient += 1
    return sign * return_type(quotient * precision)


# 添加时区 GMT8+09:00
def _diff_to_gmt(sep):
    if not sep:
        return ''
    if time.altzone == 0:
        sign = ''
    elif time.altzone > 0:
        sign = '-'
    else:
        sign = '+'
    minutes = abs(time.altzone) / 60.0    # time.altzone = -32400
    hours, minutes = divmod(minutes, 60)
    return '%sGMT%s%s%02d:%02d' % (sep, sep, sign, hours, minutes)


# Timetuple is (year, month, day, hour, min, sec[, millis])
def format_time(timetuple_or_epochsecs, daysep='', daytimesep=' ', timesep=':',
                millissep=None, gmtsep=None):
    """Returns a timestamp formatted from given time using separators.

    Time can be given either as a timetuple or seconds after epoch.

    Timetuple is (year, month, day, hour, min, sec[, millis]), where parts must
    be integers and millis is required only when millissep is not None.
    Notice that this is not 100% compatible with standard Python timetuples
    which do not have millis.

    Seconds after epoch can be either an integer or a float.
    """
    if is_number(timetuple_or_epochsecs):
        timetuple = _get_timetuple(timetuple_or_epochsecs)
    else:
        timetuple = timetuple_or_epochsecs
    daytimeparts = ['%02d' % t for t in timetuple[:6]]  # 单数值不足2位
    day = daysep.join(daytimeparts[:3])
    time_ = timesep.join(daytimeparts[3:6])
    millis = millissep and '%s%03d' % (millissep, timetuple[6]) or ''  # millis保留3位
    return day + daytimesep + time_ + millis + _diff_to_gmt(gmtsep)



# format_time理解
start_timestamp = format_time((2021, 12, 12, 12, 12, 13,22), '', '-', '',millissep='.',gmtsep=0)   # 20211212-121213.022
start_timestamp = format_time(time.time(), '', '-', '',millissep='.',gmtsep='amize')               # 20210406-104142.897
start_timestamp = format_time(time.time(), '', '-', ':')                   # 20210413-10:01:49
print(start_timestamp)


# 函数中的一些数学用法
# print(10 ** (-1 * 1))     # 0.1
# print(10 ** (-1 * 2))     # 0.01
# print(divmod(3, 2))   # (1, 1)
# print(divmod(3, 0.1)) # (29.0, 0.09999999999999984)
quotient, remainder = divmod(abs(3), 0.1)
print(quotient * 0.1 + remainder )



# time的使用
# print(time.localtime(time.time()))         # time.struct_time(tm_year=2021, tm_mon=4, tm_mday=6, tm_hour=10, tm_min=48, tm_sec=39, tm_wday=1, tm_yday=96, tm_isdst=0)
# print(time.localtime(time.time()).tm_year) # 2021
# print(tuple(time.localtime(time.time())))  # (2021, 4, 6, 10, 48, 39, 1, 96, 0)
# r = isinstance(time.localtime(time.time()),(tuple,))  #True
# print(r)
# print(time.localtime(time.time())[:6])

r= ['%02d' % t for t in time.localtime(time.time())[:6]]
print(r)


posted @ 2021-04-13 10:07  该显示昵称已被使用了  阅读(55)  评论(0编辑  收藏  举报