Exemple #1
0
def testSpeed():
    import time
    from time import strftime
    #format = 'Date: %Y/%m/%d - Time: %H:%M:%S - %a %A %C %B %b %g %G %V'
    format = '%Y/%m/%d - %H:%M:%S'
    format2 = '%OY/%Om/%Od - %OH:%OM:%OS'
    n = 1
    ########
    binFmt = compileTmFormat(format)
    mode = core.DATE_GREG
    tm = list(time.localtime())
    jd = to_jd(tm[0], tm[1], tm[2], mode)
    ########
    t0 = now()
    for i in range(n):
        strftime(format, tm)
    t1 = now()
    print('Python strftime: %s sec' % (t1 - t0))
    ########
    jd = to_jd(tm[0], tm[1], tm[2], mode)
    t0 = now()
    for i in range(n):
        formatTime(binFmt, mode, jd, tm)
    t1 = now()
    print('My strftime:     %s sec' % (t1 - t0))
    ########
    from scal3.ui_gtk.preferences import strftime
    t0 = now()
    for i in range(n):
        strftime(format2, tm)
    t1 = now()
    print('My old strftime: %s sec' % (t1 - t0))
Exemple #2
0
def testSpeed():
    import time
    from time import strftime
    #format = 'Date: %Y/%m/%d - Time: %H:%M:%S - %a %A %C %B %b %g %G %V'
    format = '%Y/%m/%d - %H:%M:%S'
    format2 = '%OY/%Om/%Od - %OH:%OM:%OS'
    n = 1
    ########
    binFmt = compileTmFormat(format)
    mode = core.DATE_GREG
    tm = list(time.localtime())
    jd = to_jd(tm[0], tm[1], tm[2], mode)
    ########
    t0 = now()
    for i in range(n):
        strftime(format, tm)
    t1 = now()
    print('Python strftime: %s sec'%(t1-t0))
    ########
    jd = to_jd(tm[0], tm[1], tm[2], mode)
    t0 = now()
    for i in range(n):
        formatTime(binFmt, mode, jd, tm)
    t1 = now()
    print('My strftime:     %s sec'%(t1-t0))
    ########
    from scal3.ui_gtk.preferences import strftime
    t0 = now()
    for i in range(n):
        strftime(format2, tm)
    t1 = now()
    print('My old strftime: %s sec'%(t1-t0))
Exemple #3
0
def datesDiff(y1, m1, d1, y2, m2, d2):
	return to_jd(
		calType.primary,
		y2,
		m2,
		d2,
	) - to_jd(
		calType.primary,
		y1,
		m1,
		d1,
	)
Exemple #4
0
def datesDiff(y1, m1, d1, y2, m2, d2):
	return to_jd(
		calType.primary,
		y2,
		m2,
		d2,
	) - to_jd(
		calType.primary,
		y1,
		m1,
		d1,
	)
Exemple #5
0
 def get_epoch(self, mode):
     from scal3.time_utils import getEpochFromJhms
     date, hms = self.get_value()
     return getEpochFromJhms(
         to_jd(date[0], date[1], date[2], mode),
         *hms
     )
Exemple #6
0
 def updateEndDates(self):
     y, m, d = self.startDateInput.get_value()
     jd0 = to_jd(y, m, d, self.altMode) - 1
     for row in self.trees:
         mLen = row[3]
         jd0 += mLen
         row[4] = dateLocale(*jd_to(jd0, self.altMode))
Exemple #7
0
	def get_epoch(self, mode):
		from scal3.time_utils import getEpochFromJhms
		date, hms = self.get_value()
		return getEpochFromJhms(
			to_jd(date[0], date[1], date[2], mode),
			*hms
		)
Exemple #8
0
def getEpochFromDate(y, m, d, mode):
	return getEpochFromJd(to_jd(
		y,
		m,
		d,
		mode,
	))
Exemple #9
0
def getEpochFromDate(y, m, d, mode):
	return getEpochFromJd(to_jd(
		y,
		m,
		d,
		mode,
	))
Exemple #10
0
def testOutput():
    from time import strftime
    binFmt = compileTmFormat('%Y/%m/%d')
    year = 2010
    month = 1
    day = 4
    jd = to_jd(year, month, day, core.DATE_GREG)
    tm = (year, month, day, 12, 10, 0, 15, 1, 1)
    print(formatTime(binFmt, core.DATE_GREG, jd, tm))
    print(strftime('%OY/%Om/%Od', tm))
Exemple #11
0
def testOutput():
    from time import strftime
    binFmt = compileTmFormat('%Y/%m/%d')
    year = 2010
    month = 1
    day = 4
    jd = to_jd(year, month, day, core.DATE_GREG)
    tm = (year, month, day, 12, 10, 0, 15, 1, 1)
    print(formatTime(binFmt, core.DATE_GREG, jd, tm))
    print(strftime('%OY/%Om/%Od', tm))
Exemple #12
0
 def updateVars(self):
     y, m, d = self.startDateInput.get_value()
     monthDb.endJd = monthDb.startJd = to_jd(y, m, d, self.altMode)
     monthDb.monthLenByYm = {}
     for row in self.trees:
         ym = row[0]
         mLen = row[3]
         monthDb.monthLenByYm[ym] = mLen
         monthDb.endJd += mLen
     monthDb.save()
Exemple #13
0
def getJdByIcsDate(dateStr):
    tm = strptime(dateStr, '%Y%m%d')
    return to_jd(tm.tm_year, tm.tm_mon, tm.tm_mday, DATE_GREG)
Exemple #14
0
	def get_jd(self, mode):
		y, m, d = self.get_value()
		return to_jd(y, m, d, mode)
Exemple #15
0
def getJdRangeForMonth(year, month, mode):
	day = getMonthLen(year, month, mode)
	return (
		to_jd(year, month, 1, mode),
		to_jd(year, month, day, mode) + 1,
	)
Exemple #16
0
	def onDraw(self, widget=None, event=None):
		cr = self.get_window().cairo_create()
		width = float(self.get_allocation().width)
		height = float(self.get_allocation().height)
		dia = min(width, height)
		maxR = float(dia) / 2.0
		minR = self.innerCircleRatio * maxR
		x0 = (width - dia) / 2.0
		y0 = (height - dia) / 2.0
		cx = x0 + maxR
		cy = y0 + maxR
		####
		#self.angleOffset
		#self.bgColor
		#self.wheelBgColor
		#self.lineColor
		#self.lineWidth
		#self.textColor
		####
		cr.rectangle(0, 0, width, height)
		fillColor(cr, self.bgColor)
		####
		calsN = len(calTypes.active)
		deltaR = (maxR - minR) / float(calsN)
		mode0 = calTypes.active[0]
		jd0 = to_jd(ui.todayCell.year, 1, 1, mode0)
		yearLen = calTypes.primaryModule().avgYearLen
		angle0 = self.angleOffset * pi / 180.0 - pi/2.0
		avgDeltaAngle = 2*pi / 12
		####
		if self.todayIndicatorEnable:
			drawLineLengthAngle(
				cr,
				cx,
				cy,
				maxR,## FIXME
				angle0 + 2.0*pi*(ui.todayCell.jd - jd0)/yearLen,
				self.todayIndicatorWidth,
			)
			fillColor(cr, self.todayIndicatorColor)
		####
		drawCircle(cr, cx, cy, self.centerR)
		fillColor(cr, self.centerColor)
		####
		drawCircleOutline(cr, cx, cy, maxR, maxR-minR)
		fillColor(cr, self.wheelBgColor)
		####
		spinngJd = getSpringJdAfter(jd0)
		springAngle = angle0 + 2.0*pi*(spinngJd - jd0)/yearLen ## radians
		for index, color in enumerate((
			self.springColor,
			self.summerColor,
			self.autumnColor,
			self.winterColor,
		)):
			drawArcOutline(
				cr,
				cx,
				cy,
				maxR,
				maxR-minR,
				springAngle + index * pi/2.0,
				springAngle + (index+1) * pi/2.0,
			)
			fillColor(cr, color)
		####
		for index, mode in enumerate(calTypes.active):
			dr = index * deltaR
			r = maxR - dr
			cx0 = x0 + dr
			cy0 = y0 + dr
			###
			drawCircleOutline(cr, cx, cy, r, self.lineWidth)
			fillColor(cr, self.lineColor)
			####
			year0, month0, day0 = jd_to(jd0, mode)
			ym0 = year0*12 + (month0-1)
			cr.set_line_width(self.lineWidth)
			for ym in range(ym0, ym0 + 12):
				year, month = divmod(ym, 12) ; month += 1
				jd = to_jd(year, month, 1, mode)
				angle = angle0 + 2.0*pi*(jd - jd0)/yearLen ## radians
				#angleD = angle * 180 / pi
				#print('mode=%s, year=%s, month=%s, angleD=%.1f'%(mode, year, month, angleD))
				d = self.lineWidth
				sepX, sepY = goAngle(
					cx,
					cy,
					angle,
					r - d*0.2,## FIXME
				)
				drawLineLengthAngle(
					cr,
					sepX,
					sepY,
					deltaR - d*0.2,## FIXME
					angle + pi,
					d,
				)
				fillColor(
					cr,
					self.yearStartLineColor if month==1 else self.lineColor,
				)
				###
				layoutMaxW = (r - deltaR) * 2.0 * pi / 12.0
				layoutMaxH = deltaR
				layout = newTextLayout(
					self,
					text=getMonthName(mode, month, year),
					maxSize=(layoutMaxW, layoutMaxH),
					maximizeScale=0.6,
					truncate=False,
				)
				layoutW, layoutH = layout.get_pixel_size()
				centerAngle = angle + avgDeltaAngle/2.0
				lx, ly = goAngle(
					cx,
					cy,
					centerAngle,
					(r - deltaR/3.0),
				)
				lx, ly = goAngle(
					lx,
					ly,
					angle - pi/2.0,
					layoutW / 2.0,
				)
				lx, ly = goAngle(
					lx,
					ly,
					angle,
					layoutH / 2.0,
				)
				cr.move_to(
					lx,
					ly,
				)
				#cr.save()
				rotateAngle = centerAngle + pi/2.0
				cr.rotate(rotateAngle)
				setColor(cr, self.textColor) ; show_layout(cr, layout)    
				cr.rotate(-rotateAngle)
				#cr.restore()
			#####
			drawCircleOutline(cr, cx, cy, minR, self.lineWidth)
			fillColor(cr, self.lineColor)
Exemple #17
0
 def onDraw(self, widget=None, event=None):
     cr = self.get_window().cairo_create()
     width = float(self.get_allocation().width)
     height = float(self.get_allocation().height)
     dia = min(width, height)
     maxR = float(dia) / 2.0
     minR = self.innerCircleRatio * maxR
     x0 = (width - dia) / 2.0
     y0 = (height - dia) / 2.0
     cx = x0 + maxR
     cy = y0 + maxR
     ####
     #self.angleOffset
     #self.bgColor
     #self.wheelBgColor
     #self.lineColor
     #self.lineWidth
     #self.textColor
     ####
     cr.rectangle(0, 0, width, height)
     fillColor(cr, self.bgColor)
     ####
     calsN = len(calTypes.active)
     deltaR = (maxR - minR) / float(calsN)
     mode0 = calTypes.active[0]
     jd0 = to_jd(ui.todayCell.year, 1, 1, mode0)
     yearLen = calTypes.primaryModule().avgYearLen
     angle0 = self.angleOffset * pi / 180.0 - pi / 2.0
     avgDeltaAngle = 2 * pi / 12
     ####
     if self.todayIndicatorEnable:
         drawLineLengthAngle(
             cr,
             cx,
             cy,
             maxR,  ## FIXME
             angle0 + 2.0 * pi * (ui.todayCell.jd - jd0) / yearLen,
             self.todayIndicatorWidth,
         )
         fillColor(cr, self.todayIndicatorColor)
     ####
     drawCircle(cr, cx, cy, self.centerR)
     fillColor(cr, self.centerColor)
     ####
     drawCircleOutline(cr, cx, cy, maxR, maxR - minR)
     fillColor(cr, self.wheelBgColor)
     ####
     spinngJd = getSpringJdAfter(jd0)
     springAngle = angle0 + 2.0 * pi * (spinngJd -
                                        jd0) / yearLen  ## radians
     for index, color in enumerate((
             self.springColor,
             self.summerColor,
             self.autumnColor,
             self.winterColor,
     )):
         drawArcOutline(
             cr,
             cx,
             cy,
             maxR,
             maxR - minR,
             springAngle + index * pi / 2.0,
             springAngle + (index + 1) * pi / 2.0,
         )
         fillColor(cr, color)
     ####
     for index, mode in enumerate(calTypes.active):
         dr = index * deltaR
         r = maxR - dr
         cx0 = x0 + dr
         cy0 = y0 + dr
         ###
         drawCircleOutline(cr, cx, cy, r, self.lineWidth)
         fillColor(cr, self.lineColor)
         ####
         year0, month0, day0 = jd_to(jd0, mode)
         ym0 = year0 * 12 + (month0 - 1)
         cr.set_line_width(self.lineWidth)
         for ym in range(ym0, ym0 + 12):
             year, month = divmod(ym, 12)
             month += 1
             jd = to_jd(year, month, 1, mode)
             angle = angle0 + 2.0 * pi * (jd - jd0) / yearLen  ## radians
             #angleD = angle * 180 / pi
             #print('mode=%s, year=%s, month=%s, angleD=%.1f'%(mode, year, month, angleD))
             d = self.lineWidth
             sepX, sepY = goAngle(
                 cx,
                 cy,
                 angle,
                 r - d * 0.2,  ## FIXME
             )
             drawLineLengthAngle(
                 cr,
                 sepX,
                 sepY,
                 deltaR - d * 0.2,  ## FIXME
                 angle + pi,
                 d,
             )
             fillColor(
                 cr,
                 self.yearStartLineColor if month == 1 else self.lineColor,
             )
             ###
             layoutMaxW = (r - deltaR) * 2.0 * pi / 12.0
             layoutMaxH = deltaR
             layout = newTextLayout(
                 self,
                 text=getMonthName(mode, month, year),
                 maxSize=(layoutMaxW, layoutMaxH),
                 maximizeScale=0.6,
                 truncate=False,
             )
             layoutW, layoutH = layout.get_pixel_size()
             centerAngle = angle + avgDeltaAngle / 2.0
             lx, ly = goAngle(
                 cx,
                 cy,
                 centerAngle,
                 (r - deltaR / 3.0),
             )
             lx, ly = goAngle(
                 lx,
                 ly,
                 angle - pi / 2.0,
                 layoutW / 2.0,
             )
             lx, ly = goAngle(
                 lx,
                 ly,
                 angle,
                 layoutH / 2.0,
             )
             cr.move_to(
                 lx,
                 ly,
             )
             #cr.save()
             rotateAngle = centerAngle + pi / 2.0
             cr.rotate(rotateAngle)
             setColor(cr, self.textColor)
             show_layout(cr, layout)
             cr.rotate(-rotateAngle)
             #cr.restore()
         #####
         drawCircleOutline(cr, cx, cy, minR, self.lineWidth)
         fillColor(cr, self.lineColor)
Exemple #18
0
 def get_jd(self, mode):
     y, m, d = self.get_value()
     return to_jd(y, m, d, mode)
Exemple #19
0
def compileTmFormat(format, hasTime=True):
    ## format:     'Today: %Y/%m/%d'
    ## pyFmt:      'Today: %s/%s/%s'
    ## funcs:      (get_y, get_m, get_d)
    pyFmt = ''
    funcs = []
    n = len(format)
    i = 0
    while i < n:
        c0 = format[i]
        if c0 != '%':
            pyFmt += c0
            i += 1
            continue
        if i == n - 1:
            pyFmt += c0
            break
        c1 = format[i + 1]
        if c1 == '%':
            pyFmt += '%'
            i += 2
            continue
        if c1 == 'Y':
            funcs.append(
                lambda cell, mode, tm: _(cell.dates[mode][0], fillZero=4))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'y':
            funcs.append(lambda cell, mode, tm: _(cell.dates[mode][0] % 100,
                                                  fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'm':
            funcs.append(
                lambda cell, mode, tm: _(cell.dates[mode][1], fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'd':
            funcs.append(
                lambda cell, mode, tm: _(cell.dates[mode][2], fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'Q':  ## calendar name (gregorian, jalali, ...)
            funcs.append(lambda cell, mode, tm: _(calTypes[mode].name))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'a':
            funcs.append(
                lambda cell, mode, tm: core.weekDayNameAb[cell.weekDay])
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'A':
            funcs.append(lambda cell, mode, tm: core.weekDayName[cell.weekDay])
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'b' or c1 == 'h':  ## ??????????
            funcs.append(lambda cell, mode, tm: _(calTypes[
                mode].getMonthNameAb(cell.dates[mode][1])))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'B':
            funcs.append(lambda cell, mode, tm: _(calTypes[mode].getMonthName(
                cell.dates[mode][1])))
            pyFmt += '%s'
            i += 2
            continue
        #elif c1=='c':## ????? locale's date and time (e.g., Thu Mar    3 23:05:25 2005)
        #elif c1=='x':## ????? locale's date representation (e.g., 12/31/99)
        elif c1 == 'C':
            funcs.append(lambda cell, mode, tm: _(cell.dates[mode][0] // 100,
                                                  fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'D':  ## %m/%d/%y
            funcs += [
                lambda cell, mode, tm: _(cell.dates[mode][1], fillZero=2),
                lambda cell, mode, tm: _(cell.dates[mode][2], fillZero=2),
                lambda cell, mode, tm: _(cell.dates[mode][0] % 100, fillZero=2)
            ]
            pyFmt += '%s/%s/%s'
            i += 2
            continue
        elif c1 == 'e':  ## day of month, space padded; same as %_d
            funcs.append(
                lambda cell, mode, tm: _(cell.dates[mode][2], fillZero=2))
            pyFmt += '%2s'
            i += 2
            continue
        elif c1 == 'F':  ## %Y-%m-%d
            funcs += [
                lambda cell, mode, tm: _(cell.dates[mode][0], fillZero=4),
                lambda cell, mode, tm: _(cell.dates[mode][1], fillZero=2),
                lambda cell, mode, tm: _(cell.dates[mode][2], fillZero=2)
            ]
            pyFmt += '%s-%s-%s'
            i += 2
            continue
        elif c1 == 'g':  ## not affected by mode!
            funcs.append(
                lambda cell, mode, tm: _(isow_year(cell.jd) % 100, fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'G':  ## not affected by mode!
            funcs.append(
                lambda cell, mode, tm: _(isow_year(cell.jd), fillZero=4))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'V':  ## not affected by mode!
            funcs.append(lambda cell, mode, tm: _(isow(cell.jd), fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'u':
            funcs.append(lambda cell, mode, tm: _(cell.jd % 7 + 1))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'w':
            funcs.append(lambda cell, mode, tm: _((cell.jd + 1) % 7))  ## jwday
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'W':

            def weekNumberMonday(cell, mode, tm):
                jd0 = to_jd(cell.dates[mode][0], 1, 1, mode)
                return _((cell.jd - jd0 + jd0 % 7) // 7, fillZero=2)

            funcs.append(weekNumberMonday)
            pyFmt += '%s'
            i += 2
            continue
        #elif c1=='U':##????????????????????????????????????????
        #	funcs.append(lambda cell, mode, tm: _())
        #	pyFmt += '%s'
        #	i += 2
        #	continue
        elif c1 == 'j':
            funcs.append(lambda cell, mode, tm: _(cell.jd - to_jd(
                cell.dates[mode][0], 1, 1, mode) + 1,
                                                  fillZero=3))
            pyFmt += '%s'
            i += 2
            continue
        elif c1 == 'n':
            pyFmt += '\n'
            i += 2
            continue
        elif c1 == 't':
            pyFmt += '\t'
            i += 2
            continue
        elif c1 == 'z':

            def tz(cell, mode, tm):
                m = int(getUtcOffsetByGDate(*cell.dates[core.DATE_GREG]) / 60)
                return _(m // 60, fillZero=2) + _(m % 60, fillZero=2)

            funcs.append(tz)
            pyFmt += '%s'
            i += 2
            continue
        #elif c1=='Z': ##alphabetic time zone abbreviation (e.g., EDT)
        elif c1 == ':':
            c2 = format[i + 2]
            if c2 == 'z':  ## %:z

                def tz(cell, mode, tm):
                    m = int(
                        getUtcOffsetByGDate(*cell.dates[core.DATE_GREG]) / 60)
                    return _(m // 60, fillZero=2) + ':' + _(m % 60, fillZero=2)

                funcs.append(tz)
                pyFmt += '%s'
                i += 3
                continue
            ## %::z , %:::z
            #elif c2==':':## ???????????????????????
        elif hasTime:
            if c1 == 'H':
                funcs.append(lambda cell, mode, tm: _(tm[0], fillZero=2))
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'I':
                funcs.append(lambda cell, mode, tm: _(
                    (tm[0] - 1) % 12 + 1, fillZero=2))  ## ????????
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'k':
                funcs.append(lambda cell, mode, tm: _(tm[0]))
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'l':
                funcs.append(lambda cell, mode, tm: _(
                    (tm[0] - 1) % 12 + 1))  ## ????????
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'r':  ## %I:%M:%s PM
                funcs.append(lambda cell, mode, tm: \
                 _((tm[0]-1)%12+1, fillZero=2) + ':' +\
                 _(tm[1], fillZero=2)          + ':' +\
                 _(tm[2], fillZero=2)          + ' ' +\
                 _('AM' if tm[0]<12 else 'PM')
                )
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'R':  ## %H:%M
                funcs.append(lambda cell, mode, tm: \
                 _(tm[0], fillZero=2) + ':' +\
                 _(tm[1], fillZero=2)
                )
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'M':
                funcs.append(lambda cell, mode, tm: _(tm[1], fillZero=2))
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'S':
                funcs.append(lambda cell, mode, tm: _(int(tm[2]), fillZero=2))
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 's':  ## seconds since 1970-01-01 00:00:00 UTC
                #funcs.append(lambda cell, mode, tm: _(int(time.mktime(a[2:7]+(int(tm[2]), 0, 0, 1)))))
                funcs.append(lambda cell, mode, tm: _(
                    core.getEpochFromJhms(cell.jd, *tm)))
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'N':
                funcs.append(lambda cell, mode, tm: _(
                    int(tm[2] * 1000000000 % 1000000000)))
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'p':
                funcs.append(lambda cell, mode, tm: _('AM'
                                                      if tm[0] < 12 else 'PM'))
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'P':
                funcs.append(lambda cell, mode, tm: _('AM' if tm[0] < 12 else
                                                      'PM').lower())
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'T':  ## %%H:%M:%S
                funcs.append(lambda cell, mode, tm: \
                 _(tm[0], fillZero=2) + ':' +\
                 _(tm[1], fillZero=2) + ':' +\
                 _(tm[2], fillZero=2)
                )
                pyFmt += '%s'
                i += 2
                continue
            elif c1 == 'X':  ## locale's time representation (e.g., 23:13:48)
                funcs.append(lambda cell, mode, tm: \
                 _(tm[0], fillZero=2) + ':' +\
                 _(tm[1], fillZero=2) + ':' +\
                 _(tm[2], fillZero=2)
                )
                pyFmt += '%s'
                i += 2
                continue
        pyFmt += ('%' + c1)
        i += 2
    return (pyFmt, funcs)  ## binFmt
Exemple #20
0
        date[0] *= -1
    checkDate(date)
    return date


def validDate(mode, y, m, d):  ## move to cal-modules
    if y < 0:
        return False
    if m < 1 or m > 12:
        return False
    if d > getMonthLen(y, m, mode):
        return False
    return True


datesDiff = lambda y1, m1, d1, y2, m2, d2: to_jd(
    calType.primary, y2, m2, d2) - to_jd(calType.primary, y1, m1, d1)

dayOfYear = lambda y, m, d: datesDiff(y, 1, 1, y, m, d) + 1

## jwday: Calculate day of week from Julian day
## 0 = Sunday
## 1 = Monday
jwday = lambda jd: (jd + 1) % 7


def getJdRangeForMonth(year, month, mode):
    day = getMonthLen(year, month, mode)
    return (
        to_jd(year, month, 1, mode),
        to_jd(year, month, day, mode) + 1,
    )
Exemple #21
0
    if neg:
        date[0] *= -1
    checkDate(date)
    return date


def validDate(mode, y, m, d):## move to cal-modules
    if y<0:
        return False
    if m<1 or m>12:
        return False
    if d > getMonthLen(y, m, mode):
        return False
    return True

datesDiff = lambda y1, m1, d1, y2, m2, d2: to_jd(calType.primary, y2, m2, d2) - to_jd(calType.primary, y1, m1, d1)

dayOfYear = lambda y, m, d: datesDiff(y, 1, 1, y, m, d) + 1

## jwday: Calculate day of week from Julian day
## 0 = Sunday
## 1 = Monday
jwday = lambda jd: (jd + 1) % 7

def getJdRangeForMonth(year, month, mode):
    day = getMonthLen(year, month, mode)
    return (
        to_jd(year, month, 1, mode),
        to_jd(year, month, day, mode) + 1,
    )
Exemple #22
0
def compileTmFormat(format, hasTime=True):
    ## format:     'Today: %Y/%m/%d'
    ## pyFmt:      'Today: %s/%s/%s'
    ## funcs:      (get_y, get_m, get_d)
    pyFmt = ''
    funcs = []
    n = len(format)
    i = 0
    while i<n:
        c0 = format[i]
        if c0!='%':
            pyFmt += c0
            i += 1
            continue
        if i==n-1:
            pyFmt += c0
            break
        c1 = format[i+1]
        if c1=='%':
            pyFmt += '%'
            i += 2
            continue
        if c1=='Y':
            funcs.append(lambda cell, mode, tm: _(cell.dates[mode][0], fillZero=4))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='y':
            funcs.append(lambda cell, mode, tm: _(cell.dates[mode][0]%100, fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='m':
            funcs.append(lambda cell, mode, tm: _(cell.dates[mode][1], fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='d':
            funcs.append(lambda cell, mode, tm: _(cell.dates[mode][2], fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='Q':## calendar name (gregorian, jalali, ...)
            funcs.append(lambda cell, mode, tm: _(calTypes[mode].name))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='a':
            funcs.append(lambda cell, mode, tm: core.weekDayNameAb[cell.weekDay])
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='A':
            funcs.append(lambda cell, mode, tm: core.weekDayName[cell.weekDay])
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='b' or c1=='h':## ??????????
            funcs.append(lambda cell, mode, tm: _(calTypes[mode].getMonthNameAb(cell.dates[mode][1])))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='B':
            funcs.append(lambda cell, mode, tm: _(calTypes[mode].getMonthName(cell.dates[mode][1])))
            pyFmt += '%s'
            i += 2
            continue
        #elif c1=='c':## ????? locale's date and time (e.g., Thu Mar    3 23:05:25 2005)
        #elif c1=='x':## ????? locale's date representation (e.g., 12/31/99)
        elif c1=='C':
            funcs.append(lambda cell, mode, tm: _(cell.dates[mode][0]//100, fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='D':## %m/%d/%y
            funcs += [
                lambda cell, mode, tm: _(cell.dates[mode][1], fillZero=2),
                lambda cell, mode, tm: _(cell.dates[mode][2], fillZero=2),
                lambda cell, mode, tm: _(cell.dates[mode][0]%100, fillZero=2)
            ]
            pyFmt += '%s/%s/%s'
            i += 2
            continue
        elif c1=='e':## day of month, space padded; same as %_d
            funcs.append(lambda cell, mode, tm: _(cell.dates[mode][2], fillZero=2))
            pyFmt += '%2s'
            i += 2
            continue
        elif c1=='F':## %Y-%m-%d
            funcs += [
                lambda cell, mode, tm: _(cell.dates[mode][0], fillZero=4),
                lambda cell, mode, tm: _(cell.dates[mode][1], fillZero=2),
                lambda cell, mode, tm: _(cell.dates[mode][2], fillZero=2)
            ]
            pyFmt += '%s-%s-%s'
            i += 2
            continue
        elif c1=='g':## not affected by mode!
            funcs.append(lambda cell, mode, tm: _(isow_year(cell.jd)%100, fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='G':## not affected by mode!
            funcs.append(lambda cell, mode, tm: _(isow_year(cell.jd), fillZero=4))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='V':## not affected by mode!
            funcs.append(lambda cell, mode, tm: _(isow(cell.jd), fillZero=2))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='u':
            funcs.append(lambda cell, mode, tm: _(cell.jd%7 + 1))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='w':
            funcs.append(lambda cell, mode, tm: _((cell.jd+1)%7)) ## jwday
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='W':
            def weekNumberMonday(a):
                jd0 = to_jd(cell.dates[mode][0], 1, 1, mode)
                return _((cell.jd - jd0 + jd0%7) / 7, fillZero=2)
            funcs.append(weekNumberMonday)
            pyFmt += '%s'
            i += 2
            continue
        #elif c1=='U':##????????????????????????????????????????
        #    funcs.append(lambda cell, mode, tm: _())
        #    pyFmt += '%s'
        #    i += 2
        #    continue
        elif c1=='j':
            funcs.append(lambda cell, mode, tm: _(
                cell.jd - to_jd(cell.dates[mode][0], 1, 1, mode) + 1,
                fillZero=3
            ))
            pyFmt += '%s'
            i += 2
            continue
        elif c1=='n':
            pyFmt += '\n'
            i += 2
            continue
        elif c1=='t':
            pyFmt += '\t'
            i += 2
            continue
        elif c1=='z':
            def tz(a):
                m = int(getUtcOffsetByDateSec(cell.dates[mode][0], cell.dates[mode][1], cell.dates[mode][2])/60)
                return _(m//60, fillZero=2) + _(m%60, fillZero=2)
            funcs.append(tz)
            pyFmt += '%s'
            i += 2
            continue
        #elif c1=='Z': ##alphabetic time zone abbreviation (e.g., EDT)
        elif c1==':':
            c2 = format[i+2]
            if c2=='z':## %:z
                def tz(a):
                    s = int(getUtcOffsetByDateSec(cell.dates[mode][0], cell.dates[mode][1], cell.dates[mode][2]))
                    return _(m//60, fillZero=2) + ':' + _(m%60, fillZero=2)
                funcs.append(tz)
                pyFmt += '%s'
                i += 3
                continue
            ## %::z , %:::z
            #elif c2==':':## ???????????????????????
        elif hasTime:
            if c1=='H':
                funcs.append(lambda cell, mode, tm: _(tm[0], fillZero=2))
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='I':
                funcs.append(lambda cell, mode, tm: _((tm[0]-1)%12+1, fillZero=2)) ## ????????
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='k':
                funcs.append(lambda cell, mode, tm: _(tm[0]))
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='l':
                funcs.append(lambda cell, mode, tm: _((tm[0]-1)%12+1)) ## ????????
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='r':## %I:%M:%s PM
                funcs.append(lambda cell, mode, tm: \
                    _((tm[0]-1)%12+1, fillZero=2) + ':' +\
                    _(tm[1], fillZero=2)          + ':' +\
                    _(tm[2], fillZero=2)          + ' ' +\
                    _('AM' if tm[0]<12 else 'PM')
                )
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='R':## %H:%M
                funcs.append(lambda cell, mode, tm: \
                    _(tm[0], fillZero=2) + ':' +\
                    _(tm[1], fillZero=2)
                )
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='M':
                funcs.append(lambda cell, mode, tm: _(tm[1], fillZero=2))
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='S':
                funcs.append(lambda cell, mode, tm: _(int(tm[2]), fillZero=2))
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='s':## seconds since 1970-01-01 00:00:00 UTC
                #funcs.append(lambda cell, mode, tm: _(int(time.mktime(a[2:7]+(int(tm[2]), 0, 0, 1)))))
                funcs.append(lambda cell, mode, tm: _(core.getEpochFromJhms(cell.jd, *tm)))
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='N':
                funcs.append(lambda cell, mode, tm: _(int(tm[2]*1000000000%1000000000)))
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='p':
                funcs.append(lambda cell, mode, tm: _('AM' if tm[0]<12 else 'PM'))
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='P':
                funcs.append(lambda cell, mode, tm: _('AM' if tm[0]<12 else 'PM').lower())
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='T':## %%H:%M:%S
                funcs.append(lambda cell, mode, tm: \
                    _(tm[0], fillZero=2) + ':' +\
                    _(tm[1], fillZero=2) + ':' +\
                    _(tm[2], fillZero=2)
                )
                pyFmt += '%s'
                i += 2
                continue
            elif c1=='X':## locale's time representation (e.g., 23:13:48)
                funcs.append(lambda cell, mode, tm: \
                    _(tm[0], fillZero=2) + ':' +\
                    _(tm[1], fillZero=2) + ':' +\
                    _(tm[2], fillZero=2)
                )
                pyFmt += '%s'
                i += 2
                continue
        pyFmt += ('%'+c1)
        i += 2
    return (pyFmt, funcs) ## binFmt
Exemple #23
0
def compileTmFormat(format, hasTime=True):
	## format:     "Today: %Y/%m/%d"
	## pyFmt:      "Today: %s/%s/%s"
	## funcs:      (get_y, get_m, get_d)
	pyFmt = ""
	funcs = []
	n = len(format)
	i = 0
	while i < n:
		c0 = format[i]
		if c0 != "%":
			pyFmt += c0
			i += 1
			continue
		if i == n - 1:
			pyFmt += c0
			break
		c1 = format[i + 1]
		if c1 == "%":
			pyFmt += "%"
			i += 2
			continue
		if c1 == "Y":
			funcs.append(lambda cell, mode, tm: _(
				cell.dates[mode][0],
				fillZero=4,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "y":
			funcs.append(lambda cell, mode, tm: _(
				cell.dates[mode][0] % 100,
				fillZero=2,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "m":
			funcs.append(lambda cell, mode, tm: _(
				cell.dates[mode][1],
				fillZero=2,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "d":
			funcs.append(lambda cell, mode, tm: _(
				cell.dates[mode][2],
				fillZero=2,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "Q":## calendar name (gregorian, jalali, ...)
			funcs.append(lambda cell, mode, tm: _(
				calTypes.nameByIndex(mode),
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "a":
			funcs.append(
				lambda cell, mode, tm:
					core.weekDayNameAb[cell.weekDay]
			)
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "A":
			funcs.append(
				lambda cell, mode, tm:
					core.weekDayName[cell.weekDay]
			)
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "b" or c1 == "h":  # FIXME
			def f(cell, mode, tm):
				module, ok = calTypes[mode]
				if not ok:
					raise RuntimeError("cal type %r not found" % mode)
				return module.getMonthNameAb(cell.dates[mode][1])
			funcs.append(f)
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "B":
			def f(cell, mode, tm):
				module, ok = calTypes[mode]
				if not ok:
					raise RuntimeError("cal type %r not found" % mode)
				return module.getMonthName(cell.dates[mode][1])
			funcs.append(f)
			pyFmt += "%s"
			i += 2
			continue
		#elif c1 == "c":
		#	FIXME locale"s date and time (e.g., Thu Mar    3 23:05:25 2005)
		#elif c1 == "x":
		#	FIXME locale"s date representation (e.g., 12/31/99)
		elif c1 == "C":
			funcs.append(lambda cell, mode, tm: _(
				cell.dates[mode][0] // 100,
				fillZero=2,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "D":## %m/%d/%y
			funcs += [
				lambda cell, mode, tm: _(
					cell.dates[mode][1],
					fillZero=2,
				),
				lambda cell, mode, tm: _(
					cell.dates[mode][2],
					fillZero=2,
				),
				lambda cell, mode, tm: _(
					cell.dates[mode][0] % 100,
					fillZero=2,
				),
			]
			pyFmt += "%s/%s/%s"
			i += 2
			continue
		elif c1 == "e":## day of month, space padded; same as %_d
			funcs.append(lambda cell, mode, tm: _(
				cell.dates[mode][2],
				fillZero=2,
			))
			pyFmt += "%2s"
			i += 2
			continue
		elif c1 == "F":## %Y-%m-%d
			funcs += [
				lambda cell, mode, tm: _(
					cell.dates[mode][0],
					fillZero=4,
				),
				lambda cell, mode, tm: _(
					cell.dates[mode][1],
					fillZero=2,
				),
				lambda cell, mode, tm: _(
					cell.dates[mode][2],
					fillZero=2,
				),
			]
			pyFmt += "%s-%s-%s"
			i += 2
			continue
		elif c1 == "g":## not affected by mode!
			funcs.append(lambda cell, mode, tm: _(
				isow_year(cell.jd) % 100,
				fillZero=2,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "G":## not affected by mode!
			funcs.append(lambda cell, mode, tm: _(
				isow_year(cell.jd),
				fillZero=4,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "V":## not affected by mode!
			funcs.append(lambda cell, mode, tm: _(
				isow(cell.jd),
				fillZero=2,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "u":
			funcs.append(lambda cell, mode, tm: _(
				cell.jd % 7 + 1,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "w":
			funcs.append(lambda cell, mode, tm: _(
				(cell.jd + 1) % 7
			))  # jwday
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "W":
			def weekNumberMonday(cell, mode, tm):
				jd0 = to_jd(cell.dates[mode][0], 1, 1, mode)
				return _(
					(cell.jd - jd0 + jd0 % 7) // 7,
					fillZero=2,
				)
			funcs.append(weekNumberMonday)
			pyFmt += "%s"
			i += 2
			continue
		#elif c1 == "U":  # FIXME
		#	funcs.append(lambda cell, mode, tm: _())
		#	pyFmt += "%s"
		#	i += 2
		#	continue
		elif c1 == "j":
			funcs.append(lambda cell, mode, tm: _(
				cell.jd - to_jd(cell.dates[mode][0], 1, 1, mode) + 1,
				fillZero=3,
			))
			pyFmt += "%s"
			i += 2
			continue
		elif c1 == "n":
			pyFmt += "\n"
			i += 2
			continue
		elif c1 == "t":
			pyFmt += "\t"
			i += 2
			continue
		elif c1 == "z":
			def tz(cell, mode, tm):
				m = int(
					getUtcOffsetByGDate(*cell.dates[core.DATE_GREG]) / 60
				)
				return _(m // 60, fillZero=2) + _(m % 60, fillZero=2)
			funcs.append(tz)
			pyFmt += "%s"
			i += 2
			continue
		#elif c1 == "Z":  # alphabetic time zone abbreviation (e.g., EDT)
		elif c1 == ":":
			c2 = format[i + 2]
			if c2 == "z":  # %:z
				def tz(cell, mode, tm):
					m = int(
						getUtcOffsetByGDate(*cell.dates[core.DATE_GREG]) / 60
					)
					return _(m // 60, fillZero=2) + ":" + _(m % 60, fillZero=2)
				funcs.append(tz)
				pyFmt += "%s"
				i += 3
				continue
			## %::z , %:::z
			#elif c2 == ":":## ???????????????????????
		elif hasTime:
			if c1 == "H":
				funcs.append(lambda cell, mode, tm: _(
					tm[0],
					fillZero=2,
				))
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "I":
				funcs.append(lambda cell, mode, tm: _(
					(tm[0] - 1) % 12 + 1,
					fillZero=2,
				))  # FIXME
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "k":
				funcs.append(lambda cell, mode, tm: _(
					tm[0],
				))
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "l":
				funcs.append(lambda cell, mode, tm: _(
					(tm[0] - 1) % 12 + 1
				))  # FIXME
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "r":## %I:%M:%s PM
				funcs.append(
					lambda cell, mode, tm:
						_((tm[0] - 1) % 12 + 1, fillZero=2) + ":" +
						_(tm[1], fillZero=2) + ":" +
						_(tm[2], fillZero=2) + " " +
						_("AM" if tm[0] < 12 else "PM")
				)
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "R":## %H:%M
				funcs.append(
					lambda cell, mode, tm:
						_(tm[0], fillZero=2) + ":" +
						_(tm[1], fillZero=2)
				)
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "M":
				funcs.append(lambda cell, mode, tm: _(
					tm[1],
					fillZero=2,
				))
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "S":
				funcs.append(lambda cell, mode, tm: _(
					int(tm[2]),
					fillZero=2,
				))
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "s":## seconds since 1970-01-01 00:00:00 UTC
				#funcs.append(lambda cell, mode, tm: _(
				#	int(time.mktime(
				#		a[2:7] + (
				#			int(tm[2]),
				#			0,
				#			0,
				#			1,
				#		),
				#	)),
				#))
				funcs.append(lambda cell, mode, tm: _(
					core.getEpochFromJhms(cell.jd, *tm),
				))
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "N":
				funcs.append(lambda cell, mode, tm: _(
					int(tm[2] * 1000000000 % 1000000000),
				))
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "p":
				funcs.append(lambda cell, mode, tm: _(
					"AM" if tm[0] < 12 else "PM",
				))
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "P":
				funcs.append(lambda cell, mode, tm: _(
					"AM" if tm[0] < 12 else "PM"
				).lower())
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "T":## %%H:%M:%S
				funcs.append(
					lambda cell, mode, tm:
						_(tm[0], fillZero=2) + ":" +
						_(tm[1], fillZero=2) + ":" +
						_(tm[2], fillZero=2)
				)
				pyFmt += "%s"
				i += 2
				continue
			elif c1 == "X":## locale"s time representation (e.g., 23:13:48)
				funcs.append(
					lambda cell, mode, tm:
						_(tm[0], fillZero=2) + ":" +
						_(tm[1], fillZero=2) + ":" +
						_(tm[2], fillZero=2)
				)
				pyFmt += "%s"
				i += 2
				continue
		pyFmt += ("%" + c1)
		i += 2
	return (pyFmt, funcs) ## binFmt
Exemple #24
0
 def weekNumberMonday(a):
     jd0 = to_jd(cell.dates[mode][0], 1, 1, mode)
     return _((cell.jd - jd0 + jd0%7) / 7, fillZero=2)
Exemple #25
0
def getJdRangeForMonth(year, month, mode):
    day = getMonthLen(year, month, mode)
    return (
        to_jd(year, month, 1, mode),
        to_jd(year, month, day, mode) + 1,
    )
Exemple #26
0
 def weekNumberMonday(cell, mode, tm):
     jd0 = to_jd(cell.dates[mode][0], 1, 1, mode)
     return _((cell.jd - jd0 + jd0 % 7) // 7, fillZero=2)