/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Aaron Bentley
  • Date: 2007-01-17 16:13:50 UTC
  • mfrom: (2236 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2240.
  • Revision ID: abentley@panoramicfeedback.com-20070117161350-z0poe4762mzt2mlb
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
from bzrlib.lazy_import import lazy_import
27
27
lazy_import(globals(), """
 
28
import codecs
 
29
from datetime import datetime
28
30
import errno
29
31
from ntpath import (abspath as _nt_abspath,
30
32
                    join as _nt_join,
379
381
        output_encoding = bzrlib.user_encoding
380
382
        mutter('cp0 is invalid encoding.'
381
383
               ' encoding stdout as bzrlib.user_encoding %r', output_encoding)
 
384
    # check encoding
 
385
    try:
 
386
        codecs.lookup(output_encoding)
 
387
    except LookupError:
 
388
        sys.stderr.write('bzr: warning:'
 
389
                         ' unknown terminal encoding %s.\n'
 
390
                         '  Using encoding %s instead.\n'
 
391
                         % (output_encoding, bzrlib.user_encoding)
 
392
                        )
 
393
        output_encoding = bzrlib.user_encoding
 
394
 
382
395
    return output_encoding
383
396
 
384
397
 
554
567
 
555
568
def local_time_offset(t=None):
556
569
    """Return offset of local zone from GMT, either at present or at time t."""
557
 
    # python2.3 localtime() can't take None
558
570
    if t is None:
559
571
        t = time.time()
560
 
        
561
 
    if time.localtime(t).tm_isdst and time.daylight:
562
 
        return -time.altzone
563
 
    else:
564
 
        return -time.timezone
 
572
    offset = datetime.fromtimestamp(t) - datetime.utcfromtimestamp(t)
 
573
    return offset.days * 86400 + offset.seconds
565
574
 
566
575
    
567
576
def format_date(t, offset=0, timezone='original', date_fmt=None, 
1059
1068
_cached_user_encoding = None
1060
1069
 
1061
1070
 
1062
 
def get_user_encoding():
 
1071
def get_user_encoding(use_cache=True):
1063
1072
    """Find out what the preferred user encoding is.
1064
1073
 
1065
1074
    This is generally the encoding that is used for command line parameters
1066
1075
    and file contents. This may be different from the terminal encoding
1067
1076
    or the filesystem encoding.
1068
1077
 
 
1078
    :param  use_cache:  Enable cache for detected encoding.
 
1079
                        (This parameter is turned on by default,
 
1080
                        and required only for selftesting)
 
1081
 
1069
1082
    :return: A string defining the preferred user encoding
1070
1083
    """
1071
1084
    global _cached_user_encoding
1072
 
    if _cached_user_encoding is not None:
 
1085
    if _cached_user_encoding is not None and use_cache:
1073
1086
        return _cached_user_encoding
1074
1087
 
1075
1088
    if sys.platform == 'darwin':
1083
1096
        import locale
1084
1097
 
1085
1098
    try:
1086
 
        _cached_user_encoding = locale.getpreferredencoding()
 
1099
        user_encoding = locale.getpreferredencoding()
1087
1100
    except locale.Error, e:
1088
1101
        sys.stderr.write('bzr: warning: %s\n'
1089
1102
                         '  Could not determine what text encoding to use.\n'
1091
1104
                         '  doesn\'t support the locale set by $LANG (%s)\n'
1092
1105
                         "  Continuing with ascii encoding.\n"
1093
1106
                         % (e, os.environ.get('LANG')))
 
1107
        user_encoding = 'ascii'
1094
1108
 
1095
1109
    # Windows returns 'cp0' to indicate there is no code page. So we'll just
1096
1110
    # treat that as ASCII, and not support printing unicode characters to the
1097
1111
    # console.
1098
 
    if _cached_user_encoding in (None, 'cp0'):
1099
 
        _cached_user_encoding = 'ascii'
1100
 
    return _cached_user_encoding
 
1112
    if user_encoding in (None, 'cp0'):
 
1113
        user_encoding = 'ascii'
 
1114
    else:
 
1115
        # check encoding
 
1116
        try:
 
1117
            codecs.lookup(user_encoding)
 
1118
        except LookupError:
 
1119
            sys.stderr.write('bzr: warning:'
 
1120
                             ' unknown encoding %s.'
 
1121
                             ' Continuing with ascii encoding.\n'
 
1122
                             % user_encoding
 
1123
                            )
 
1124
            user_encoding = 'ascii'
 
1125
 
 
1126
    if use_cache:
 
1127
        _cached_user_encoding = user_encoding
 
1128
 
 
1129
    return user_encoding
1101
1130
 
1102
1131
 
1103
1132
def recv_all(socket, bytes):