Skip to content
API Documentation
BastionGuard™
TeeJee.Misc.vala
Go to the documentation of this file.
1
2/*
3 * TeeJee.Misc.vala
4 *
5 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 *
22 *
23 */
24
25namespace TeeJee.Misc {
26
27 /* Various utility functions */
28
29 using Gtk;
30 using TeeJee.Logging;
33
34 // localization --------------------
35
36 public void set_numeric_locale(string type){
37 Intl.setlocale(GLib.LocaleCategory.NUMERIC, type);
38 Intl.setlocale(GLib.LocaleCategory.COLLATE, type);
39 Intl.setlocale(GLib.LocaleCategory.TIME, type);
40 }
41
42 // timestamp ----------------
43
44 public string timestamp (bool show_millis = false){
45
46 /* Returns a formatted timestamp string */
47
48 // NOTE: format() does not support milliseconds
49
50 DateTime now = new GLib.DateTime.now_local();
51
52 if (show_millis){
53 var msec = now.get_microsecond () / 1000;
54 return "%s.%03d".printf(now.format("%H:%M:%S"), msec);
55 }
56 else{
57 return now.format ("%H:%M:%S");
58 }
59 }
60
61 public string timestamp_numeric(){
62
63 /* Returns a numeric timestamp string */
64
65 return "%ld".printf((long) time_t ());
66 }
67
68 public string timestamp_for_path(){
69
70 /* Returns a formatted timestamp string */
71
72 Time t = Time.local (time_t ());
73 return t.format ("%Y-%m-%d_%H-%M-%S");
74 }
75
76 // string formatting -------------------------------------------------
77
78 public string format_duration (double millis){
79
80 /* Converts time in milliseconds to format '00:00:00.0' */
81
82 double time = millis / 1000.0; // time in seconds
83
84 double hr = Math.floor(time / (60.0 * 60));
85 time = time - (hr * 60 * 60);
86 double min = Math.floor(time / 60.0);
87 time = time - (min * 60);
88 double sec = Math.floor(time);
89
90 return "%02.0lf:%02.0lf:%02.0lf".printf (hr, min, sec);
91 }
92
93 public string escape_html(string html){
94
95 return GLib.Markup.escape_text(html);
96 }
97
98 public string random_string(int length = 8, string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"){
99
100 string random = "";
101
102 for(int i=0;i<length;i++){
103 int random_index = Random.int_range(0,charset.length);
104 string ch = charset.get_char(charset.index_of_nth_char(random_index)).to_string();
105 random += ch;
106 }
107
108 return random;
109 }
110}
void set_numeric_locale(string type)
string timestamp_for_path()
string format_duration(double millis)
string timestamp_numeric()
string random_string(int length=8, string charset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")
string timestamp(bool show_millis=false)
string escape_html(string html)