forked from BlakeBoxberger/DateUnderTimeX
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Functions.m
122 lines (100 loc) · 3.56 KB
/
Functions.m
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
#import "Functions.h"
#import <mach/mach.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <net/if.h>
@interface Functions()
@end
@implementation Functions
+ (NSNumber *)getFreeMemory {
@autoreleasepool {
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
vm_size_t pagesize;
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
NSLog(@"FreeRAMUnderTimeX: Failed to fetch vm statistics");
return @-1;
}
natural_t mem_free = (vm_stat.free_count + vm_stat.inactive_count) * pagesize;
NSNumber *freeMemory = [NSNumber numberWithUnsignedInt:round((mem_free / 1024) / 1024)];
return freeMemory;
}
}
+ (NSString *)getTime {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"HHmm" options:0 locale:[NSLocale currentLocale]]];
return [dateFormatter stringFromDate:[NSDate date]];
}
+ (NSString *)getDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"Md" options:0 locale:[NSLocale currentLocale]]];
return [dateFormatter stringFromDate:[NSDate date]];
}
+ (NSString *)getDayOfTheWeek {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"EEEE" options:0 locale:[NSLocale currentLocale]]];
return [dateFormatter stringFromDate:[NSDate date]];
}
+ (NSString *)getDateAndDayOfTheWeek {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"MdEEEEE" options:0 locale:[NSLocale currentLocale]]];
return [dateFormatter stringFromDate:[NSDate date]];
}
// + (long)getBytesTotal {
// @autoreleasepool {
// uint32_t iBytes = 0;
// uint32_t oBytes = 0;
// struct ifaddrs *ifa_list = NULL, *ifa;
// if ((getifaddrs(&ifa_list) < 0) || !ifa_list || ifa_list==0) {
// return 0;
// }
// for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
// if (ifa->ifa_addr == NULL) {
// continue;
// }
// if (AF_LINK != ifa->ifa_addr->sa_family) {
// continue;
// }
// if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING)) {
// continue;
// }
// if (ifa->ifa_data == NULL || ifa->ifa_data == 0) {
// continue;
// }
// struct if_data *if_data = (struct if_data *)ifa->ifa_data;
// iBytes += if_data->ifi_ibytes;
// oBytes += if_data->ifi_obytes;
// }
// if(ifa_list) {
// freeifaddrs(ifa_list);
// }
// return iBytes + oBytes;
// }
// }
// + (NSString *)getSpeed {
// @autoreleasepool {
// long nowData = [self getBytesTotal];
// if(!oldSpeed) {
// oldSpeed = nowData;
// }
// if(nowData<=0) {
// return @"";
// }
// long speed = nowData - oldSpeed;
// oldSpeed = nowData;
// if(speed < 1024) {
// return [NSString stringWithFormat:@"%ldB/s", speed];
// }
// if(speed >= 1024 && speed < 1024 * 1024) {
// return [NSString stringWithFormat:@"%.1fK/s", (double)speed / 1024];
// }
// if(speed >= 1024 * 1024 && speed < 1024 * 1024 * 1024) {
// return [NSString stringWithFormat:@"%.2fM/s", (double)speed / (1024 * 1024)];
// }
// return [NSString stringWithFormat:@"%.3fG/s", (double)speed / (1024 * 1024 * 1024)];
// }
// }
@end