~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to lib/common/date.py

  • Committer: wagrant
  • Date: 2008-12-20 03:46:56 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1060
Make IVLE work fine in Firefox 3.1 (ie. Gecko/XULRunner 1.9.1). Gecko 1.9.1 has
a built-in JSON object, so our json2.js isn't used, and compatibility problems
arise. The only one significant to us is that Gecko's doesn't support having
a primitive as the root object. Unfortunately we use that sort of thing in a
few places, so IVLE ends up pretty much broken.

A few adjustments were needed both client- and server-side for proto changes:

 - consoleservice and the JS interacting with it now deal with the key returned
   by start_server in a dict like {'key': ...}.
 - tutorialservice and interacting JS deal with code returned by getattempt in
   a dict like {'code': ...}.


The JS repr() implementation used JSON.stringify on the given object, and was
used only with primitive objects in order to create function calls. This needed
to be eliminated for compatibility with the new Gecko, so these call
constructions were replaced with better events:

 - listing.js constructed events for generated file listings by repr()ing some
   strings. That is foul, so we instead use DOM operations from 'this' in a
   somewhat nicer event handler.
 - tutorial.js previously created save_exercise() call strings by repr()ing
   lots of stuff. That was really bad, and broke like the rest. I replaced it
   with something that's still bad, but not quite as awful - grabbing the call
   string from the save button. This should be fixed at some point.
 - console.js had some strange Firebug logging stuff which used repr(). I
   removed it, as it shouldn't have been in released code in the first place.

repr() itself was removed after all references were.
   

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE
 
2
# Copyright (C) 2008 The University of Melbourne
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 
 
18
# Module: Date utilities
 
19
# Author: William Grant
 
20
# Date: 16/07/2008
 
21
 
 
22
import time
 
23
 
 
24
seconds_per_day = 86400 # 60 * 60 * 24
 
25
if time.daylight:
 
26
    timezone_offset = time.altzone
 
27
else:
 
28
    timezone_offset = time.timezone
 
29
 
 
30
def make_date_nice(seconds_since_epoch):
 
31
    """Given a number of seconds elapsed since the epoch,
 
32
    generates a string representing the date/time in human-readable form.
 
33
    "ddd mmm dd, yyyy h:m a"
 
34
    """
 
35
    #return time.ctime(seconds_since_epoch)
 
36
    return time.strftime("%a %b %d %Y, %I:%M %p",
 
37
        time.localtime(seconds_since_epoch))
 
38
 
 
39
def make_date_nice_short(seconds_since_epoch):
 
40
    """Given a number of seconds elapsed since the epoch,
 
41
    generates a string representing the date in human-readable form.
 
42
    Does not include the time.
 
43
    This function generates a very compact representation."""
 
44
    # Use a "naturalisation" algorithm.
 
45
    days_ago = (int(time.time() - timezone_offset) / seconds_per_day
 
46
        - int(seconds_since_epoch - timezone_offset) / seconds_per_day)
 
47
    if days_ago <= 5:
 
48
        # Dates today or yesterday, return "today" or "yesterday".
 
49
        if days_ago == 0:
 
50
            return "Today"
 
51
        elif days_ago == 1:
 
52
            return "Yesterday"
 
53
        else:
 
54
            return str(days_ago) + " days ago"
 
55
        # Dates in the last 5 days, return "n days ago".
 
56
    # Other dates, return a short date format.
 
57
    # If within the same year, omit the year (mmm dd)
 
58
    if time.localtime(seconds_since_epoch).tm_year==time.localtime().tm_year:
 
59
        return time.strftime("%b %d", time.localtime(seconds_since_epoch))
 
60
    # Else, include the year (mmm dd, yyyy)
 
61
    else:
 
62
        return time.strftime("%b %d, %Y", time.localtime(seconds_since_epoch))