time.rs (1253B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 //! Computed time values. 6 7 use crate::derives::*; 8 use crate::values::CSSFloat; 9 use crate::Zero; 10 use std::fmt::{self, Write}; 11 use style_traits::{CssWriter, ToCss}; 12 13 /// A computed `<time>` value. 14 #[derive(Animate, Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue)] 15 #[cfg_attr(feature = "servo", derive(Deserialize, Serialize))] 16 #[repr(C)] 17 pub struct Time { 18 seconds: CSSFloat, 19 } 20 21 impl Time { 22 /// Creates a time value from a seconds amount. 23 pub fn from_seconds(seconds: CSSFloat) -> Self { 24 Time { seconds } 25 } 26 27 /// Returns the amount of seconds this time represents. 28 #[inline] 29 pub fn seconds(&self) -> CSSFloat { 30 self.seconds 31 } 32 } 33 34 impl ToCss for Time { 35 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result 36 where 37 W: Write, 38 { 39 self.seconds().to_css(dest)?; 40 dest.write_char('s') 41 } 42 } 43 44 impl Zero for Time { 45 fn zero() -> Self { 46 Self::from_seconds(0.0) 47 } 48 49 fn is_zero(&self) -> bool { 50 self.seconds == 0. 51 } 52 }