1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | // // Kerbal Engineer Redux // // Copyright (C) 2014 CYBUTEK // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. // #region Using Directives using System; using KerbalEngineer.Settings; #endregion namespace KerbalEngineer.Helpers { public static class TimeFormatter { #region Constructors static TimeFormatter() { SetReference(false); Load(); } #endregion #region Properties public static string Reference { get; set; } public static double SecondsPerDay { get; set; } public static double SecondsPerHour { get; set; } public static double SecondsPerMinute { get; set; } public static double SecondsPerYear { get; set; } #endregion #region Methods: public public static string ConvertToString(double seconds, string format = "F1") { var years = 0; var days = 0; var hours = 0; var minutes = 0; if (seconds > 0) { years = (int)(seconds / SecondsPerYear); seconds -= years * SecondsPerYear; days = (int)(seconds / SecondsPerDay); seconds -= days * SecondsPerDay; hours = (int)(seconds / SecondsPerHour); seconds -= hours * SecondsPerHour; minutes = (int)(seconds / SecondsPerMinute); seconds -= minutes * SecondsPerMinute; } if (years > 0) { return String.Format("{0}y {1}d {2}h {3}m {4}s", years, days, hours, minutes, seconds.ToString(format)); } if (days > 0) { return String.Format("{0}d {1}h {2}m {3}s", days, hours, minutes, seconds.ToString(format)); } if (hours > 0) { return String.Format("{0}h {1}m {2}s", hours, minutes, seconds.ToString(format)); } return minutes > 0 ? String.Format("{0}m {1}s", minutes, seconds.ToString(format)) : String.Format("{0}s", seconds.ToString(format)); } public static void Load() { var handler = SettingHandler.Load("TimeFormatter.xml"); SecondsPerMinute = handler.Get("SecondsPerMinute", SecondsPerMinute); SecondsPerHour = handler.Get("SecondsPerHour", SecondsPerHour); SecondsPerDay = handler.Get("SecondsPerDay", SecondsPerDay); SecondsPerYear = handler.Get("SecondsPerYear", SecondsPerYear); Reference = handler.Get("Reference", Reference); } public static void Save() { var handler = SettingHandler.Load("TimeFormatter.xml"); handler.Set("SecondsPerMinute", SecondsPerMinute); handler.Set("SecondsPerHour", SecondsPerHour); handler.Set("SecondsPerDay", SecondsPerDay); handler.Set("SecondsPerYear", SecondsPerYear); handler.Set("Reference", Reference); handler.Save("TimeFormatter.xml"); } public static void SetReference(bool save = true) { const double minute = 60.0; const double hour = minute * 60.0; const double day = hour * 24.0; const double year = day * 365.0; SetReference(minute, hour, day, year, "Earth", save); } public static void SetReference(CelestialBody body, bool save = true) { SetReference(SecondsPerMinute, SecondsPerHour, body.rotationPeriod, body.orbit.period, body.bodyName, save); } public static void SetReference(double minute, double hour, double day, double year, string reference, bool save = true) { SecondsPerMinute = minute; SecondsPerHour = hour; SecondsPerDay = day; SecondsPerYear = year; Reference = reference; if (save) { Save(); } } public new static string ToString() { return String.Format("SecondsPerMinute: {0}", SecondsPerMinute) + Environment.NewLine + String.Format("SecondsPerHour: {0}", SecondsPerHour) + Environment.NewLine + String.Format("SecondsPerDay: {0}", SecondsPerDay) + Environment.NewLine + String.Format("SecondsPerYear: {0}", SecondsPerYear) + Environment.NewLine; } #endregion } } |