clock.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import time
  5. import shutil
  6. import cPickle
  7. import subprocess
  8. from hashlib import sha1
  9. from pardus import csapi
  10. # real script
  11. tz_file = "/etc/localtime"
  12. tz_info = "/etc/timezone"
  13. tz_zones = "/usr/share/zoneinfo/"
  14. tz_dict = "/usr/share/zoneinfo/zoneinfo.dict"
  15. def getTimeZone():
  16. if os.path.exists(tz_dict) and os.path.exists(tz_file):
  17. tzdb = cPickle.Unpickler(open(tz_dict, "rb")).load()
  18. tz = tzdb.get(sha1(open(tz_file, "r").read()).hexdigest(), "/etc/localtime doesn't exist.")
  19. return tz
  20. def setTimeZone(zone=None):
  21. if zone:
  22. # Check if zone is a valid one, if not skip this call
  23. if not os.path.exists(os.path.join(tz_zones, zone)):
  24. return 1
  25. ret = subprocess.call(["/usr/sbin/zic", "-l", zone])
  26. return ret
  27. """
  28. if zone:
  29. try:
  30. if os.path.exists(tz_file):
  31. os.unlink(tz_file)
  32. except:
  33. pass
  34. # Copy the new TZ file to /etc/localtime
  35. shutil.copy(os.path.join(tz_zones, zone), tz_file)
  36. """
  37. def setDate(year=None, month=None, day=None, hour=None, minute=None, second=None):
  38. new = list(time.localtime())
  39. if year: new[0] = int(year)
  40. if month: new[1] = int(month)
  41. if day: new[2] = int(day)
  42. if hour: new[3] = int(hour)
  43. if minute: new[4] = int(minute)
  44. if second: new[5] = int(second)
  45. csapi.settimeofday(time.mktime(new))
  46. def getDate():
  47. return time.strftime("%Y %m %d %H %M %S %Z")
  48. def saveToHW():
  49. subprocess.call(["/sbin/hwclock", "--systohc"])
  50. def loadFromHW():
  51. subprocess.call(["/sbin/hwclock", "--hctosys"])