View previous topic :: View next topic |
Author |
Message |
G2k l33t
Joined: 06 Mar 2004 Posts: 672 Location: Rome, Italy
|
Posted: Mon Oct 11, 2004 6:49 pm Post subject: [OT] Python: programma per la data |
|
|
Ragazzi, ho scritto un programmino in python perche' 1. non ho niente da fare e 2. di recente mi si sta incasinando l'ora perche sto giocherellando un po' troppo con il mio linuzzo.
Comunque, ecco il source del programma...non e' accuratissimo e so che ci stanno altri programmi che fanno cio' che fa il mio programma con molta piu' accuratezza, pero' l'ho fatto tanto come esperienza educativa
Il problema che mi rimane e' che non sta' settando l'ora perche' per settarla bisogna avere i privilegi di super user.
Comunque....ecco il codice ^_^ Code: | #!/usr/bin/env python
##################################################
## dateCollector - Written by Daniele P.
##################################################
from urllib import urlopen
import re
import os
#Timezone selection
welcome = 'Welcome to the dateCollector program!'
print '-'*len(welcome) + '\n' + welcome + '\n' + '-'*len(welcome)
print 'Select the offset of your timezone with respect to the GMT.'
print 'Offset can be a positive integer or a negative integer (preceeded by "-")'
print 'Default is +1 since that\'s where the author lives ;-)'
offset = raw_input('\nOffset: ')
if offset == '':
url = 'http://time.gov/timezone.cgi?Eastern/d/1'
else:
url = 'http://time.gov/timezone.cgi?Eastern/d/%s' % (offset)
print "Please wait...retrieving data"
lines = urlopen(url).readlines()
for l in lines:
if re.search(r'<b>[0-2][0-9]:[0-5][0-9]:[0-5][0-9]', l):
h,m,s = l[-13:-5].split(":")
for d in lines:
for j in range(0,7):
for i in range(0,12):
days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', \
'August', 'September', 'October', 'November', 'December']
if re.search(r'%s, %s [0-3][0-9], [0-9][0-9][0-9][0-9]<br>' % (days[j],months[i]), d):
M = str(i + 1)
if M == r'[0-9]':
M = '0' + M
D = d[-13:-11]
Y = d[-9:-5]
#Format for setting date is [MMDDhhmmYYYY[.ss]]
os.system('date %s%s%s%s%s.%s' % (M,D,h,m,Y,s))
if h == r'0[0-9]' or h == '10' or h == '11':
am_pm = 'am'
else:
am_pm = 'pm'
print "\nThe date is:\n\n%s:%s:%s %s %s/%s/%s" % (h,m,s,am_pm,M,D,Y) |
_________________ Animula vagula blandula,
Hospes comesque corporis,
Quae nunc abibis in loca
Pallidula rigida nudula,
Nec ut soles dabis iocos...
- Imp. Caesar Hadrianus
Last edited by G2k on Tue Oct 12, 2004 2:10 pm; edited 1 time in total |
|
Back to top |
|
|
federico Advocate
Joined: 18 Feb 2003 Posts: 3272 Location: Italy, Milano
|
Posted: Mon Oct 11, 2004 7:14 pm Post subject: |
|
|
if os.getenv('USER')!='root':
print >> sys.stderr, 'Devi essere root per avviare questo software.'
sys.exit(-1)
Puoi eseguire un controllo come questo per verificare se l'utente che ha lanciato il software e' root o meno in modo da determinare se e' il caso di lanciare la procedura di salvataggio della data.
Tuttavia questo topic mi pare un po' [OT] _________________ Sideralis www.sideralis.org
Pic http://blackman.amicofigo.com/gallery
Arduino http://www.arduino.cc
Chi aveva potuto aveva spaccato
2000 pezzi buttati là
Molti saluti,qualche domanda
Semplice come musica punk |
|
Back to top |
|
|
G2k l33t
Joined: 06 Mar 2004 Posts: 672 Location: Rome, Italy
|
Posted: Mon Oct 11, 2004 7:18 pm Post subject: |
|
|
[OT] == Off topic?
forse... mi spiace
cmqe buona l'idea del controllo del user. Poi la provero'. Grazie del feedback _________________ Animula vagula blandula,
Hospes comesque corporis,
Quae nunc abibis in loca
Pallidula rigida nudula,
Nec ut soles dabis iocos...
- Imp. Caesar Hadrianus |
|
Back to top |
|
|
randomaze Bodhisattva
Joined: 21 Oct 2003 Posts: 9985
|
Posted: Tue Oct 12, 2004 11:24 am Post subject: |
|
|
G2k wrote: | [OT] == Off topic?
forse... mi spiace |
Beh, basta che editi il primo post e metti il tag "[OT]" a inizio post _________________ Ciao da me! |
|
Back to top |
|
|
IgaRyu Guru
Joined: 23 Jan 2003 Posts: 302 Location: Verona
|
Posted: Tue Oct 12, 2004 12:48 pm Post subject: |
|
|
ma un Code: | ebegin "Sincronizzo l'ora ..."
rdate -s -u ntp2.ien.it >> /dev/null
eend 0 |
nell'/etc/conf.d/local.start non e piu comodo ?
Iga _________________ One Flew East
One Flew West
Some Flew On The Kukool's Nest |
|
Back to top |
|
|
RedNeckCracker Tux's lil' helper
Joined: 27 Oct 2003 Posts: 146 Location: Italy
|
|
Back to top |
|
|
G2k l33t
Joined: 06 Mar 2004 Posts: 672 Location: Rome, Italy
|
Posted: Tue Oct 12, 2004 2:14 pm Post subject: |
|
|
RedNeckCracker: niente
IgaRyu: volevo scrivere il mio...rdate e' forse anche piu' preciso in quanto il mio dipende dalla velocita' della connessione internet e la velocita' del processore, pero' sul mio computer va tutto liscio come l'olio, ci sta' bene o male un secondo e mezzo di differenza se non di meno quidni non mi preoccupo. Preferisco questo metodo qua perche' cosi' non devo stare li a guardare flag inutili e decidere da quale server predere l'ora, time.gov ci stara' per molto tempo quindi come soluzione mi sembrava abbastanza ragionevole. _________________ Animula vagula blandula,
Hospes comesque corporis,
Quae nunc abibis in loca
Pallidula rigida nudula,
Nec ut soles dabis iocos...
- Imp. Caesar Hadrianus |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|