~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/utils/GDateTime.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it 
 
5
# and/or modify it under the terms of the GNU General Public 
 
6
# License as published by the Free Software Foundation; either 
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be 
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied 
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public 
 
15
# License along with program; see the file COPYING. If not, 
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place 
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2001-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# GDateTime.py
 
23
#
 
24
# DESCRIPTION:
 
25
#
 
26
# NOTES:
 
27
#
 
28
 
 
29
 
 
30
def isLeapYear(year):
 
31
  return divmod(year,400)[1] == 0 or \
 
32
      (divmod(year,4)[1] == 0 and divmod(year,100)[1] != 0)
 
33
 
 
34
class InvalidDate(StandardError):
 
35
  pass
 
36
 
 
37
class GDateTime:
 
38
  def __init__(self):
 
39
    self.month = 0
 
40
    self.day = 0
 
41
    self.year = 0
 
42
    self.hour = 0
 
43
    self.minute = 0
 
44
    self.second = 0
 
45
 
 
46
  def __repr__(self):
 
47
    return "%04d/%02d/%02d %02d:%02d:%02d" % \
 
48
      (self.year, self.month, self.day, self.hour, self.minute, self.second)
 
49
 
 
50
  def getDayOfWeek(self):
 
51
    # from the Calendar FAQ (http://www.pauahtun.org/CalendarFAQ/)
 
52
    # 0 = Sunday
 
53
    a = int((14 - self.month) / 12)
 
54
    y = self.year - a
 
55
    m = self.month + 12*a - 2
 
56
    return divmod(self.day + y + int(y/4) - int(y/100) + int(y/400) + (31*m)/12,7)[1]
 
57
 
 
58
 
 
59
  def validate(self):
 
60
    if not (\
 
61
        self.month >= 1 and self.month <= 12 and \
 
62
        self.year >= 0 and \
 
63
        self.day >= 1 and self.day <= ( \
 
64
             (self.month in (1,3,5,7,8,10,12) and 31) or \
 
65
             (self.month == 2 and (28 + isLeapYear(self.year))) \
 
66
             or 30) and \
 
67
        self.hour >= 0 and self.hour <= 23 and \
 
68
        self.minute >= 0 and self.minute <= 59 and \
 
69
        self.second >= 0 and self.second <= 59 ):
 
70
      tmsg =  _("Not a valid date")
 
71
      raise InvalidDate, tmsg
 
72
 
 
73