Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFrank Campos
4,175 Pointswhy doesn't my timestamp hours change?
it shows all the time with the string format provided but I show the same hours. For example, Wednesday August 02, 2017 12:00AM. All entrances show the same hours but I created them at a different time.
here is my code.
#!/usr/bin/env python3
import datetime
from collections import OrderedDict
import sys
from peewee import *
db = SqliteDatabase('diary.db')
class Entry(Model):
content = TextField()
timestamp = DateField(default=datetime.datetime.now)
class Meta:
database = db
def initialize():
"""create the database and the table if they don't exist"""
db.connect()
db.create_tables([Entry], safe=True)
def menu_loop():
choice = None
while choice != 'q':
print('Enter "q" to quit.')
for key, value in menu.items():
print('{}){}'.format(key, value.__doc__))
choice = input('Action: ').lower().strip()
if choice in menu:
menu[choice]()
def add_entry():
'''add an Entry'''
print('Enter you Entry. Please enter ctrl+ D when you finished')
data = sys.stdin.read().strip() # stdin.read() capture all the data that we type on the screen
if data:
try:
if input('Save Entry y/n?').lower() != 'n':
Entry.create(content=data)
print('saved successfully! ')
except EOFError:
print("save aborted")
def views_entries(search_query=None):
'''views entries'''
entries = Entry.select().order_by(Entry.timestamp.desc())
if search_query:
entries = entries.where(Entry.content.contains(search_query))
for entry in entries:
timestamp = entry.timestamp.strftime('%A %B %d, %Y %I:%M%p')
print(timestamp)
print("=" * len(timestamp))
print(entry.content)
print('n) next entry')
print('q) return to main menu')
next_action = input("Action; [Nq)").lower().strip()
if next_action == 'q':
break
def search_entries():
'''Search entries for string'''
views_entries(input("search queries"))
def del_entry(entry):
pass
menu = OrderedDict([
('a', add_entry),
('v', views_entries),
('s', search_entries),
])
if __name__ == "__main__":
initialize()
menu_loop()
1 Answer
Mark Ramos
19,209 PointsI think that your line:
timestamp = DateField(default=datetime.datetime.now)
Should be instead:
timestamp = DateTimeField(default=datetime.datetime.now)