Newer
Older
Import / projects / LGN-IP3870 / t / new / ntptime.py
import time, os, config
#Roxia Begin jhbang 06.03.16
import calendar
#Roxia End jhbang
from roxiadebug import *


# system time과 real time과의 차이(sec.)를 얻어온다
def timediff():
	try:
		if os.path.exists(config.ntptime_file):
			fd = open(config.ntptime_file)
			line = fd.readline().strip()
			# 소수점 제거
			if line.find('.') >= 0:
				line = line[:line.find('.')]
			return float(line)
		else:
			return 0
	except:
		return 0


# 시간을 가져올 경우 꼭 ntime()함수를 사용하여야 한다.
# ex) time.localtime(ntptime.ntime())
def ntime():
	if not os.path.exists(config.ntptime_file):
		return time.time()
	else:
		diff = timediff()
		return time.time() + diff


def set_timediff(cidtime, year=''):
	import profile
	if year or 0 == profile.profile.get_profile():
		try:

			new_localtime = [0, 0, 0, 0, 0, 0, 0, 0, 0]
			sys_time = None
			#단말의 시간.

			ntpyear = time.localtime(ntime())[0]
			sys_time = time.time()
			sys_localtime = time.localtime(sys_time)
			sys_localtime = list(sys_localtime)
			sys_localtime[5] = 0
			sys_localtime[6] = 0
			sys_localtime[7] = 0
			sys_localtime[8] = 0
			sys_time = time.mktime(sys_localtime)

			new_localtime[0] = ntpyear
			if year:
				new_localtime[0] = int(year)

			new_localtime[1] = int(cidtime[0:2])
			new_localtime[2] = int(cidtime[2:4])
			new_localtime[3] = int(cidtime[4:6])
			new_localtime[4] = int(cidtime[6:8])

			new_time = time.mktime(new_localtime)

			diff = new_time - sys_time
			fd = open(config.ntptime_file, 'wb+')
			fd.write(str(diff))
			fd.close()
		except:
			return

		import status
		status.date_time_ready = True
		
#Roxia Begin jhbang 06.03.16 <ntp>
#해당월의 마지막주 일요일의 날짜를 구함
def get_last_sunday(year, month):
	cal = calendar.monthcalendar(year, month)
	sunday = cal[len(cal) - 1][6]
	if sunday == 0:
		sunday = cal[len(cal) - 2][6]
	return sunday

# 3월 마지막주 일요일에 시작
def get_summertime_begin_date(year, gmt=1):
	last_sunday = get_last_sunday(year, 3)
	try:
		begin = time.mktime((year, 3, last_sunday, 1 + gmt, 0, 0, 0, 0, 0))
	except:
		if config.ntp_debug:
			print '[ntp] error. get_summertime_begin_date(), year=' + str(year) + ', last_sunday=' + str(last_sunday)
		return 0
	return begin

# 10월 마지막주 일요일에 마침
def get_summertime_end_date(year, gmt=1):
	last_sunday = get_last_sunday(year, 10)
	try:
		end	= time.mktime((year, 10, last_sunday, 1 + gmt, 0, 0, 0, 0, 0))
	except:
		if config.ntp_debug:
			print '[ntp] error. get_summertime_end_date(), year=' + str(year) + ', last_sunday=' + str(last_sunday)
		return 0
	return end

def is_summertime_period(date):
#Roxia Begin jhbang 06.04.21
	from setting import setting

	today = time.localtime(date)
	if config.ntp_debug:
		print '***********************'
		print '[ntp] today:', today
		print '[ntp] timezone:', setting.ntp_timezone

	if setting.ntp_timezone == 'Canarias (GMT)':
		gmt = 0
	else:
		gmt = 1

	begin_summertime = get_summertime_begin_date(today[0], gmt)
	end_summertime = get_summertime_end_date(today[0], gmt)
#Roxia End jhbang

	if config.ntp_debug:
		print '[ntp] summer time begin:', time.localtime(begin_summertime)
		print '[ntp] summer time end:', time.localtime(end_summertime)

	if date >= begin_summertime and date < end_summertime:
		if config.ntp_debug:
			print '[ntp] summertime period..'
			print '***********************'
		return True
	else:
		if config.ntp_debug:
			print '***********************'
		return False

def load_ntptime():
	if os.path.exists(config.ntptime_file):
		fd = open(config.ntptime_file)
		line = fd.readline().strip()
		fd.close()
		if line.find('.') >= 0:
			line = line[:line.find('.')]
		ntptime = float(line)
#Roxia Begin jhbang 06.03.29
		if config.ntp_debug:
			print '[ntp] load ' + config.ntptime_file + '. value=' + str(ntptime)
#Roxia End jhbang
		return True, ntptime
	else:
#Roxia Begin jhbang 06.03.29
		if config.ntp_debug:
			print '[ntp] ' + config.ntptime_file + ' not found.'
		return False, 0
#Roxia End jhbang

def load_ntptime_from_msntp():
	# add shchun, 2007. 11. 20. : saparate file for ntp dispatch and ntp reference.
	#      introducing /usr/etc/ntptime_mmi
	if os.path.exists(config.ntptime_file_from_msntp):
		fd = open(config.ntptime_file_from_msntp)
		line = fd.readline().strip()
		fd.close()
		if line.find('.') >= 0:
			line = line[:line.find('.')]
		ntptime = float(line)
#Roxia Begin jhbang 06.03.29
		if config.ntp_debug:
			print '[ntp] from msntp load ' + config.ntptime_file_from_msntp + '. value=' + str(ntptime)
#Roxia End jhbang
		return True, ntptime
	else:
#Roxia Begin jhbang 06.03.29
		if config.ntp_debug:
			print '[ntp] from msntp' + config.ntptime_file_from_msntp + ' not found.'
		return False, 0
#Roxia End jhbang

def save_ntptime(ntptime):
	if None != ntptime:
		fd = open(config.ntptime_file, 'wb+')
		fd.write(str(ntptime))
		fd.close()

def set_ntpadjust():
	from setting import setting

	#result, ntptime = load_ntptime()
	result, ntptime = load_ntptime_from_msntp()
	if result == False:
		return
		
	ntptime = 3600 * float(setting.ntp_timezone) + ntptime
	#if 'Peninsula (GMT+01:00)' == setting.ntp_timezone:
	#	ntptime += 3600

	save_ntptime(ntptime)

	#if setting.ntp_summertime and is_summertime_period(ntime()):
	if setting.ntp_summertime:
		save_ntptime(ntptime + 3600)

#Roxia End jhbang