comparison mercurial/util.py @ 2522:85f796baab10

Allow the use of human readable dates (issue 251)
author Jose M. Prieto <jmprieto@gmx.net>
date Fri, 30 Jun 2006 18:47:35 +0200
parents 519a1011db91
children 4ab59a3acd16
comparison
equal deleted inserted replaced
2521:9cceb439048b 2522:85f796baab10
857 s = time.strftime(format, time.gmtime(float(t) - tz)) 857 s = time.strftime(format, time.gmtime(float(t) - tz))
858 if timezone: 858 if timezone:
859 s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60)) 859 s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60))
860 return s 860 return s
861 861
862 def strdate(string, format='%a %b %d %H:%M:%S %Y'):
863 """parse a localized time string and return a (unixtime, offset) tuple.
864 if the string cannot be parsed, ValueError is raised."""
865 def hastimezone(string):
866 return (string[-4:].isdigit() and
867 (string[-5] == '+' or string[-5] == '-') and
868 string[-6].isspace())
869
870 if hastimezone(string):
871 date, tz = string.rsplit(None, 1)
872 tz = int(tz)
873 offset = - 3600 * (tz / 100) - 60 * (tz % 100)
874 else:
875 date, offset = string, 0
876 when = int(time.mktime(time.strptime(date, format))) + offset
877 return when, offset
878
879 def parsedate(string, formats=('%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M')):
880 """parse a localized time string and return a (unixtime, offset) tuple.
881 The date may be a "unixtime offset" string or in one of the specified
882 formats."""
883 try:
884 when, offset = map(int, string.split(' '))
885 return when, offset
886 except ValueError: pass
887 for format in formats:
888 try:
889 return strdate(string, format)
890 except ValueError: pass
891 raise ValueError(_('invalid date: %r') % string)
892
862 def shortuser(user): 893 def shortuser(user):
863 """Return a short representation of a user name or email address.""" 894 """Return a short representation of a user name or email address."""
864 f = user.find('@') 895 f = user.find('@')
865 if f >= 0: 896 if f >= 0:
866 user = user[:f] 897 user = user[:f]