Top

gludb.utils module

Central place for misc utilities

"""Central place for misc utilities
"""

import datetime

from uuid import uuid4


def uuid():
    """Return a decent UUID as a string"""
    return uuid4().hex


def now_field():
    """Return a string we use for storing our date time values"""
    return 'UTC:' + datetime.datetime.utcnow().isoformat()


def parse_now_field(s):
    """Return a datetime instance from a string generated by now_field.
    IMPORTANT: the datetime will be in UTC"""
    if not s.startswith('UTC:'):
        return None  # Invalid string
    s = s[4:]

    # isoformat can return strings both with and without microseconds - we
    # account for both
    try:
        dt = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')
    except ValueError:
        dt = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')

    return dt

Functions

def now_field(

)

Return a string we use for storing our date time values

def now_field():
    """Return a string we use for storing our date time values"""
    return 'UTC:' + datetime.datetime.utcnow().isoformat()

def parse_now_field(

s)

Return a datetime instance from a string generated by now_field. IMPORTANT: the datetime will be in UTC

def parse_now_field(s):
    """Return a datetime instance from a string generated by now_field.
    IMPORTANT: the datetime will be in UTC"""
    if not s.startswith('UTC:'):
        return None  # Invalid string
    s = s[4:]

    # isoformat can return strings both with and without microseconds - we
    # account for both
    try:
        dt = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')
    except ValueError:
        dt = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')

    return dt

def uuid(

)

Return a decent UUID as a string

def uuid():
    """Return a decent UUID as a string"""
    return uuid4().hex