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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#![cfg_attr(feature = "clippy", feature(plugin))]
#![cfg_attr(feature = "clippy", plugin(clippy))]
#[macro_use]
extern crate nom;
use nom::IResult::*;
use std::default::Default;
use std::str::FromStr;
#[macro_use]
mod helper;
pub mod parsers;
mod display;
#[derive(Eq,PartialEq,Debug,Copy,Clone)]
pub enum Date {
YMD {
year: i32,
month: u32,
day: u32,
},
Week {
year: i32,
ww: u32,
d: u32,
},
Ordinal {
year: i32,
ddd: u32,
},
}
#[derive(Eq,PartialEq,Debug,Copy,Clone,Default)]
pub struct Time {
pub hour: u32,
pub minute: u32,
pub second: u32,
pub millisecond: u32,
pub tz_offset_hours: i32,
pub tz_offset_minutes: i32,
}
#[derive(Eq,PartialEq,Debug,Copy,Clone,Default)]
pub struct DateTime {
pub date: Date,
pub time: Time,
}
impl Time {
pub fn set_tz(&self, tzo: (i32, i32)) -> Time {
let mut t = *self;
t.tz_offset_hours = tzo.0;
t.tz_offset_minutes = tzo.1;
t
}
}
impl Default for Date {
fn default() -> Date {
Date::YMD { year: 0, month: 0, day: 0 }
}
}
impl FromStr for Date {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
date(s)
}
}
impl FromStr for Time {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
time(s)
}
}
impl FromStr for DateTime {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
datetime(s)
}
}
pub fn date(string: &str) -> Result<Date, String> {
if let Done(_, parsed) = parsers::parse_date(string.as_bytes()) {
Ok(parsed)
} else {
Err(format!("Parser Error: {}", string))
}
}
pub fn time(string: &str) -> Result<Time, String> {
if let Done(_, parsed) = parsers::parse_time(string.as_bytes()) {
Ok(parsed)
} else {
Err(format!("Parser Error: {}", string))
}
}
pub fn datetime(string: &str) -> Result<DateTime, String> {
if let Done(_left_overs, parsed) = parsers::parse_datetime(string.as_bytes()) {
Ok(parsed)
} else {
Err(format!("Parser Error: {}", string))
}
}