4 from datetime import datetime
7 # Takes a timestamp and puts out a string with the approxomation of the age
8 def age(from_date, since_date = None, target_tz=None, include_seconds=False):
12 from_date = datetime.fromtimestamp(from_date)
13 if since_date is None:
14 since_date = datetime.now(target_tz)
16 distance_in_time = since_date - from_date
17 distance_in_seconds = int(round(abs(distance_in_time.days * 86400 + distance_in_time.seconds)))
18 distance_in_minutes = int(round(distance_in_seconds/60))
21 if distance_in_minutes <= 1:
23 for remainder in [5, 10, 20]:
24 if distance_in_seconds < remainder:
25 return "less than %s seconds ago" % remainder
26 if distance_in_seconds < 40:
27 return "half a minute"
28 elif distance_in_seconds < 60:
29 return "less than a minute ago"
33 if distance_in_minutes == 0:
34 return "less than a minute ago"
37 elif distance_in_minutes < 45:
38 return "%s minutes ago" % distance_in_minutes
39 elif distance_in_minutes < 90:
40 return "about 1 hour ago"
41 elif distance_in_minutes < 1440:
42 return "about %d hours ago" % (round(distance_in_minutes / 60.0))
43 elif distance_in_minutes < 2880:
45 elif distance_in_minutes < 43220:
46 return "%d days ago" % (round(distance_in_minutes / 1440))
47 elif distance_in_minutes < 86400:
48 return "about 1 month ago"
49 elif distance_in_minutes < 525600:
50 return "%d months ago" % (round(distance_in_minutes / 43200))
51 elif distance_in_minutes < 1051200:
52 return "about 1 year ago"
54 return "over %d years ago" % (round(distance_in_minutes / 525600))
60 def print_error(*args):
61 if not is_verbose: return
62 args = [str(item) for item in args]
63 sys.stderr.write(" ".join(args) + "\n")
68 args = [str(item) for item in args]
69 sys.stdout.write(" ".join(args) + "\n")
72 def check_windows_wallet_migration():
73 from PyQt4.QtGui import *
76 app = QApplication(sys.argv)
77 if os.path.exists(os.path.join(os.environ["LOCALAPPDATA"], "Electrum")):
78 if os.path.exists(os.path.join(os.environ["APPDATA"], "Electrum")):
79 QMessageBox.information(None,_("Folder migration"), _("Two Electrum folders have been found, the default Electrum location for Windows has changed from %s to %s since Electrum 1.7, please check your wallets and fix the problem manually." % (os.environ["LOCALAPPDATA"], os.environ["APPDATA"])))
82 QMessageBox.information(None, _("Folder migration"), _("This version of Electrum moved the Electrum folder on windows from %s to %s, your Electrum folder will now be moved.") % (os.environ["LOCALAPPDATA"], os.environ["APPDATA"]))
83 shutil.move(os.path.join(os.environ["LOCALAPPDATA"], "Electrum"), os.path.join(os.environ["APPDATA"], "Electrum"))
84 QMessageBox.information(None,_("Migration status"), _("Your wallet has been moved sucessfully."))
89 if "HOME" in os.environ:
90 return os.path.join(os.environ["HOME"], ".electrum")
91 elif "APPDATA" in os.environ:
92 return os.path.join(os.environ["APPDATA"], "Electrum")
93 elif "LOCALAPPDATA" in os.environ:
94 return os.path.join(os.environ["LOCALAPPDATA"], "Electrum")
96 #raise BaseException("No home directory found in environment variables.")
100 """Find the path to the application data directory; add an electrum folder and return path."""
101 if platform.system() == "Windows":
102 return os.path.join(os.environ["APPDATA"], "Electrum")
103 elif platform.system() == "Linux":
104 return os.path.join(sys.prefix, "share", "electrum")
105 elif (platform.system() == "Darwin" or
106 platform.system() == "DragonFly" or
107 platform.system() == "NetBSD"):
108 return "/Library/Application Support/Electrum"
110 raise Exception("Unknown system")
113 def get_resource_path(*args):
114 return os.path.join(".", *args)
117 def local_data_dir():
118 """Return path to the data folder."""
120 prefix_path = os.path.dirname(sys.argv[0])
121 local_data = os.path.join(prefix_path, "data")
125 def format_satoshis(x, is_diff=False, num_zeros = 0):
126 from decimal import Decimal
128 sign, digits, exp = s.as_tuple()
129 digits = map(str, digits)
130 while len(digits) < 9:
132 digits.insert(-8,'.')
133 s = ''.join(digits).rstrip('0')
140 s += "0"*( 1 + num_zeros - ( len(s) - p ))
141 s += " "*( 9 - ( len(s) - p ))
142 s = " "*( 5 - ( p )) + s