Guest User

Untitled

a guest
Dec 10th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. extern crate chrono; // 0.4.6
  2. use chrono::TimeZone;
  3. use chrono::Datelike;
  4. use chrono::{DateTime, Utc, Weekday, Duration};
  5.  
  6.  
  7. fn string_to_datetime(s: String) -> DateTime<Utc> {
  8. let now = Utc::now();
  9. match s.as_ref() {
  10. "this_week" => {
  11. let this_week = Utc.isoywd(now.iso_week().year(), now.iso_week().week(), Weekday::Fri).and_hms(8, 0, 0);
  12. if this_week <= now { Utc.isoywd(now.iso_week().year(), now.iso_week().week() + 1, Weekday::Fri).and_hms(8, 0, 0) } else { this_week }
  13. },
  14. "next_week" => {
  15. let this_week = Utc.isoywd(now.iso_week().year(), now.iso_week().week(), Weekday::Fri).and_hms(8, 0, 0);
  16. if this_week <= now {
  17. Utc.isoywd(now.iso_week().year(), now.iso_week().week() + 2, Weekday::Fri).and_hms(8, 0, 0)
  18. } else {
  19. Utc.isoywd(now.iso_week().year(), now.iso_week().week() + 1, Weekday::Fri).and_hms(8, 0, 0)
  20. }
  21. },
  22. "quarter" => {
  23. let now = Utc::now();
  24. match now {
  25. x if x < Utc.ymd(2018, 12, 28).and_hms(8, 0, 0) => Utc.ymd(2018, 12, 28).and_hms(8, 0, 0),
  26. x if x < Utc.ymd(2019, 03, 29).and_hms(8, 0, 0) => Utc.ymd(2019, 03, 29).and_hms(8, 0, 0),
  27. x if x < Utc.ymd(2019, 06, 28).and_hms(8, 0, 0) => Utc.ymd(2019, 06, 28).and_hms(8, 0, 0),
  28. _ => panic!("Need to update hardcoded quarterly futures for okex.")
  29. }
  30. },
  31. _ => panic!("Unrecognized futures string.")
  32. }
  33. }
  34.  
  35.  
  36. fn datetime_to_string(d: DateTime<Utc>) -> String {
  37. let now = Utc::now();
  38. if d < now + Duration::weeks(1) {
  39. "this_week".to_owned()
  40. } else if d < now + Duration::weeks(2) {
  41. "next_week".to_owned()
  42. } else {
  43. "quarter".to_owned()
  44. }
  45. }
  46.  
  47. fn main() {
  48. let d1 = Utc.ymd(2018, 12, 7).and_hms(8, 0, 0);
  49. let d2 = Utc.ymd(2018, 12, 14).and_hms(8, 0, 0);
  50. let d3 = Utc.ymd(2018, 12, 28).and_hms(8, 0, 0);
  51. assert_eq!(datetime_to_string(d1), "this_week");
  52. assert_eq!(datetime_to_string(d2), "next_week");
  53. assert_eq!(datetime_to_string(d3), "quarter");
  54. assert_eq!(string_to_datetime("this_week".to_string()), d1);
  55. assert_eq!(string_to_datetime("next_week".to_string()), d2);
  56. assert_eq!(string_to_datetime("quarter".to_string()), d3);
  57. }
Add Comment
Please, Sign In to add comment