So the part where it gets fuzzy is the timestamp saved and converting it to readable and editable format. I am also not very good working with time still.
So i figured the "save file" will save the unix epoch of the time the person quit smoking. The option of reseting the quit date to "now" is easy as it is just time.time(). But getting the users input of month, day, year, hour, minute, etc. and then converting that into a unix timestamp to save it is where i am having the problem.
I may be missing a simple solution that you guys might know, but this was my first go at this problem. I figured to get the user input, convert that to unix epoch using the pattern, and save it that way?
this is the code i have made for this
- Code: Select all
self.time_string = '03/11/2013 11:05 AM'
self.pattern = '%m/%d/%Y %I:%M %p'
self.time_stamp = 0
self.tweny_four_hour_format = '%m/%d/%Y %H:%M:%S' #the H and no p
self.twelve_hour_format = '%m/%d/%Y %I:%M:%S %p' #the I for 12's, and p for AM/PM
def convert_string_to_epoch(self, string):
self.time_stamp = int(time.mktime(time.strptime(string, self.pattern)))
def convert_epoch_to_string(self, epoch, pattern=None):
if not pattern:
pattern = self.pattern
self.time_string = time.strftime(pattern, time.localtime(epoch))
def update_pattern(self, new_pattern):
self.pattern = new_pattern
timer = TimeConvert()
timer.convert_string_to_epoch('03/11/2013 11:10 AM')
timer.update_pattern('%m/%d/%Y %I:%M %p')
The problem with this method is i was going to allow the user to edit the pattern as he sees fit, which would nullify the conversion to and from string_time and epoch. I am not sure how to do both.
I have looked into datetime module, and i was first using that, but then i switched it all to use the time module. Probably beacuse i am more comfortable with it as i used it more. Not sure i havent messed with time much using python and i need to play wit hit to understand it more