29
29
ZERO = timedelta(0)
30
30
HOUR = timedelta(hours=1)
32
33
class FixedOffset(tzinfo):
33
34
"""Fixed offset in minutes east from UTC."""
35
36
def __init__(self, offset, name):
36
self.__offset = timedelta(minutes = offset)
37
self.__offset = timedelta(minutes=offset)
39
40
def utcoffset(self, dt):
48
def do_custom_date( format, date, timezone, now= None ):
50
Returns a string formatted representation of date according to format. This accepts
51
all formats that strftime also accepts, but it also accepts some new options which are dependant
50
def do_custom_date(format, date, timezone, now=None):
51
"""Returns a string formatted representation of date according to format.
52
This accepts all formats that strftime also accepts, but it also accepts
53
some new options which are dependant on the current date.
53
55
%NY(format) (natural year) for example %NY(.%Y) will give ".2008" if this year is not 2008, else
55
57
%ND(alternatives) (natural day) for example %ND(%d.%m.%Y)
80
83
ForumStdTimeZone = FixedOffset(60, 'UTC+1')
82
85
# set the user's timezone information
83
ForumUserTimeZone = FixedOffset(timezone*60, tz_info)
86
ForumUserTimeZone = FixedOffset(timezone * 60, tz_info)
85
88
# if there is tzinfo not set
87
90
if not date.tzinfo:
88
91
date = date.replace(tzinfo=ForumStdTimeZone)
89
92
date = date.astimezone(ForumUserTimeZone)
90
except AttributeError: # maybe this is no valid date object?
93
except AttributeError: # maybe this is no valid date object?
93
96
# If it's done, timezone informations are now available ;)
96
99
def _replace_ny(g):
97
100
if now.year == date.year:
101
104
def _replace_nd(g):
102
delta = ddate(date.year,date.month,date.day) - \
103
ddate(now.year,now.month,now.day)
105
delta = ddate(date.year, date.month, date.day) - \
106
ddate(now.year, now.month, now.day)
104
107
if delta.days == 0:
105
108
return _(ur'\T\o\d\a\y')
106
109
elif delta.days == 1:
115
format = natural_year_expr.sub(_replace_ny,format)
116
format = natural_day_expr.sub(_replace_nd,format)
118
format = natural_year_expr.sub(_replace_ny, format)
119
format = natural_day_expr.sub(_replace_nd, format)
117
120
if oformat == format:
120
data = django_date(date,format)
123
data = django_date(date, format)
121
124
except NotImplementedError:
127
def custom_date( date, user ):
129
If this user is logged in, return his representation,
130
otherwise, return a sane default
131
def custom_date(date, user):
132
"""If this user is logged in, return his representation, otherwise, return
132
134
if not user.is_authenticated():
133
return do_custom_date( DEFAULT_TIME_DISPLAY, date, float(DEFAULT_TIME_ZONE) )
135
return do_custom_date(DEFAULT_TIME_DISPLAY, date, float(DEFAULT_TIME_ZONE))
135
userprofile = User.objects.get(username=user).wlprofile
136
return do_custom_date( userprofile.time_display, date, userprofile.time_zone )
137
userprofile = User.objects.get(username=user).wlprofile
138
return do_custom_date(userprofile.time_display, date, userprofile.time_zone)
137
139
except ObjectDoesNotExist:
138
return do_custom_date( DEFAULT_TIME_DISPLAY, date, float(DEFAULT_TIME_ZONE) )
140
return do_custom_date(DEFAULT_TIME_DISPLAY, date, float(DEFAULT_TIME_ZONE))
140
142
custom_date.is_safe = False
142
145
def total_seconds(td):
143
146
# Not defined in 2.6, so we redefine it.
144
147
return (td.microseconds + (td.seconds + td.days * 24. * 3600.) * 1000000.) / 1000000.