Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add string_to_seconds(str)

  • Loading branch information...
commit d9fbc4f22eb651b17ae91ee1ffabb26821b5651d 1 parent ebcdca6
@tj tj authored
Showing with 33 additions and 16 deletions.
  1. +6 −5 Readme.md
  2. +27 −4 ms.c
  3. +0 −7 ms.h
View
11 Readme.md
@@ -2,19 +2,20 @@
# ms.c
```c
-#define strtous string_to_microseconds
-#define strtoms string_to_milliseconds
-#define mstostr milliseconds_to_string
-#define mstolstr milliseconds_to_long_string
-
long long
string_to_microseconds(const char *str);
long long
string_to_milliseconds(const char *str);
+long long
+string_to_seconds(const char *str);
+
char *
milliseconds_to_string(long long ms);
+
+char *
+milliseconds_to_long_string(long long ms);
```
For usage view the [tests](https://github.com/visionmedia/ms.c/blob/master/ms.c#L52)
View
31 ms.c
@@ -60,6 +60,17 @@ string_to_milliseconds(const char *str) {
}
/*
+ * Convert the given `str` representation to seconds.
+ */
+
+long long
+string_to_seconds(const char *str) {
+ long long ret = string_to_milliseconds(str);
+ if (-1 == ret) return ret;
+ return ret / 1000;
+}
+
+/*
* Convert the given `ms` to a string. This
* value must be `free()`d by the developer.
*/
@@ -145,7 +156,6 @@ test_string_to_microseconds() {
assert(string_to_microseconds("1m") == 60000000);
assert(string_to_microseconds("1h") == 3600000000);
assert(string_to_microseconds("2d") == 2 * 24 * 3600000000);
- assert(strtous("1ms") == 1000);
}
void
@@ -161,12 +171,25 @@ test_string_to_milliseconds() {
assert(string_to_milliseconds("1m") == 60 * 1000);
assert(string_to_milliseconds("1h") == 60 * 60 * 1000);
assert(string_to_milliseconds("1d") == 24 * 60 * 60 * 1000);
- assert(strtoms("50s") == 50000);
+}
+
+void
+test_string_to_seconds() {
+ assert(string_to_seconds("") == -1);
+ assert(string_to_seconds("s") == -1);
+ assert(string_to_seconds("hey") == -1);
+ assert(string_to_seconds("5000") == 5);
+ assert(string_to_seconds("1ms") == 0);
+ assert(string_to_seconds("5ms") == 0);
+ assert(string_to_seconds("1s") == 1);
+ assert(string_to_seconds("5s") == 5);
+ assert(string_to_seconds("1m") == 60);
+ assert(string_to_seconds("1h") == 60 * 60);
+ assert(string_to_seconds("1d") == 24 * 60 * 60);
}
void
test_milliseconds_to_string() {
- equal("500ms", mstostr(500));
equal("500ms", milliseconds_to_string(500));
equal("5s", milliseconds_to_string(5000));
equal("2s", milliseconds_to_string(2500));
@@ -180,7 +203,6 @@ test_milliseconds_to_string() {
void
test_milliseconds_to_long_string() {
- equal("less than one second", mstolstr(500));
equal("less than one second", milliseconds_to_long_string(500));
equal("5 seconds", milliseconds_to_long_string(5000));
equal("2 seconds", milliseconds_to_long_string(2500));
@@ -197,6 +219,7 @@ int
main(){
test_string_to_microseconds();
test_string_to_milliseconds();
+ test_string_to_seconds();
test_milliseconds_to_string();
test_milliseconds_to_long_string();
printf("\n \e[32m\u2713 \e[90mok\e[0m\n\n");
View
7 ms.h
@@ -14,13 +14,6 @@
#define MS_MAX 256
#endif
-// aliases
-
-#define strtous string_to_microseconds
-#define strtoms string_to_milliseconds
-#define mstostr milliseconds_to_string
-#define mstolstr milliseconds_to_long_string
-
// prototypes
long long
Please sign in to comment.
Something went wrong with that request. Please try again.