/usr/share/pyshared/pycalendar/datetime.py is in python-pycalendar 2.0~svn188-1.
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 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 | ##
# Copyright (c) 2007-2011 Cyrus Daboo. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
from pycalendar import definitions
from pycalendar import locale
from pycalendar import utils
from pycalendar import xmldefs
from pycalendar.duration import PyCalendarDuration
from pycalendar.timezone import PyCalendarTimezone
from pycalendar.valueutils import ValueMixin
import cStringIO as StringIO
import time
import xml.etree.cElementTree as XML
class PyCalendarDateTime(ValueMixin):
SUNDAY = 0
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6
FULLDATE = 0
ABBREVDATE = 1
NUMERICDATE = 2
FULLDATENOYEAR = 3
ABBREVDATENOYEAR = 4
NUMERICDATENOYEAR = 5
@staticmethod
def sort(e1, e2):
return e1.compareDateTime(e2)
def __init__( self, year = None, month = None, day = None, hours = None, minutes = None, seconds = None, tzid = None, utcoffset = None ):
self.mYear = 1970
self.mMonth = 1
self.mDay = 1
self.mHours = 0
self.mMinutes = 0
self.mSeconds = 0
self.mDateOnly = False
self.mTZUTC = False
self.mTZID = None
self.mPosixTimeCached = False
self.mPosixTime = 0
if (year is not None) and (month is not None) and (day is not None):
self.mYear = year
self.mMonth = month
self.mDay = day
if (hours is not None) and (minutes is not None) and (seconds is not None):
self.mHours = hours
self.mMinutes = minutes
self.mSeconds = seconds
else:
self.mDateOnly = True
if tzid:
self.mTZUTC = tzid.getUTC()
self.mTZID = tzid.getTimezoneID()
def duplicate(self):
other = PyCalendarDateTime(self.mYear, self.mMonth, self.mDay, self.mHours, self.mMinutes, self.mSeconds)
other.mDateOnly = self.mDateOnly
other.mTZUTC = self.mTZUTC
other.mTZID = self.mTZID
other.mPosixTimeCached = self.mPosixTimeCached
other.mPosixTime = self.mPosixTime
return other
def duplicateAsUTC(self):
other = self.duplicate()
other.adjustToUTC()
return other
def __repr__(self):
return "PyCalendarDateTime: %s" % (self.getText(),)
def __hash__(self):
return hash(self.getPosixTime())
# Operators
def __add__( self, duration ):
# Add duration seconds to temp object and normalise it
result = self.duplicate()
result.mSeconds += duration.getTotalSeconds()
result.normalise()
return result
def __sub__( self, dateorduration ):
if isinstance(dateorduration, PyCalendarDateTime):
date = dateorduration
# Look for floating
if self.floating() or date.floating():
# Adjust the floating ones to fixed
copy1 = self.duplicate()
copy2 = date.duplicate()
if copy1.floating() and copy2.floating():
# Set both to UTC and do comparison
copy1.setTimezoneUTC( True )
copy2.setTimezoneUTC( True )
return copy1 - copy2
elif copy1.floating():
# Set to be the same
copy1.setTimezoneID( copy2.getTimezoneID() )
copy1.setTimezoneUTC( copy2.getTimezoneUTC() )
return copy1 - copy2
else:
# Set to be the same
copy2.setTimezoneID( copy1.getTimezoneID() )
copy2.setTimezoneUTC( copy1.getTimezoneUTC() )
return copy1 - copy2
else:
# Do diff of date-time in seconds
diff = self.getPosixTime() - date.getPosixTime()
return PyCalendarDuration(duration=diff)
elif isinstance(dateorduration, PyCalendarDuration):
duration = dateorduration
result = self.duplicate()
result.mSeconds -= duration.getTotalSeconds()
result.normalise()
return result
raise ValueError
# Comparators
def __eq__( self, comp ):
return self.compareDateTime( comp ) == 0
def __ne__( self, comp ):
return self.compareDateTime( comp ) != 0
def __ge__( self, comp ):
return self.compareDateTime( comp ) >= 0
def __le__( self, comp ):
return self.compareDateTime( comp ) <= 0
def __gt__( self, comp ):
return self.compareDateTime( comp ) > 0
def __lt__( self, comp ):
return self.compareDateTime( comp ) < 0
def compareDateTime( self, comp ):
if comp is None:
return 1
# If either are date only, then just do date compare
if self.mDateOnly or comp.mDateOnly:
if self.mYear == comp.mYear:
if self.mMonth == comp.mMonth:
if self.mDay == comp.mDay:
return 0
else:
if self.mDay < comp.mDay:
return -1
else:
return 1
else:
if self.mMonth < comp.mMonth:
return -1
else:
return 1
else:
if self.mYear < comp.mYear:
return -1
else:
return 1
# If they have the same timezone do simple compare - no posix calc
# needed
elif ( PyCalendarTimezone.same( self.mTZUTC, self.mTZID, comp.mTZUTC, comp.mTZID ) ):
if self.mYear == comp.mYear:
if self.mMonth == comp.mMonth:
if self.mDay == comp.mDay:
if self.mHours == comp.mHours:
if self.mMinutes == comp.mMinutes:
if self.mSeconds == comp.mSeconds:
return 0
else:
if self.mSeconds < comp.mSeconds:
return -1
else:
return 1
else:
if self.mMinutes < comp.mMinutes:
return -1
else:
return 1
else:
if self.mHours < comp.mHours:
return -1
else:
return 1
else:
if self.mDay < comp.mDay:
return -1
else:
return 1
else:
if self.mMonth < comp.mMonth:
return -1
else:
return 1
else:
if self.mYear < comp.mYear:
return -1
else:
return 1
else:
posix1 = self.getPosixTime()
posix2 = comp.getPosixTime()
if posix1 == posix2:
return 0
else:
if posix1 < posix2:
return -1
else:
return 1
def compareDate( self, comp ):
return ( self.mYear == comp.mYear ) and ( self.mMonth == comp.mMonth ) and ( self.mDay == comp.mDay )
def getPosixTime( self ):
# Look for cached value (or floating time which has to be calculated
# each time)
if ( not self.mPosixTimeCached ) or self.floating():
result = 0L
# Add hour/mins/secs
result = ( self.mHours * 60L + self.mMinutes ) * 60L + self.mSeconds
# Number of days since 1970
result += self.daysSince1970() * 24L * 60L * 60L
# Adjust for timezone offset
result -= self.timeZoneSecondsOffset()
# Now indicate cache state
self.mPosixTimeCached = True
self.mPosixTime = result
return self.mPosixTime
def isDateOnly( self ):
return self.mDateOnly
def setDateOnly( self, date_only ):
self.mDateOnly = date_only
self.changed()
def getYear( self ):
return self.mYear
def setYear( self, year ):
if self.mYear != year:
self.mYear = year
self.changed()
def offsetYear( self, diff_year ):
self.mYear += diff_year
self.normalise()
def getMonth( self ):
return self.mMonth
def setMonth( self, month ):
if self.mMonth != month:
self.mMonth = month
self.changed()
def offsetMonth( self, diff_month ):
self.mMonth += diff_month
self.normalise()
def getDay( self ):
return self.mDay
def setDay( self, day ):
if self.mDay != day:
self.mDay = day
self.changed()
def offsetDay( self, diff_day ):
self.mDay += diff_day
self.normalise()
def setYearDay( self, day ):
# 1 .. 366 offset from start, or
# -1 .. -366 offset from end
if day > 0:
# Offset current date to 1st January of current year
self.mMonth = 1
self.mDay = 1
# Increment day
self.mDay += day - 1
# Normalise to get proper month/day values
self.normalise()
elif day < 0:
# Offset current date to 1st January of next year
self.mYear += 1
self.mMonth = 1
self.mDay = 1
# Decrement day
self.mDay += day
# Normalise to get proper year/month/day values
self.normalise()
def getYearDay( self ):
return self.mDay + utils.daysUptoMonth( self.mMonth, self.mYear )
def setMonthDay( self, day ):
# 1 .. 31 offset from start, or
# -1 .. -31 offset from end
if day > 0:
# Offset current date to 1st of current month
self.mDay = 1
# Increment day
self.mDay += day - 1
# Normalise to get proper month/day values
self.normalise()
elif day < 0:
# Offset current date to 1st of next month
self.mMonth += 1
self.mDay = 1
# Decrement day
self.mDay += day
# Normalise to get proper year/month/day values
self.normalise()
def isMonthDay( self, day ):
if day > 0:
return self.mDay == day
elif day < 0:
return self.mDay - 1 - utils.daysInMonth( self.mMonth, self.mYear ) == day
else:
return False
def setWeekNo( self, weekno ):
# This is the iso 8601 week number definition
# What day does the current year start on
temp = PyCalendarDateTime(year=self.mYear, month=1, day=1)
first_day = temp.getDayOfWeek()
# Calculate and set yearday for start of week
if ( first_day == PyCalendarDateTime.SUNDAY ) or ( first_day == PyCalendarDateTime.MONDAY ) or \
( first_day == PyCalendarDateTime.TUESDAY ) or ( first_day == PyCalendarDateTime.WEDNESDAY ) or ( first_day == PyCalendarDateTime.THURSDAY ):
self.setYearDay( ( weekno - 1 ) * 7 - first_day )
elif ( first_day == PyCalendarDateTime.FRIDAY ) or ( first_day == PyCalendarDateTime.SATURDAY ):
self.setYearDay( ( weekno - 1 ) * 7 - first_day + 7 )
def getWeekNo( self ):
# This is the iso 8601 week number definition
# What day does the current year start on
temp = PyCalendarDateTime(year=self.mYear, month=1, day=1)
first_day = temp.getDayOfWeek()
# Get days upto the current one
yearday = self.getYearDay()
if ( first_day == PyCalendarDateTime.SUNDAY ) or ( first_day == PyCalendarDateTime.MONDAY ) or \
( first_day == PyCalendarDateTime.TUESDAY ) or ( first_day == PyCalendarDateTime.WEDNESDAY ) or ( first_day == PyCalendarDateTime.THURSDAY ):
return ( yearday + first_day ) / 7 + 1
elif ( first_day == PyCalendarDateTime.FRIDAY ) or ( first_day == PyCalendarDateTime.SATURDAY ):
return ( yearday + first_day - 7 ) / 7 + 1
def isWeekNo( self, weekno ):
# This is the iso 8601 week number definition
if weekno > 0:
return self.getWeekNo() == weekno
else:
# This needs to calculate the negative offset from the last week in
# the current year
return False
def setDayOfWeekInYear( self, offset, day ):
# Set to first day in year
self.mMonth = 1
self.mDay = 1
# Determine first weekday in year
first_day = self.getDayOfWeek()
if offset > 0:
cycle = ( offset - 1 ) * 7 + day
cycle -= first_day
if first_day > day:
cycle += 7
self.mDay = cycle + 1
elif offset < 0:
first_day += 365 + [1, 0][not utils.isLeapYear( self.mYear )] - 1
first_day %= 7
cycle = ( -offset - 1 ) * 7 - day
cycle += first_day
if day > first_day:
cycle += 7
self.mDay = 365 + [1, 0][not utils.isLeapYear( self.mYear )] - cycle
self.normalise()
def setDayOfWeekInMonth( self, offset, day ):
# Set to first day in month
self.mDay = 1
# Determine first weekday in month
first_day = self.getDayOfWeek()
if offset > 0:
cycle = ( offset - 1 ) * 7 + day
cycle -= first_day
if first_day > day:
cycle += 7
self.mDay = cycle + 1
elif offset < 0:
days_in_month = utils.daysInMonth( self.mMonth, self.mYear )
first_day += days_in_month - 1
first_day %= 7
cycle = ( -offset - 1 ) * 7 - day
cycle += first_day
if day > first_day:
cycle += 7
self.mDay = days_in_month - cycle
self.normalise()
def setNextDayOfWeek( self, start, day ):
# Set to first day in month
self.mDay = start
# Determine first weekday in month
first_day = self.getDayOfWeek()
if first_day > day:
self.mDay += 7
self.mDay += day - first_day
self.normalise()
def isDayOfWeekInMonth( self, offset, day ):
# First of the actual day must match
if self.getDayOfWeek() != day:
return False
# If there is no count the we match any of this day in the month
if offset == 0:
return True
# Create temp date-time with the appropriate parameters and then
# compare
temp = self.duplicate()
temp.setDayOfWeekInMonth( offset, day )
# Now compare dates
return self.compareDate( temp )
def getDayOfWeek( self ):
# Count days since 01-Jan-1970 which was a Thursday
result = PyCalendarDateTime.THURSDAY + self.daysSince1970()
result %= 7
if result < 0:
result += 7
return result
def getMonthText( self, short_txt ):
# Make sure range is valid
if ( self.mMonth < 1 ) or ( self.mMonth > 12 ):
return ""
else:
return locale.getMonth( self.mMonth,
[locale.SHORT, locale.LONG][not short_txt] )
def getDayOfWeekText( self, day ):
return locale.getDay( day, locale.SHORT )
def setHHMMSS( self, hours, minutes, seconds ):
if ( self.mHours != hours ) or ( self.mMinutes != minutes ) or ( self.mSeconds != seconds ):
self.mHours = hours
self.mMinutes = minutes
self.mSeconds = seconds
self.changed()
def getHours( self ):
return self.mHours
def setHours( self, hours ):
if self.mHours != hours:
self.mHours = hours
self.changed()
def offsetHours( self, diff_hour ):
self.mHours += diff_hour
self.normalise()
def getMinutes( self ):
return self.mMinutes
def setMinutes( self, minutes ):
if self.mMinutes != minutes:
self.mMinutes = minutes
self.changed()
def offsetMinutes( self, diff_minutes ):
self.mMinutes += diff_minutes
self.normalise()
def getSeconds( self ):
return self.mSeconds
def setSeconds( self, seconds ):
if self.mSeconds != seconds:
self.mSeconds = seconds
self.changed()
def offsetSeconds( self, diff_seconds ):
self.mSeconds += diff_seconds
self.normalise()
def getTimezoneUTC( self ):
return self.mTZUTC
def setTimezoneUTC( self, utc ):
if self.mTZUTC != utc:
self.mTZUTC = utc
self.changed()
def getTimezoneID( self ):
return self.mTZID
def setTimezoneID( self, tzid ):
self.mTZUTC = False
self.mTZID = tzid
self.changed()
def utc( self ):
return self.mTZUTC
def local( self ):
return ( not self.mTZUTC ) and self.mTZID
def floating( self ):
return ( not self.mTZUTC ) and not self.mTZID
def getTimezone(self):
return PyCalendarTimezone(utc=self.mTZUTC, tzid=self.mTZID)
def setTimezone( self, tzid ):
self.mTZUTC = tzid.getUTC()
self.mTZID = tzid.getTimezoneID()
self.changed()
def adjustTimezone( self, tzid ):
# Only if different
s1 = tzid.getTimezoneID()
if ( tzid.getUTC() != self.mTZUTC ) or ( s1 != self.mTZID ):
offset_from = self.timeZoneSecondsOffset()
self.setTimezone( tzid )
offset_to = self.timeZoneSecondsOffset()
self.offsetSeconds( offset_to - offset_from )
return self
def adjustToUTC( self ):
if self.local() and not self.mDateOnly:
utc = PyCalendarTimezone(utc=True)
offset_from = self.timeZoneSecondsOffset()
self.setTimezone( utc )
offset_to = self.timeZoneSecondsOffset()
self.offsetSeconds( offset_to - offset_from )
return self
def getAdjustedTime( self, tzid = None ):
# Copy this and adjust to input timezone
adjusted = self.duplicate()
adjusted.adjustTimezone( tzid )
return adjusted
def setToday( self ):
tz = PyCalendarTimezone(utc=self.mTZUTC, tzid=self.mTZID)
self.copy_ICalendarDateTime( self.getToday( tz ) )
@staticmethod
def getToday( tzid=None ):
# Get from posix time
now = time.time()
now_tm = time.localtime( now )
temp = PyCalendarDateTime(year=now_tm.tm_year, month=now_tm.tm_mon, day=now_tm.tm_mday, tzid=tzid)
return temp
def setNow( self ):
tz = PyCalendarTimezone(utc=self.mTZUTC, tzid=self.mTZID)
self.copy_ICalendarDateTime( self.getNow( tz ) )
@staticmethod
def getNow( tzid ):
utc = PyCalendarDateTime.getNowUTC()
utc.adjustTimezone( tzid if tzid is not None else PyCalendarTimezone() )
return utc
def setNowUTC( self ):
self.copy_PyCalendarDateTime( self.getNowUTC() )
@staticmethod
def getNowUTC( ):
# Get from posix time
now = time.time()
now_tm = time.gmtime( now )
tzid = PyCalendarTimezone(utc=True)
return PyCalendarDateTime(year=now_tm.tm_year, month=now_tm.tm_mon, day=now_tm.tm_mday, hours=now_tm.tm_hour, minutes=now_tm.tm_min, seconds=now_tm.tm_sec, tzid=tzid )
def recur( self, freq, interval ):
# Add appropriate interval
if freq == definitions.eRecurrence_SECONDLY:
self.mSeconds += interval
elif freq == definitions.eRecurrence_MINUTELY:
self.mMinutes += interval
elif freq == definitions.eRecurrence_HOURLY:
self.mHours += interval
elif freq == definitions.eRecurrence_DAILY:
self.mDay += interval
elif freq == definitions.eRecurrence_WEEKLY:
self.mDay += 7 * interval
elif freq == definitions.eRecurrence_MONTHLY:
# Iterate until a valid day in the next month is found.
# This can happen if adding one month to e.g. 31 January.
# That is an undefined operation - does it mean 28/29 February
# or 1/2 May, or 31 March or what? We choose to find the next month with
# the same day number as the current one.
self.mMonth += interval
self.normalise()
while self.mDay > utils.daysInMonth( self.mMonth, self.mYear ):
self.mMonth += interval
self.normalise()
elif freq == definitions.eRecurrence_YEARLY:
self.mYear += interval
# Normalise to standard date-time ranges
self.normalise()
def getLocaleDate( self, locale ):
buf = StringIO.StringIO()
if locale == PyCalendarDateTime.FULLDATE:
buf.write( locale.getDay( self.getDayOfWeek(), locale.LONG ) )
buf.write( ", " )
buf.write( locale.getMonth( self.mMonth, locale.LONG ) )
buf.write( " " )
buf.write( str( self.mDay ) )
buf.write( ", " )
buf.write( str( self.mYear ) )
elif locale == PyCalendarDateTime.ABBREVDATE:
buf.write( locale.getDay( self.getDayOfWeek(), locale.SHORT ) )
buf.write( ", " )
buf.write( locale.getMonth( self.mMonth, locale.SHORT ) )
buf.write( " " )
buf.write( str( self.mDay ) )
buf.write( ", " )
buf.write( str( self.mYear ) )
elif locale == PyCalendarDateTime.NUMERICDATE:
buf.write( str( self.mMonth ) )
buf.write( "/" )
buf.write( str( self.mDay ) )
buf.write( "/" )
buf.write( str( self.mYear ) )
elif locale == PyCalendarDateTime.FULLDATENOYEAR:
buf.write( locale.getDay( self.getDayOfWeek(), locale.LONG ) )
buf.write( ", " )
buf.write( locale.getMonth( self.mMonth, locale.LONG ) )
buf.write( " " )
buf.write( str( self.mDay ) )
elif locale == PyCalendarDateTime.ABBREVDATENOYEAR:
buf.write( locale.getDay( self. getDayOfWeek(), locale.SHORT ) )
buf.write( ", " )
buf.write( locale.getMonth( self.mMonth, locale.SHORT ) )
buf.write( " " )
buf.write( str( self.mDay ) )
elif locale == PyCalendarDateTime.NUMERICDATENOYEAR:
buf.write( str( self.mMonth ) )
buf.write( "/" )
buf.write( str( self.mDay ) )
return buf.getvalue()
def getTime( self, with_seconds, am_pm, tzid ):
buf = StringIO.StringIO()
adjusted_hour = self.mHours
if am_pm:
am = True
if adjusted_hour >= 12:
adjusted_hour -= 12
am = False
if adjusted_hour == 0:
adjusted_hour = 12
buf.write( str( adjusted_hour ) )
buf.write( ":" )
if self.mMinutes < 10:
buf.write( "0" )
buf.write( str( self.mMinutes ) )
if with_seconds:
buf.write( ":" )
if self.mSeconds < 10:
buf.write( "0" )
buf.write( str( self.mSeconds ) )
buf.write( [" AM", " PM"][not am] )
else:
if self.mHours < 10:
buf.write( "0" )
buf.write( str( self.mHours ) )
buf.write( ":" )
if self.mMinutes < 10:
buf.write( "0" )
buf.write( str( self.mMinutes ) )
if with_seconds:
buf.write( ":" )
if self.mSeconds < 10:
buf.write( "0" )
buf.write( str( self.mSeconds ) )
if tzid:
desc = self.timeZoneDescriptor()
if len( desc ) > 0:
buf.write( " " )
buf.write( desc )
return buf.getvalue()
def getLocaleDateTime( self, locale, with_seconds, am_pm, tzid ):
return self.getLocaleDate( locale ) + " " + self.getTime( with_seconds, am_pm, tzid )
def getText( self ):
if self.mDateOnly:
return "%04d%02d%02d" % ( self.mYear, self.mMonth, self.mDay )
else:
if self.mTZUTC:
return "%04d%02d%02dT%02d%02d%02dZ" % ( self.mYear, self.mMonth, self.mDay, self.mHours, self.mMinutes, self.mSeconds )
elif isinstance(self.mTZID, int):
sign = "-" if self.mTZID < 0 else "+"
hours = abs(self.mTZID) / 3600
minutes = divmod(abs(self.mTZID) / 60, 60)[1]
return "%04d%02d%02dT%02d%02d%02d%s%02d%02d" % ( self.mYear, self.mMonth, self.mDay, self.mHours, self.mMinutes, self.mSeconds, sign, hours, minutes )
else:
return "%04d%02d%02dT%02d%02d%02d" % ( self.mYear, self.mMonth, self.mDay, self.mHours, self.mMinutes, self.mSeconds )
def getXMLText( self ):
if self.mDateOnly:
return "%04d-%02d-%02d" % ( self.mYear, self.mMonth, self.mDay )
else:
if self.mTZUTC:
return "%04d-%02d-%02dT%02d:%02d:%02dZ" % ( self.mYear, self.mMonth, self.mDay, self.mHours, self.mMinutes, self.mSeconds )
elif isinstance(self.mTZID, int):
sign = "-" if self.mTZID < 0 else "+"
hours = abs(self.mTZID) / 3600
minutes = divmod(abs(self.mTZID) / 60, 60)[1]
return "%04d-%02d-%02dT%02d:%02d:%02d%s%02d:%02d" % ( self.mYear, self.mMonth, self.mDay, self.mHours, self.mMinutes, self.mSeconds, sign, hours, minutes )
else:
return "%04d-%02d-%02dT%02d:%02d:%02d" % ( self.mYear, self.mMonth, self.mDay, self.mHours, self.mMinutes, self.mSeconds )
@classmethod
def parseText(cls, data, fullISO=False):
dt = cls()
dt.parse(data, fullISO)
return dt
def parseDate(self, data, fullISO):
# Get year
self.mYear = int(data[0:4])
index = 4
if fullISO and data[index] == "-":
index += 1
# Get month
self.mMonth = int(data[index:index + 2])
index += 2
if fullISO and data[index] == "-":
index += 1
# Get day
self.mDay = int(data[index:index + 2])
index += 2
if ' ' in data[:index]:
raise ValueError
if self.mYear < 0 or self.mMonth < 0 or self.mDay < 0:
raise ValueError
return index
def parseTime(self, data, index, fullISO):
# Get hours
self.mHours = int(data[index:index + 2])
index += 2
if fullISO and data[index] == ":":
index += 1
# Get minutes
self.mMinutes = int(data[index:index + 2])
index += 2
if fullISO and data[index] == ":":
index += 1
# Get seconds
self.mSeconds = int(data[index:index + 2])
index += 2
return index
def parseFractionalAndUTCOffset(self, data, index, dlen):
# Optional fraction
if data[index] == ",":
index += 1
if not data[index].isdigit():
raise ValueError
while index < dlen and data[index].isdigit():
index += 1
# Optional timezone descriptor
if index < dlen:
if data[index] == "Z":
index += 1
self.mTZUTC = True
else:
if data[index] == "+":
sign = 1
elif data[index] == "-":
sign = -1
else:
raise ValueError
index += 1
# Get hours
hours_offset = int(data[index:index + 2])
index += 2
if data[index] == ":":
index += 1
# Get minutes
minutes_offset = int(data[index:index + 2])
index += 2
self.mTZID = sign * (hours_offset * 60 + minutes_offset) * 60
if index < dlen:
raise ValueError
return index
def parse(self, data, fullISO=False):
# iCalendar:
# parse format YYYYMMDD[THHMMSS[Z]]
# vCard (fullISO)
# parse format YYYY[-]MM[-]DD[THH[:]MM[:]SS[(Z/(+/-)HHMM]]
try:
# Parse out the date
index = self.parseDate(data, fullISO)
# Now look for more
dlen = len(data)
if index < dlen:
if data[index] != 'T':
raise ValueError
index += 1
# Parse out the time
index = self.parseTime(data, index, fullISO)
self.mDateOnly = False
if index < dlen:
if fullISO:
index = self.parseFractionalAndUTCOffset(data, index, dlen)
else:
if index < dlen:
if data[index] != 'Z':
raise ValueError
index += 1
self.mTZUTC = True
if index < dlen:
raise ValueError
else:
self.mTZUTC = False
else:
self.mDateOnly = True
except IndexError:
raise ValueError
# Always uncache posix time
self.changed()
def generate( self, os ):
try:
os.write( self.getText() )
except:
pass
def generateRFC2822( self, os ):
pass
def writeXML(self, node, namespace):
value = XML.SubElement(
node,
xmldefs.makeTag(
namespace,
xmldefs.value_date if self.isDateOnly() else xmldefs.value_date_time
))
value.text = self.getXMLText()
def normalise( self ):
# Normalise seconds
normalised_secs = self.mSeconds % 60
adjustment_mins = self.mSeconds / 60
if normalised_secs < 0:
normalised_secs += 60
adjustment_mins -= 1
self.mSeconds = normalised_secs
self.mMinutes += adjustment_mins
# Normalise minutes
normalised_mins = self.mMinutes % 60
adjustment_hours = self.mMinutes / 60
if normalised_mins < 0:
normalised_mins += 60
adjustment_hours -= 1
self.mMinutes = normalised_mins
self.mHours += adjustment_hours
# Normalise hours
normalised_hours = self.mHours % 24
adjustment_days = self.mHours / 24
if normalised_hours < 0:
normalised_hours += 24
adjustment_days -= 1
self.mHours = normalised_hours
self.mDay += adjustment_days
# Wipe the time if date only
if self.mDateOnly:
self.mSeconds = self.mMinutes = self.mHours = 0
# Adjust the month first, since the day adjustment is month dependent
# Normalise month
normalised_month = ( ( self.mMonth - 1 ) % 12 ) + 1
adjustment_year = ( self.mMonth - 1 ) / 12
if ( normalised_month - 1 ) < 0:
normalised_month += 12
adjustment_year -= 1
self.mMonth = normalised_month
self.mYear += adjustment_year
# Now do days
if self.mDay > 0:
while self.mDay > utils.daysInMonth( self.mMonth, self.mYear ):
self.mDay -= utils.daysInMonth( self.mMonth, self.mYear )
self.mMonth += 1
if self.mMonth > 12:
self.mMonth = 1
self.mYear += 1
else:
while self.mDay <= 0:
self.mMonth -= 1
if self.mMonth < 1:
self.mMonth = 12
self.mYear -= 1
self.mDay += utils.daysInMonth( self.mMonth, self.mYear )
# Always invalidate posix time cache
self.changed()
def timeZoneSecondsOffset( self ):
if self.mTZUTC:
return 0
tz = PyCalendarTimezone(utc=self.mTZUTC, tzid=self.mTZID)
return tz.timeZoneSecondsOffset( self )
def timeZoneDescriptor( self ):
tz = PyCalendarTimezone(utc=self.mTZUTC, tzid=self.mTZID)
return tz.timeZoneDescriptor( self )
def changed( self ):
self.mPosixTimeCached = False
def daysSince1970( self ):
# Add days between 1970 and current year (ignoring leap days)
result = ( self.mYear - 1970 ) * 365
# Add leap days between years
result += utils.leapDaysSince1970( self.mYear - 1970 )
# Add days in current year up to current month (includes leap day for
# current year as needed)
result += utils.daysUptoMonth( self.mMonth, self.mYear )
# Add days in month
result += self.mDay - 1
return result
|