Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[OT] Python: programma per la data
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano
View previous topic :: View next topic  
Author Message
G2k
l33t
l33t


Joined: 06 Mar 2004
Posts: 672
Location: Rome, Italy

PostPosted: Mon Oct 11, 2004 6:49 pm    Post subject: [OT] Python: programma per la data Reply with quote

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 :P

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
View user's profile Send private message
federico
Advocate
Advocate


Joined: 18 Feb 2003
Posts: 3272
Location: Italy, Milano

PostPosted: Mon Oct 11, 2004 7:14 pm    Post subject: Reply with quote

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
View user's profile Send private message
G2k
l33t
l33t


Joined: 06 Mar 2004
Posts: 672
Location: Rome, Italy

PostPosted: Mon Oct 11, 2004 7:18 pm    Post subject: Reply with quote

[OT] == Off topic?
forse... mi spiace :cry:
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
View user's profile Send private message
randomaze
Bodhisattva
Bodhisattva


Joined: 21 Oct 2003
Posts: 9985

PostPosted: Tue Oct 12, 2004 11:24 am    Post subject: Reply with quote

G2k wrote:
[OT] == Off topic?
forse... mi spiace :cry:


Beh, basta che editi il primo post e metti il tag "[OT]" a inizio post ;-)
_________________
Ciao da me!
Back to top
View user's profile Send private message
IgaRyu
Guru
Guru


Joined: 23 Jan 2003
Posts: 302
Location: Verona

PostPosted: Tue Oct 12, 2004 12:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
RedNeckCracker
Tux's lil' helper
Tux's lil' helper


Joined: 27 Oct 2003
Posts: 146
Location: Italy

PostPosted: Tue Oct 12, 2004 1:08 pm    Post subject: Reply with quote

Grazie G2K, avevo giusto bisogno di vedere le regex di python con sorgente urlopen.
_________________
Rage powered

http://www.stud.ntnu.no/~shane/stasj/pics/humor/div/faster_bigger.jpg
Back to top
View user's profile Send private message
G2k
l33t
l33t


Joined: 06 Mar 2004
Posts: 672
Location: Rome, Italy

PostPosted: Tue Oct 12, 2004 2:14 pm    Post subject: Reply with quote

RedNeckCracker: niente :wink:
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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano All times are GMT
Page 1 of 1

 
Jump to:  
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