Posts (Latest 10 updated) : Read all

Link List (Edit):
Contents:
  1. Working with datetime and time.time in python
    1. Converting time.time to datetime
    2. Working with time objects and timedelta()
      1. Getting setup for work
      2. The Code
#  ____        _       _   _
# |  _ \  __ _| |_ ___| |_(_)_ __ ___   ___
# | | | |/ _` | __/ _ \ __| | '_ ` _ \ / _ \
# | |_| | (_| | ||  __/ |_| | | | | | |  __/
# |____/ \__,_|\__\___|\__|_|_| |_| |_|\___|
#
#     _    _   _ ____
#    / \  | \ | |  _ \
#   / _ \ |  \| | | | |
#  / ___ \| |\  | |_| |
# /_/   \_\_| \_|____/
#
#  _____ _                 _   _
# |_   _(_)_ __ ___   ___ | |_(_)_ __ ___   ___
#   | | | | '_ ` _ \ / _ \| __| | '_ ` _ \ / _ \
#   | | | | | | | | |  __/| |_| | | | | | |  __/
#   |_| |_|_| |_| |_|\___(_)__|_|_| |_| |_|\___|
#

Working with datetime and time.time in python

Working with time functions in python really sucks.

Converting time.time to datetime

"""x = datetime_obj"""
time_obj = time.mktime(x.timetuple()) + x.microsecond / 1E6

Working with time objects and timedelta()

(AND then for some reason prose does not want me to paste entries.)

Getting setup for work

import re
import time
from datetime import date, datetime, timedelta
# Admittedly, I cannot remember why scheduler was imported.
from scheduler import Scheduler

today = datetime.today()
schedule = Scheduler()
format = '%m/%d/%Y %I:%M %p'

The Code

# Lets turn this lovely forumula into a function now
def convert_to_timetime(dt):
    time_obj = time.mktime(dt.timetuple()) + dt.microsecond / 1E6
    return time_obj
#Acquire the current time as time.time obj
current_time = time.localtime(time.time())
import re
# acquire a datetime obj from a time.time obj
def get_dt_now(datetime_obj):
    date_num = re.findall(r'\d+', str(datetime_obj))
    dti = list(map(int, date_num))
    yr = dti[0]
    mnt = dti[1]
    day = dti[2]
    hr = dti[3]
    mnu = dti[4]
    if dti[5]:
        sec = dti[5]
    else:
        sec = 0
    return datetime(yr, mnt, day, hr, mnu, sec)
def get_atime(dt):
    sec = 0
    minute = int(dt.minute) + 3
    year = dt.year
    month = dt.month
    day = dt.day
    hour = int(dt.hour) + 1
    return datetime(year, month, day, hour, minute, sec)
#Acquire a date time obj for now
datetime_now = get_dt_now(current_time)

# Now get a time in the future in datetime obj form
future_time = get_atime(datetime_now)

"""
    Now convert both from a datetime obj to a time obj
"""
time_now = convert_to_timetime(datetime_now)
new_future = convert_to_timetime(future_time)

"""
    Now lets acquire the difference and reflect it to a timedelta obj
"""
difference = int(new_future) - int(time_now)
print('Seconds: ' + difference)

Perhaps the most important - strfttime