/usr/share/pyshared/mx/DateTime/mxDateTime/mxDateTime_Python.py is in python-egenix-mxdatetime 3.2.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 | """
Python implementation courtesy of Drew Csillag (StarMedia Network, Inc.)
This version has been somewhat modified by MAL. It is still fairly
rough though and not necessarily high performance...
XXX Still needs testing and checkup !!!
WARNING: Using this file is only recommended if you really must
use it for some reason. It is not being actively maintained !
"""
__version__ = '1.2.0 [Python]'
import time,types,exceptions,math
### Errors
class Error(exceptions.StandardError):
pass
class RangeError(Error):
pass
### Constants (internal use only)
month_offset=(
(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365),
(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366),
)
days_in_month=(
(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
)
### Helpers
def _IS_LEAPYEAR(d):
return ((d.year % 4 == 0)
and (
(d.year % 100 != 0)
or (d.year % 400 == 0)
)
)
def _YEAROFFSET(d):
return (
(d.year - 1) * 365
+ (d.year - 1) / 4
- (d.year - 1) / 100
+ (d.year - 1) / 400
)
class _EmptyClass:
pass
def createEmptyObject(Class,
_EmptyClass=_EmptyClass):
o = _EmptyClass()
o.__class__ = Class
return o
### DateTime class
class DateTime:
def __init__(self, year, month=1, day=1, hour=0, minute=0, second=0.0):
second=1.0 * second
if month <= 0:
raise RangeError, "year out of range (>0)"
#calculate absolute date
leap = (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))
#Negative values indicate days relative to the years end
if month < 0:
month = month + 13
if not (month >= 1 and month <= 12):
raise RangeError, "month out of range (1-12)"
#Negative values indicate days relative to the months end
if (day < 0):
day = day + days_in_month[leap][month - 1] + 1;
if not (day >= 1 and day <= days_in_month[leap][month - 1]):
raise RangeError, "day out of range"
year = year - 1
yearoffset = year * 365 + year / 4 - year / 100 + year / 400
year = year + 1
absdate = day + month_offset[leap][month - 1] + yearoffset;
self.absdate = absdate
self.year = year
self.month = month
self.day = day
self.day_of_week = (absdate - 1) % 7
self.day_of_year = absdate - yearoffset
self.days_in_month = days_in_month[leap][month - 1]
comdate = absdate - 693594
if not (hour >=0 and hour <= 23):
raise RangeError, "hour out of range (0-23)"
if not (minute >= 0 and minute <= 59):
raise RangeError, "minute out of range (0-59)"
if not (second >= 0.0 and
(second < 60.0 or
(hour == 23 and minute == 59 and second < 61.0))):
raise RangeError, "second out of range (0.0 - <60.0; <61.0 for 23:59)"
self.abstime = (hour * 3600 + minute * 60) + second
self.hour = hour
self.minute = minute
self.second = second
self.dst = -1
self.tz = "???"
self.is_leapyear = leap
self.yearoffset = yearoffset
if comdate < 0.0:
comdate = comdate - self.abstime / 86400.0
else:
comdate = comdate + self.abstime / 86400.0
self.comdate = comdate
def COMDate(self):
return self.comdate
def __str__(self):
return "%04d-%02d-%02d %02d:%02d:%05.2f" % (
self.year, self.month, self.day, self.hour, self.minute,
self.second)
def __getattr__(self, attr):
if attr == 'mjd':
return (self - mjd0).days
elif attr == 'jdn':
return (self - jdn0).days
elif attr == 'tjd':
return (self - jdn0).days % 10000
elif attr == 'tjd_myriad':
return int((self - jdn0).days) / 10000 + 240
elif attr == 'absdays':
return self.absdate - 1 + self.abstime / 86400.0
else:
try:
return self.__dict__[attr]
except:
raise AttributeError, attr
def __mul__(self, other):
raise TypeError, "bad operand type(s) for *"
def __div__(self, other):
raise TypeError, "bad operand type(s) for /"
def strftime(self, format_string="%c"):
return time.strftime(format_string, self.tuple())
# Alias
Format = strftime
def tuple(self):
return (self.year, self.month, self.day, self.hour, self.minute, self.second, self.day_of_week, 0, -1)
#return time.localtime(self.ticks())
def absvalues(self):
return self.absdate, self.abstime
def __float__(self):
return self.ticks()
def __int__(self):
return int(self.ticks)
def ticks(self, offset=0.0, dst=-1):
tticks=time.mktime(self.year, self.month, self.day, self.hour,
self.minute, self.second, self.day_of_week, 0, dst)
if tticks == -1:
raise OverflowError, "cannot convert value to a time value"
ticks = (1.0*tticks) + (self.abstime - int(self.abstime)) - offset
return ticks
def gmticks(self, offset=0.0):
from mx.DateTime import tz_offset
return (self-tz_offset(self)).ticks()
def __repr__(self):
return "<DateTime object for '%d-%02d-%02d %02d:%02d:%05.2f' at %x>"% (
self.year, self.month, self.day, self.hour, self.minute,
self.second, id(self))
def __cmp__(self, other,
cmp=cmp):
if isinstance(other,DateTime):
cmpdate = cmp(self.absdate,other.absdate)
if cmpdate == 0:
return cmp(self.abstime,other.abstime)
else:
return cmpdate
elif type(other) == types.NoneType:
return -1
elif type(other) == types.StringType:
return -1
elif type(other) in (types.FloatType, types.LongType, types.IntType):
return 1
return -1
def __add__(self, other):
abstime=self.abstime
absdate=self.absdate
didadd=0
if type(other) == types.InstanceType:
if other.__class__ == DateTimeDelta:
abstime = abstime + other.seconds
didadd=1
elif other.__class__ == DateTime:
raise TypeError, "DateTime + DateTime is not supported"
else:
return other.__class__.__radd__(other, self)
elif type(other) == types.IntType or type(other) == types.FloatType:
abstime = abstime + other * 86400.0
didadd=1
if not didadd:
raise TypeError, "cannot add these two types"
if abstime >= 86400.0:
days = abstime / 86400.0
absdate = absdate + days
abstime = abstime - (86400.0 * int(days))
#print "absdate, abstime = ", absdate, abstime
elif abstime < 0.0:
days = int(((-abstime - 1) / 86400.0)) + 1
#days = int(-abstime / 86400.0)
absdate = absdate - days
abstime = abstime + 86400.0 * int(days)
if absdate < 1:
raise RangeError, "underflow while adding"
return DateTimeFromAbsDateTime(absdate, abstime)
def __radd__(self, other):
return DateTime.__add__(other, self)
def __sub__(self, other):
abstime=self.abstime
absdate=self.absdate
didsub=0
if type(other) == types.InstanceType:
if other.__class__ == DateTimeDelta:
abstime = abstime - other.seconds
didsub = 1
elif other.__class__ == DateTime:
absdate = absdate - other.absdate
abstime = abstime - other.abstime
return DateTimeDelta(absdate,0.0,0.0,abstime)
elif type(other) == types.IntType or type(other) == types.FloatType:
abstime = abstime - other * 86400.0;
didsub=1
if not didsub:
raise TypeError, "cannot subtract these two types"
if abstime >= 86400.0:
days = abstime / 86400.0
absdate = absdate + days
abstime = abstime - (86400.0 * days)
#print "absdate, abstime = ", absdate, abstime
elif abstime < 0.0:
#print "abstime < 0"
days = int( ((-abstime - 1) / 86400.0) + 1)
#days = -abstime / 86400.0
absdate = absdate - int(days)
abstime = (1.0*abstime) + (86400.0 * days)
#print "absdate, abstime", absdate, abstime
if absdate < 1:
raise RangeError, "underflow while adding"
return DateTimeFromAbsDateTime(absdate, abstime)
# Constants
mjd0 = DateTime(1858, 11, 17)
jdn0 = DateTime(-4713, 1, 1, 12, 0, 0.0)
# Other DateTime constructors
def DateTimeFromCOMDate(comdate):
absdate = int(comdate)
abstime = (comdate - float(absdate)) * 86400.0
if abstime < 0.0:
abstime = -abstime
absdate = absdate + 693594;
dt = DateTimeFromAbsDateTime(absdate, abstime)
dt.comdate = comdate
return dt
def DateTimeFromAbsDateTime(absdate, abstime):
# Create the object without calling its default constructor
dt = createEmptyObject(DateTime)
# Init. the object
abstime=1.0 * abstime
if abstime < 0 and abstime > -0.001: abstime = 0.0
if not (absdate > 0):
raise RangeError, "absdate out of range (>0)"
if not (abstime >= 0.0 and abstime <= 86400.0):
raise RangeError, "abstime out of range (0.0 - 86400.0) <%s>" % abstime
dt.absdate=absdate
dt.abstime=abstime
#calculate com date
comdate = 1.0 * (dt.absdate - 693594)
if comdate < 0.0:
comdate = comdate - dt.abstime / 86400.0
else:
comdate = comdate + dt.abstime / 86400.0
dt.comdate = comdate
#calculate the date
#print "absdate=", absdate
year = int((1.0 * absdate) / 365.2425)
#newApproximation:
while 1:
#print "year=", year
yearoffset = year * 365 + year / 4 - year / 100 + year / 400
#print "yearoffset=", yearoffset
#print "absdate=", absdate
if yearoffset >= absdate:
year = year - 1
#print "year = ", year
continue #goto newApproximation
year = year + 1
leap = (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))
dayoffset = absdate - yearoffset
#print "dayoffset=", dayoffset
if dayoffset > 365 and leap == 0:
#print "dayoffset=", dayoffset
continue #goto newApproximation
monthoffset = month_offset[leap]
for month in range(1, 13):
if monthoffset[month] >= dayoffset:
break
dt.year = year
dt.month = month
dt.day = dayoffset - month_offset[leap][month-1]
dt.day_of_week = (dt.absdate - 1) % 7
dt.day_of_year = dayoffset
break
#calculate the time
inttime = int(abstime)
hour = inttime / 3600
minute = (inttime % 3600) / 60
second = abstime - 1.0 * (hour*3600 + minute*60)
dt.hour = hour;
dt.minute = minute;
dt.second = second;
dt.days_in_month = days_in_month[leap][month - 1]
dt.dst = -1
dt.tz = "???"
dt.is_leapyear = leap
dt.yearoffset = yearoffset
return dt
def now(
time=time.time,float=float,localtime=time.localtime,
round=round,int=int,DateTime=DateTime,floor=math.floor):
ticks = time()
Y,M,D,h,m,s = localtime(ticks)[:6]
s = s + (ticks - floor(ticks))
return DateTime(Y,M,D,h,m,s)
def utc(
time=time.time,float=float,gmtime=time.gmtime,
round=round,int=int,DateTime=DateTime,floor=math.floor):
ticks = time()
Y,M,D,h,m,s = gmtime(ticks)[:6]
s = s + (ticks - floor(ticks))
return DateTime(Y,M,D,h,m,s)
# Aliases
Date = Timestamp = DateTime
# XXX Calendars are not supported:
def notSupported(*args,**kws):
raise Error,'calendars are not supported by the Python version of mxDateTime'
JulianDateTime = notSupported
### DateTimeDelta class
class DateTimeDelta:
def __init__(self, days=0, hours=0, minutes=0, seconds=0):
seconds = seconds + (days * 86400.0 + hours * 3600.0 + minutes * 60.0)
self.seconds = seconds
if seconds < 0.0:
seconds = -seconds
day = long(seconds / 86400.0)
seconds = seconds - (86400.0 * day)
wholeseconds = int(seconds)
hour = wholeseconds / 3600
minute = (wholeseconds % 3600) / 60
second = seconds - (hour * 3600.0 + minute * 60.0)
self.day = day
self.hour = hour
self.minute = minute
self.second = second
seconds=self.seconds
self.minutes = seconds / 60.0
self.hours = seconds / 3600.0
self.days = seconds / 86400.0
def __str__(self):
if self.day != 0:
if self.seconds >= 0.0:
r="%s:%02d:%02d:%05.2f" % (
self.day, self.hour, self.minute, self.second)
else:
r="-%s:%02d:%02d:%05.2f" % (
self.day, self.hour, self.minute, self.second)
else:
if self.seconds >= 0.0:
r="%02d:%02d:%05.2f" % (self.hour, self.minute, self.second)
else:
r="-%02d:%02d:%05.2f" % (self.hour, self.minute, self.second)
return r
def absvalues(self):
days=self.seconds / 86400
seconds=self.seconds - (days * 86400.0)
return days, seconds
def tuple(self):
return (self.day, self.hour, self.minute, self.second)
def strftime(self, format_string):
raise NotImplementedError
def __int__(self):
return int(self.seconds)
def __float__(self):
return self.seconds
def __cmp__(self, other, accuracy=0.0):
if (type(other) == types.InstanceType
and other.__class__ == DateTimeDelta):
diff=self.seconds - other.seconds
if abs(diff) > accuracy:
if diff > 0: return 1
return -1
elif type(other) == types.FloatType:
diff=self.seconds - other
if abs(diff) > accuracy:
if diff > 0: return 1
return -1
elif type(other) == types.IntType:
diff=self.seconds - other
if abs(diff) > accuracy:
if diff > 0: return 1
return -1
return 0
def __getattr__(self, attr):
seconds=self.__dict__['seconds']
if attr in ('hour', 'minute', 'second', 'day'):
if seconds >= 0.0:
return self.__dict__[attr]
else:
return -self.__dict__[attr]
else:
try:
return self.__dict__[attr]
except:
raise AttributeError, attr
def __div__(self, other):
if type(other) in (types.IntType, types.FloatType):
return DateTimeDelta(0.0,0.0,0.0,self.seconds / other)
elif (type(other) == types.InstanceType
and isinstance(other,DateTimeDelta)):
return DateTimeDelta(0.0,0.0,0.0,self.seconds / other.seconds)
raise TypeError, "bad operand types for /"
def __mul__(self, other):
if type(other) == types.IntType or type(other) == types.FloatType:
return DateTimeDelta(0.0,0.0,0.0,self.seconds * other)
else:
#print "type", type(other)
raise TypeError, "cannot multiply these two types"
def __rmul__(self, other):
return self.__mul__(other)
def __neg__(self):
return DateTimeDelta(0.0,0.0,0.0,-self.seconds)
def __repr__(self):
if self.day != 0:
if self.seconds >= 0.0:
strval="%s:%02d:%02d:%05.2f" % (self.day, self.hour,
self.minute, self.second)
else:
strval="-%s:%02d:%02d:%05.2f" % (self.day, self.hour,
self.minute, self.second)
else:
if self.seconds >= 0.0:
strval="%02d:%02d:%05.2f" % (self.hour, self.minute,
self.second)
else:
strval="-%02d:%02d:%05.2f" % (self.hour, self.minute,
self.second)
return "<DateTimeDelta object for '%s' at %x>" % (strval, id(self))
def __abs__(self):
if self.seconds < 0:
return -self
return self
def __nonzero__(self):
return self.seconds != 0.0
def __add__(self, other):
if type(other) == types.InstanceType:
if isinstance(other,DateTime):
return other + self
elif isinstance(other,DateTimeDelta):
return DateTimeDelta(0.0,0.0,0.0,self.seconds + other.seconds)
# What about __radd__ ?
# Other DateTimeDelta constructors
def TimeDelta(hour=0.0, minute=0.0, second=0.0):
return DateTimeDelta(0.0, hours, minutes, seconds)
Time=TimeDelta
def DateTimeDeltaFromSeconds(seconds):
return DateTimeDelta(0.0,0.0,0.0,seconds)
def DateTimeDeltaFromDays(days):
return DateTimeDelta(days)
### Types
DateTimeType = DateTime
DateTimeDeltaType = DateTimeDelta
### Functions
def cmp(a,b,acc):
if isinstance(a,DateTime) and isinstance(b,DateTime):
diff = a.absdays - b.absdays
if (diff >= 0 and diff <= acc) or (diff < 0 and -diff <= acc):
return 0
elif diff < 0:
return 1
else:
return -1
elif isinstance(a,DateTimeDelta) and isinstance(b,DateTimeDelta):
diff = a.days - b.days
if (diff >= 0 and diff <= acc) or (diff < 0 and -diff <= acc):
return 0
elif diff < 0:
return 1
else:
return -1
else:
raise TypeError,"objects must be DateTime[Delta] instances"
|