Skip to content

Commit a18f0d4

Browse files
jesse99erickt
authored andcommitted
Made Tm_ a struct instead of a record and added serialization support to Tm and Tm_.
Not entirely clear what the best way to do this is. Right now we persist the entire struct which seems to be both portable and exactly round-trippable.
1 parent 7bc29c6 commit a18f0d4

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

src/libstd/time.rs

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use core::cmp::Eq;
44
use libc::{c_char, c_int, c_long, size_t, time_t};
55
use io::{Reader, ReaderUtil};
66
use result::{Result, Ok, Err};
7+
use serialization::{Serializable,
8+
Deserializable,
9+
Serializer,
10+
Deserializer};
711

812
#[abi = "cdecl"]
913
extern mod rustrt {
@@ -75,20 +79,60 @@ pub fn tzset() {
7579
rustrt::rust_tzset();
7680
}
7781

78-
type Tm_ = {
79-
tm_sec: i32, // seconds after the minute ~[0-60]
80-
tm_min: i32, // minutes after the hour ~[0-59]
81-
tm_hour: i32, // hours after midnight ~[0-23]
82-
tm_mday: i32, // days of the month ~[1-31]
83-
tm_mon: i32, // months since January ~[0-11]
84-
tm_year: i32, // years since 1900
85-
tm_wday: i32, // days since Sunday ~[0-6]
86-
tm_yday: i32, // days since January 1 ~[0-365]
87-
tm_isdst: i32, // Daylight Savings Time flag
88-
tm_gmtoff: i32, // offset from UTC in seconds
89-
tm_zone: ~str, // timezone abbreviation
90-
tm_nsec: i32, // nanoseconds
91-
};
82+
pub struct Tm_ {
83+
pub tm_sec: i32, // seconds after the minute ~[0-60]
84+
pub tm_min: i32, // minutes after the hour ~[0-59]
85+
pub tm_hour: i32, // hours after midnight ~[0-23]
86+
pub tm_mday: i32, // days of the month ~[1-31]
87+
pub tm_mon: i32, // months since January ~[0-11]
88+
pub tm_year: i32, // years since 1900
89+
pub tm_wday: i32, // days since Sunday ~[0-6]
90+
pub tm_yday: i32, // days since January 1 ~[0-365]
91+
pub tm_isdst: i32, // Daylight Savings Time flag
92+
pub tm_gmtoff: i32, // offset from UTC in seconds
93+
pub tm_zone: ~str, // timezone abbreviation
94+
pub tm_nsec: i32, // nanoseconds
95+
}
96+
97+
impl<S: Serializer> Tm_: Serializable<S> {
98+
fn serialize(&self, s: &S) {
99+
s.emit_i32(self.tm_sec);
100+
s.emit_i32(self.tm_min);
101+
s.emit_i32(self.tm_hour);
102+
s.emit_i32(self.tm_mday);
103+
s.emit_i32(self.tm_mon);
104+
s.emit_i32(self.tm_year);
105+
s.emit_i32(self.tm_wday);
106+
s.emit_i32(self.tm_yday);
107+
s.emit_i32(self.tm_isdst);
108+
s.emit_i32(self.tm_gmtoff);
109+
s.emit_owned_str(self.tm_zone);
110+
s.emit_i32(self.tm_nsec);
111+
}
112+
}
113+
114+
pub fn deserialize_tm_<D: Deserializer>(d: &D) -> Tm_ {
115+
Tm_ {
116+
tm_sec: d.read_i32(),
117+
tm_min: d.read_i32(),
118+
tm_hour: d.read_i32(),
119+
tm_mday: d.read_i32(),
120+
tm_mon: d.read_i32(),
121+
tm_year: d.read_i32(),
122+
tm_wday: d.read_i32(),
123+
tm_yday: d.read_i32(),
124+
tm_isdst: d.read_i32(),
125+
tm_gmtoff: d.read_i32(),
126+
tm_zone: d.read_owned_str(),
127+
tm_nsec: d.read_i32(),
128+
}
129+
}
130+
131+
impl<D: Deserializer> Tm_: Deserializable<D> {
132+
static fn deserialize(d: &D) -> Tm_ {
133+
deserialize_tm_(d)
134+
}
135+
}
92136

93137
impl Tm_ : Eq {
94138
#[cfg(stage0)]
@@ -133,6 +177,23 @@ pub enum Tm {
133177
Tm_(Tm_)
134178
}
135179

180+
impl<S: Serializer> Tm: Serializable<S> {
181+
fn serialize(&self, s: &S) {
182+
let t: Tm_ = **self;
183+
t.serialize(s);
184+
}
185+
}
186+
187+
pub fn deserialize_tm<D: Deserializer>(d: &D) -> Tm {
188+
Tm_(deserialize_tm_(d))
189+
}
190+
191+
impl<D: Deserializer> Tm: Deserializable<D> {
192+
static fn deserialize(d: &D) -> Tm {
193+
deserialize_tm(d)
194+
}
195+
}
196+
136197
impl Tm : Eq {
137198
#[cfg(stage0)]
138199
pure fn eq(other: &Tm) -> bool { *self == *(*other) }
@@ -147,7 +208,7 @@ impl Tm : Eq {
147208
}
148209

149210
pub pure fn empty_tm() -> Tm {
150-
Tm_({
211+
Tm_(Tm_{
151212
tm_sec: 0_i32,
152213
tm_min: 0_i32,
153214
tm_hour: 0_i32,
@@ -652,7 +713,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
652713
}
653714

654715
do io::with_str_reader(str::from_slice(format)) |rdr| {
655-
let mut tm = {
716+
let mut tm = Tm_ {
656717
tm_sec: 0_i32,
657718
tm_min: 0_i32,
658719
tm_hour: 0_i32,
@@ -686,7 +747,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
686747
}
687748

688749
if pos == len && rdr.eof() {
689-
Ok(Tm_({
750+
Ok(Tm_(Tm_ {
690751
tm_sec: tm.tm_sec,
691752
tm_min: tm.tm_min,
692753
tm_hour: tm.tm_hour,

src/test/run-pass/auto_serialize.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use EBWriter = std::ebml::Writer;
1010
use io::Writer;
1111
use std::serialization::{Serializable, Deserializable, deserialize};
1212
use std::prettyprint;
13+
use std::time;
1314

1415
fn test_prettyprint<A: Serializable<prettyprint::Serializer>>(
1516
a: &A,
@@ -184,4 +185,7 @@ fn main() {
184185
let a = &B;
185186
test_prettyprint(a, &~"B");
186187
test_ebml(a);
188+
189+
let a = &time::now();
190+
test_ebml(a);
187191
}

0 commit comments

Comments
 (0)