【Python小随笔】时间戳互转时间格式,IPV4互转整型

def change_time(timeStamp):
    """
        :param timeStamp: 时间戳
        :return: 时间格式
    """
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    minuteTime = time.strftime("%H:%M", timeArray)
    return otherStyleTime, minuteTime


def return_timeStamp(times):
    """
        :param time: 时间格式  "%Y-%m-%d %H:%M:%S"
        :return: 时间戳
    """
    timeArray = time.strptime(times, "%Y-%m-%d %H:%M:%S")
    timeStamp = int(time.mktime(timeArray))
    return timeStamp

def inet_aton(ip_string):
    """ 将 ip 字符串转化为整型 """
    return sum([256 ** j * int(i) for j, i in enumerate(ip_string.split(.)[::-1])])


def inet_ntoa(ip_int):
    """ 将整型转化为 ip 字符串 """
    return ..join([str(math.floor(ip_int / (256 ** i)) % 256) for i in range(3, -1, -1)])


def network_segment_toip(ipv4):
    """
        网段格式 转换成 IP范围
        :return 起始IP,范围IP

        不支持跨B类IP转换
    """
    start_ip, ip_subnet_mask = ipv4.split("/")
    ipv4_split = ipv4.split(.)
    ip_count = 2 ** (32 - int(ip_subnet_mask)) - 2
    if ip_count / 256 > 1:
        c_rem = int(ip_count / 256)
        end_rem = ip_count - (c_rem * 256)
        end_ipv4 = ipv4_split[0] + "." + ipv4_split[1] + "." + str(c_rem) + "." + str(end_rem)
    else:
        end_ipv4 = ipv4_split[0] + "." + ipv4_split[1] + "." + ipv4_split[2] + "." + str(ip_count)

    return start_ip, end_ipv4

 

【Python小随笔】时间戳互转时间格式,IPV4互转整型

上一篇:十分钟带你回顾Spring常问的知识点,2021年Java高级面试题总结


下一篇:java命令行命令