@@ -10,11 +10,11 @@ use serde_json::Value;
1010use std:: fmt:: Display ;
1111use std:: fmt:: Formatter ;
1212use std:: time:: Duration ;
13+ use std:: time:: SystemTime ;
1314use tedge_mqtt_ext:: MqttMessage ;
1415use tedge_mqtt_ext:: Topic ;
1516use tedge_mqtt_ext:: TopicFilter ;
1617use tedge_watch_ext:: WatchError ;
17- use time:: OffsetDateTime ;
1818use tokio:: time:: Instant ;
1919use tracing:: error;
2020use tracing:: warn;
@@ -112,17 +112,11 @@ impl FlowResult {
112112 }
113113}
114114
115- #[ derive( Copy , Clone , Debug , serde:: Deserialize , serde:: Serialize , Eq , PartialEq ) ]
116- pub struct DateTime {
117- pub seconds : u64 ,
118- pub nanoseconds : u32 ,
119- }
120-
121115#[ derive( Clone , serde:: Deserialize , serde:: Serialize , Eq , PartialEq ) ]
122116pub struct Message {
123117 pub topic : String ,
124118 pub payload : Vec < u8 > ,
125- pub timestamp : Option < DateTime > ,
119+ pub timestamp : Option < SystemTime > ,
126120}
127121
128122#[ derive( thiserror:: Error , Debug ) ]
@@ -198,7 +192,7 @@ impl Flow {
198192 & mut self ,
199193 js_runtime : & JsRuntime ,
200194 stats : & mut Counter ,
201- timestamp : DateTime ,
195+ timestamp : SystemTime ,
202196 message : & Message ,
203197 ) -> FlowResult {
204198 let stated_at = stats. flow_on_message_start ( self . name ( ) ) ;
@@ -218,7 +212,7 @@ impl Flow {
218212 & mut self ,
219213 js_runtime : & JsRuntime ,
220214 stats : & mut Counter ,
221- timestamp : DateTime ,
215+ timestamp : SystemTime ,
222216 message : & Message ,
223217 ) -> Result < Vec < Message > , FlowError > {
224218 let mut messages = vec ! [ message. clone( ) ] ;
@@ -246,7 +240,7 @@ impl Flow {
246240 & mut self ,
247241 js_runtime : & JsRuntime ,
248242 stats : & mut Counter ,
249- timestamp : DateTime ,
243+ timestamp : SystemTime ,
250244 now : Instant ,
251245 ) -> FlowResult {
252246 let stated_at = stats. flow_on_interval_start ( self . name ( ) ) ;
@@ -266,7 +260,7 @@ impl Flow {
266260 & mut self ,
267261 js_runtime : & JsRuntime ,
268262 stats : & mut Counter ,
269- timestamp : DateTime ,
263+ timestamp : SystemTime ,
270264 now : Instant ,
271265 ) -> Result < Vec < Message > , FlowError > {
272266 let mut messages = vec ! [ ] ;
@@ -399,36 +393,6 @@ impl FlowStep {
399393 }
400394}
401395
402- impl DateTime {
403- pub fn now ( ) -> Self {
404- DateTime :: try_from ( OffsetDateTime :: now_utc ( ) ) . unwrap ( )
405- }
406-
407- pub fn tick_now ( & self , tick_every : std:: time:: Duration ) -> bool {
408- let tick_every_secs = tick_every. as_secs ( ) ;
409- tick_every_secs != 0 && ( self . seconds % tick_every_secs == 0 )
410- }
411-
412- pub fn json ( & self ) -> Value {
413- json ! ( { "seconds" : self . seconds, "nanoseconds" : self . nanoseconds} )
414- }
415- }
416-
417- impl TryFrom < OffsetDateTime > for DateTime {
418- type Error = FlowError ;
419-
420- fn try_from ( value : OffsetDateTime ) -> Result < Self , Self :: Error > {
421- let seconds = u64:: try_from ( value. unix_timestamp ( ) ) . map_err ( |err| {
422- FlowError :: UnsupportedMessage ( format ! ( "failed to convert timestamp: {}" , err) )
423- } ) ?;
424-
425- Ok ( DateTime {
426- seconds,
427- nanoseconds : value. nanosecond ( ) ,
428- } )
429- }
430- }
431-
432396impl Message {
433397 pub fn new ( topic : impl ToString , payload : impl Into < Vec < u8 > > ) -> Self {
434398 Message {
@@ -441,7 +405,7 @@ impl Message {
441405 pub fn with_timestamp (
442406 topic : impl ToString ,
443407 payload : impl Into < Vec < u8 > > ,
444- timestamp : DateTime ,
408+ timestamp : SystemTime ,
445409 ) -> Self {
446410 Message {
447411 topic : topic. to_string ( ) ,
@@ -450,15 +414,9 @@ impl Message {
450414 }
451415 }
452416
453- #[ cfg( test) ]
454- pub fn sent_now ( mut self ) -> Self {
455- self . timestamp = Some ( DateTime :: now ( ) ) ;
456- self
457- }
458-
459417 pub fn json ( & self ) -> Value {
460418 if let Some ( timestamp) = & self . timestamp {
461- json ! ( { "topic" : self . topic, "payload" : self . payload, "timestamp" : timestamp . json ( ) } )
419+ json ! ( { "topic" : self . topic, "payload" : self . payload, "timestamp" : epoch_ms ( timestamp ) } )
462420 } else {
463421 json ! ( { "topic" : self . topic, "payload" : self . payload, "timestamp" : null} )
464422 }
@@ -508,6 +466,13 @@ impl TryFrom<Message> for MqttMessage {
508466 }
509467}
510468
469+ pub ( crate ) fn epoch_ms ( time : & SystemTime ) -> u128 {
470+ let duration = time
471+ . duration_since ( SystemTime :: UNIX_EPOCH )
472+ . expect ( "SystemTime after UNIX EPOCH" ) ;
473+ duration. as_millis ( )
474+ }
475+
511476pub fn error_from_js ( err : LoadError ) -> FlowError {
512477 FlowError :: IncorrectSetting ( format ! ( "{err:#}" ) )
513478}
0 commit comments