converting user input to unix timestamp

This is the place for queries that don't fit in any of the other categories.

converting user input to unix timestamp

Postby metulburr » Tue Mar 12, 2013 10:34 pm

A Year ago i made a quit smoking program. Now i want to revise it, but being the fact that i wrote it a year ago, i hate it, the code. I wanted to redo it completely so i can add things a lot easier when needed. So now i am starting from scratch.

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
import time

class TimeConvert:
   def __init__(self):
      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.convert_epoch_to_string(time.time(), pattern=new_pattern)
      self.pattern = new_pattern


timer = TimeConvert()

def test(timer):
   print(timer.time_stamp)
   print(timer.pattern)
   print(timer.time_string)
   
#test(timer)

timer.convert_string_to_epoch('03/11/2013 11:10 AM')

test(timer)

timer.convert_epoch_to_string(time.time())

test(timer)

timer.update_pattern('%m/%d/%Y %I:%M %p')

test(timer)


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
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: converting user input to unix timestamp

Postby metulburr » Wed Mar 13, 2013 12:05 am

ah nevermind, i think this is what i am looking for:
Code: Select all
import datetime
import time

date = '03/11/2013 4:02 PM'
pattern = '%m/%d/%Y %I:%M %p'
dt = datetime.datetime.strptime(date, pattern)
epoch = int(time.mktime(dt.timetuple()))
print(epoch)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests