Skip to content

Commit b323255

Browse files
committed
!stardate command
1 parent a9a7b0a commit b323255

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ Options:
3636
* greetings
3737
* diceroller
3838
* random number
39+
* Star Trek stardate

bot_core/irc_bot.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from plugins.welcome_machine import WelcomeMachine
1212
from plugins.dice_roller import DiceRoller
1313
from plugins.reddit import RedditManager
14+
from plugins.star_date import StarDate
1415

1516
class IRCBot(irc.IRCClient):
1617

@@ -184,6 +185,9 @@ def evaluate_command(self, user, channel, msg):
184185

185186
elif msg == "!randtime":
186187
self.msg(channel, "Random Hour:Minute %d:%.2d" % (random.randint(0,23), random.randint(0,59)) )
188+
189+
elif msg == "!stardate":
190+
self.msg(channel, StarDate.current_stardate())
187191

188192
elif msg.startswith( ('!commands', '!help') ):
189193
if len(msg_splits) == 1:
@@ -270,7 +274,7 @@ def _help_command(self, command=None):
270274
"""This method returns the help message"""
271275

272276
help_msg = "Valid commands: !help <command>, !commands, !karma [user], !roll Nd(3|4|6|8|10|20), !rand arg, !randtime,"
273-
help_msg += "!reddit [entries] [subject], !lastseen USER, !beskarma, !worstkarma, !bestwords, !worstwords"
277+
help_msg += "!reddit [entries] [subject], !lastseen USER, !beskarma, !worstkarma, !bestwords, !worstwords, !stardate"
274278

275279
if command is not None:
276280

@@ -305,6 +309,9 @@ def _help_command(self, command=None):
305309
elif command == "worstkarma":
306310
help_msg = "!worstkarma returns a list of the worst karmed users. Short form: !wk"
307311

312+
elif command == "stardate":
313+
help_msg = "!stardate returns the current Star Trek stardate (info [it] http://it.wikipedia.org/wiki/Data_stellare - Test: http://goo.gl/DEc4n)"
314+
308315
else:
309316
help_msg = "%s is not a valid command!" % command
310317

plugins/star_date.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from datetime import datetime
2+
from datetime import date
3+
import time
4+
import calendar
5+
6+
# Test with http://www.hillschmidt.de/gbr/sternenzeit.htm
7+
8+
# Constants
9+
10+
K_NORMAL = 3.170979 * (10**-5)
11+
# LEAP YEARS
12+
K_LEAP = 3.162315 * (10**-5)
13+
# it could be also 2161 (birth of the United Planet Federation) or 2323
14+
FOUNDATION_YEAR = 2323
15+
MEAN_DAYS_IN_YEAR = 365.2425
16+
17+
class StarDate(object):
18+
''' Star Trek star date generator '''
19+
20+
@staticmethod
21+
def current_stardate():
22+
'''Returns the current stardate (float) in the classic Star Trek format'''
23+
# UTC datetime.now()
24+
now = datetime.fromtimestamp(time.mktime(time.localtime()))
25+
earth_date = date(now.year,1,1)
26+
# delta from the first day of the year
27+
delta = now.date() - earth_date
28+
# Find if this year is leap
29+
k = K_NORMAL if not calendar.isleap(now.year) else K_LEAP
30+
# Computing star date
31+
first = (now.year - FOUNDATION_YEAR) + (delta.days / MEAN_DAYS_IN_YEAR)
32+
second = now.hour * 3600 + now.minute * 60 + now.second
33+
stardate = first * 1000 + second * k
34+
# Return a stardate with only two decimal digits
35+
return "Captain's log, stardate %.2f." % stardate
36+
37+
if __name__ == "__main__":
38+
print StarDate.current_stardate()
39+
40+
41+

0 commit comments

Comments
 (0)