1 |
|
---|
2 | PARSE_TIME(3) BSD Library Functions Manual PARSE_TIME(3)
|
---|
3 |
|
---|
4 | NNAAMMEE
|
---|
5 | ppaarrssee__ttiimmee, pprriinntt__ttiimmee__ttaabbllee, uunnppaarrssee__ttiimmee, uunnppaarrssee__ttiimmee__aapppprrooxx, -- parse
|
---|
6 | and unparse time intervals
|
---|
7 |
|
---|
8 | LLIIBBRRAARRYY
|
---|
9 | The roken library (libroken, -lroken)
|
---|
10 |
|
---|
11 | SSYYNNOOPPSSIISS
|
---|
12 | ##iinncclluuddee <<ppaarrssee__ttiimmee..hh>>
|
---|
13 |
|
---|
14 | _i_n_t
|
---|
15 | ppaarrssee__ttiimmee(_c_o_n_s_t _c_h_a_r _*_t_i_m_e_s_p_e_c, _c_o_n_s_t _c_h_a_r _*_d_e_f___u_n_i_t);
|
---|
16 |
|
---|
17 | _v_o_i_d
|
---|
18 | pprriinntt__ttiimmee__ttaabbllee(_F_I_L_E _*_f);
|
---|
19 |
|
---|
20 | _s_i_z_e___t
|
---|
21 | uunnppaarrssee__ttiimmee(_i_n_t _s_e_c_o_n_d_s, _c_h_a_r _*_b_u_f, _s_i_z_e___t _l_e_n);
|
---|
22 |
|
---|
23 | _s_i_z_e___t
|
---|
24 | uunnppaarrssee__ttiimmee__aapppprrooxx(_i_n_t _s_e_c_o_n_d_s, _c_h_a_r _*_b_u_f, _s_i_z_e___t _l_e_n);
|
---|
25 |
|
---|
26 | DDEESSCCRRIIPPTTIIOONN
|
---|
27 | The ppaarrssee__ttiimmee() function converts a the period of time specified in into
|
---|
28 | a number of seconds. The _t_i_m_e_s_p_e_c can be any number of <number unit>
|
---|
29 | pairs separated by comma and whitespace. The number can be negative. Num-
|
---|
30 | ber without explicit units are taken as being _d_e_f___u_n_i_t.
|
---|
31 |
|
---|
32 | The uunnppaarrssee__ttiimmee() and uunnppaarrssee__ttiimmee__aapppprrooxx() does the opposite of
|
---|
33 | ppaarrssee__ttiimmee(), that is they take a number of seconds and express that as
|
---|
34 | human readable string. _u_n_p_a_r_s_e___t_i_m_e produces an exact time, while
|
---|
35 | _u_n_p_a_r_s_e___t_i_m_e___a_p_p_r_o_x restricts the result to only include one units.
|
---|
36 |
|
---|
37 | pprriinntt__ttiimmee__ttaabbllee() prints a descriptive list of available units on the
|
---|
38 | passed file descriptor.
|
---|
39 |
|
---|
40 | The possible units include:
|
---|
41 | second, s
|
---|
42 | minute, m
|
---|
43 | hour, h
|
---|
44 | day
|
---|
45 | week seven days
|
---|
46 | month 30 days
|
---|
47 | year 365 days
|
---|
48 |
|
---|
49 | Units names can be arbitrarily abbreviated (as long as they are unique).
|
---|
50 |
|
---|
51 | RREETTUURRNN VVAALLUUEESS
|
---|
52 | ppaarrssee__ttiimmee() returns the number of seconds that represents the expression
|
---|
53 | in _t_i_m_e_s_p_e_c or -1 on error. uunnppaarrssee__ttiimmee() and uunnppaarrssee__ttiimmee__aapppprrooxx()
|
---|
54 | return the number of characters written to _b_u_f. if the return value is
|
---|
55 | greater than or equal to the _l_e_n argument, the string was too short and
|
---|
56 | some of the printed characters were discarded.
|
---|
57 |
|
---|
58 | EEXXAAMMPPLLEESS
|
---|
59 | #include <stdio.h>
|
---|
60 | #include <parse_time.h>
|
---|
61 |
|
---|
62 | int
|
---|
63 | main(int argc, char **argv)
|
---|
64 | {
|
---|
65 | int i;
|
---|
66 | int result;
|
---|
67 | char buf[128];
|
---|
68 | print_time_table(stdout);
|
---|
69 | for (i = 1; i < argc; i++) {
|
---|
70 | result = parse_time(argv[i], "second");
|
---|
71 | if(result == -1) {
|
---|
72 | fprintf(stderr, "%s: parse error\n", argv[i]);
|
---|
73 | continue;
|
---|
74 | }
|
---|
75 | printf("--\n");
|
---|
76 | printf("parse_time = %d\n", result);
|
---|
77 | unparse_time(result, buf, sizeof(buf));
|
---|
78 | printf("unparse_time = %s\n", buf);
|
---|
79 | unparse_time_approx(result, buf, sizeof(buf));
|
---|
80 | printf("unparse_time_approx = %s\n", buf);
|
---|
81 | }
|
---|
82 | return 0;
|
---|
83 | }
|
---|
84 |
|
---|
85 | $ ./a.out "1 minute 30 seconds" "90 s" "1 y -1 s"
|
---|
86 | 1 year = 365 days
|
---|
87 | 1 month = 30 days
|
---|
88 | 1 week = 7 days
|
---|
89 | 1 day = 24 hours
|
---|
90 | 1 hour = 60 minutes
|
---|
91 | 1 minute = 60 seconds
|
---|
92 | 1 second
|
---|
93 | --
|
---|
94 | parse_time = 90
|
---|
95 | unparse_time = 1 minute 30 seconds
|
---|
96 | unparse_time_approx = 1 minute
|
---|
97 | --
|
---|
98 | parse_time = 90
|
---|
99 | unparse_time = 1 minute 30 seconds
|
---|
100 | unparse_time_approx = 1 minute
|
---|
101 | --
|
---|
102 | parse_time = 31535999
|
---|
103 | unparse_time = 12 months 4 days 23 hours 59 minutes 59 seconds
|
---|
104 | unparse_time_approx = 12 months
|
---|
105 |
|
---|
106 | BBUUGGSS
|
---|
107 | Since ppaarrssee__ttiimmee() returns -1 on error there is no way to parse "minus
|
---|
108 | one second". Currently "s" at the end of units is ignored. This is a
|
---|
109 | hack for English plural forms. If these functions are ever localised,
|
---|
110 | this scheme will have to change.
|
---|
111 |
|
---|
112 | HEIMDAL October 31, 2004 HEIMDAL
|
---|