| ||||||||||||||||||||||||||||||||
Popular topics
| Copy to clipboard
#========================================================
# _DateTimeMacros.py
# Mark Pickin March 2006
# edited for instructions November 07
# Global Natlink macro to insert common date formats into open document
# Vocola utilities not used in this version
# To use, use the copy to clipbard button and put in your natlink user directory as "_DateTimeMacros.py"
# it is important the the name begins with an underscore to allow global access
# Your Natlink user directory is shown the in "Configure Natlink & Vocola" utility
#========================================================
import natlink
from natlinkutils import *
from time import *
class ThisGrammar(GrammarBase):
gramSpec = """
<1> = 'Test Date and Time Macros';
<2> = 'Insert Short Date';
<3> = 'Insert Long Date';
<4> = 'Insert' ('Day and'|'Full') 'Date';
<any> = <1>|<2>|<3>|<4>;
<sequence> exported = <any>;
"""
def initialize(self):
self.load(self.gramSpec)
self.currentModule = ("","",0)
self.ruleSet1 = ['sequence']
def gotBegin(self,moduleInfo):
window = moduleInfo[2]
self.firstWord = 0
# Return if same window and title as before
if moduleInfo == self.currentModule: return None
self.currentModule = moduleInfo
self.deactivateAll()
title = string.lower(moduleInfo[1])
if string.find(title,'') >= 0:
for rule in self.ruleSet1:
self.activate(rule)
#========================================================
# numsuffixed... strips leading zeros and adds st, nd etc
# generic to deal with all numbers 1st, 111th 11131st etc
#========================================================
def numsuffixed(self,n):
while n[0] == "0":
n = n[1:] # end of while loop, remove indent
n2 = n[-2:] # need last two "digits" for 11th etc
n1 = n[-1] # just last for all others
if n2 == "11": # 11, 12, 13 are exceptions
sfx = "th"
elif n2 == "12":
sfx = "th"
elif n2 == "13":
sfx = "th"
elif n1 == "1": # all other number strings
sfx = "st"
elif n1 == "2":
sfx = "nd"
elif n1 == "3":
sfx = "rd"
else:
sfx = "th"
return n+sfx
#==========================================
def getFormatedDate(self,dateFormat):
current_time = localtime(time())
x = strftime(dateFormat,current_time)
return x
#==========================================
# Test Date and time macros
def gotResults_1(self, words, fullResults):
natlink.playString('Arrived in Date Time Macros')
#== this would be the Vocola way of doing the line above
#== using the Vocola utilities
# actions = Value()
# actions.augment('Arrived in Date TimeMacros')
# actions.perform()
###########
# Insert Short Date is the command and is command 2 on the gramspec list above
# if you wish to change the name.. do it in Grampec
# New commands need to be added to Gramspec and Called as "def gotResults_#(self, words, fullResults):
# in the general format shown below where # is the number of the command.. the indent is essential!!
############
# Insert Short Date (22.03.2006 )
def gotResults_2(self, words, fullResults):
dateFormatString = " %d.%m.%y "
formatedDate = self.getFormatedDate(dateFormatString)
natlink.playString(formatedDate)
# Insert Long Date (22nd March 2006 )
def gotResults_3(self, words, fullResults):
dateFormatString = "%d"
dayofmonth = self.getFormatedDate(dateFormatString)
dayofmonth = self.numsuffixed(dayofmonth)
dateFormatString = " %B %Y "
dateString = dayofmonth + self.getFormatedDate(dateFormatString)
natlink.playString(dateString)
# Insert (Day and Date |Full Date) (Tuesday 4th April 2006)
def gotResults_4(self, words, fullResults):
dayofweekFormat = "%A"
dayofweek = self.getFormatedDate(dayofweekFormat)
dateFormatString = "%d"
dayofmonth = self.getFormatedDate(dateFormatString)
dayofmonth = self.numsuffixed(dayofmonth)
dateFormatString = " %B %Y "
dateString = " " + dayofweek + " " +dayofmonth + self.getFormatedDate(dateFormatString)
natlink.playString(dateString)
#========================================================
thisGrammar = ThisGrammar()
thisGrammar.initialize()
def unload():
global thisGrammar
if thisGrammar: thisGrammar.unload()
thisGrammar = None
##================= TIME CODES ===========================
#=== Included to facilitate future customisation
#=== Substitute these codes in place of the % codes above
#
#
# %a Locale's abbreviated weekday name.
# %A Locale's full weekday name.
# %b Locale's abbreviated month name.
# %B Locale's full month name.
# %c Locale's appropriate date and time representation.
# %d Day of the month as a decimal number [01,31].
# %H Hour (24-hour clock) as a decimal number [00,23].
# %I Hour (12-hour clock) as a decimal number [01,12].
# %j Day of the year as a decimal number [001,366].
# %m Month as a decimal number [01,12].
# %M Minute as a decimal number [00,59].
# %p Locale's equivalent of either AM or PM. (1)
# %S Second as a decimal number [00,61]. (2)
# %U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
# %w Weekday as a decimal number [0(Sunday),6].
# %W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
# %x Locale's appropriate date representation.
# %X Locale's appropriate time representation.
# %y Year without century as a decimal number [00,99].
# %Y Year with century as a decimal number.
#==========================================================
| |||||||||||||||||||||||||||||||