~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2005-03-14 07:40:45 UTC
  • Revision ID: mbp@sourcefrog.net-20050314074045-37aa087acfe6b97b
todo

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
 
19
19
import os, types, re, time, types
20
 
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
 
20
from stat import S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE
21
21
 
22
22
from errors import bailout
23
23
 
51
51
        return 'file'
52
52
    elif S_ISDIR(mode):
53
53
        return 'directory'
 
54
    elif S_ISLNK(mode):
 
55
        return 'symlink'
54
56
    else:
55
 
        bailout("can't handle file kind of %r" % fp)
 
57
        bailout("can't handle file kind with mode %o of %r" % (mode, f)) 
56
58
 
57
59
 
58
60
 
133
135
        import pwd
134
136
        uid = os.getuid()
135
137
        w = pwd.getpwuid(uid)
136
 
        realname, junk = w.pw_gecos.split(',', 1)
 
138
        gecos = w.pw_gecos
 
139
        comma = gecos.find(',')
 
140
        if comma == -1:
 
141
            realname = gecos
 
142
        else:
 
143
            realname = gecos[:comma]
137
144
        return '%s <%s@%s>' % (realname, w.pw_name, socket.getfqdn())
138
145
    except ImportError:
139
146
        pass
167
174
 
168
175
 
169
176
 
170
 
def format_date(t, inutc=False):
 
177
def local_time_offset():
 
178
    if time.daylight:
 
179
        return -time.altzone
 
180
    else:
 
181
        return -time.timezone
 
182
 
 
183
    
 
184
def format_date(t, offset=0, timezone='original'):
171
185
    ## TODO: Perhaps a global option to use either universal or local time?
172
186
    ## Or perhaps just let people set $TZ?
173
187
    import time
174
188
    
175
189
    assert isinstance(t, float)
176
190
    
177
 
    if inutc:
 
191
    if timezone == 'utc':
178
192
        tt = time.gmtime(t)
179
 
        zonename = 'UTC'
180
193
        offset = 0
181
 
    else:
 
194
    elif timezone == 'original':
 
195
        if offset == None:
 
196
            offset = 0
 
197
        tt = time.gmtime(t + offset)
 
198
    elif timezone == 'local':
182
199
        tt = time.localtime(t)
183
 
        if time.daylight:
184
 
            zonename = time.tzname[1]
185
 
            offset = - time.altzone
186
 
        else:
187
 
            zonename = time.tzname[0]
188
 
            offset = - time.timezone
189
 
            
 
200
        offset = local_time_offset()
 
201
    else:
 
202
        bailout("unsupported timezone format %r",
 
203
                ['options are "utc", "original", "local"'])
 
204
 
190
205
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
191
 
            + ' ' + zonename + ' '
192
 
            + '%+03d%02d' % (offset / 3600, (offset / 60) % 60))
 
206
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
193
207
 
194
208
 
195
209
def compact_date(when):