def convert_dhms_to_secs(dhms_str): # This function is the opposite of convert_secs_to_dhms. It takes in a DHMS # string and returns the total number of seconds. # # DHMS format: # DD HH:MM:SS [days|hours|minutes|seconds] munch_string = string.lowercase + string.uppercase + " :" total_seconds = 0 # We'll munch away at the string to get the info we need, in reverse order. # We'll make sure to remove all letters in the string as we munch, so we # don't accidentally preserve the unit. dhms_rem, seconds = gstr.rmunch(dhms_str,munch_string) dhms_rem, minutes = gstr.rmunch(dhms_rem,munch_string) dhms_rem, hours = gstr.rmunch(dhms_rem,munch_string) dhms_rem, days = gstr.rmunch(dhms_rem,munch_string) # Add up all of the seconds. if days != "": total_seconds += int(days) * 86400 if hours != "": total_seconds += int(hours) * 3600 if minutes != "": total_seconds += int(minutes) * 60 if seconds != "": total_seconds += int(seconds) return total_seconds
def __init__(self, arg=None, sql=False): # We can be given either an integer (the number of epoch seconds) or a # string (in our preferred format or SQL format). We'll assume that it's # epoch seconds, either in int or string format, and if Python throws an # error we'll try it as a string. If arg=None, then we'll make a MyDate # object that corresponds to right now. # We have to start with the epoch seconds. Once we have that in some way, # the process of creating the object is always the same. if arg is None: # If we were given no argument, just use now. current_time = time.time() microseconds, epoch = math.modf(current_time) epoch = int(epoch) microseconds = int(round(microseconds,6) * (10**6)) # In the case where we were given an argument. else: # Try to process it as an int or float. try: microseconds, epoch = math.modf(arg) epoch = int(epoch) microseconds = int(round(microseconds,6) * (10**6)) # If that fails, try to process it as a string. except ValueError: if not sql: epoch = date_to_seconds(arg) microseconds = 0 else: # The SQL case is much more complicated. microseconds = int(gstr.rmunch(arg,".")[1]) microseconds = float("0." + microseconds) microseconds = int(round(microseconds,6) * (10**6)) sql_dummy_str = "#(CDT) " + sql_date_to_std(arg) epoch = date_to_seconds(sql_dummy_str) # Now we use this function to set all of the time data. self.apply_date_from_epoch(epoch, microseconds)