@@ -17,54 +17,51 @@ fn main() {
1717 println ! ( ) ;
1818
1919 println ! ( "Next execution time in different timezones:" ) ;
20- println ! ( "" ) ;
20+ println ! ( "-------------------------------------------------------- " ) ;
2121
2222 // UTC
23- if let Ok ( next) = parse ( cron_expr, & utc_now) {
24- println ! ( "UTC: {}" , next. format( "%Y-%m-%d %H:%M:%S %Z" ) ) ;
25- }
23+ print_next ( "UTC" , cron_expr, & utc_now, false ) ;
2624
2725 // Pacific
2826 let pacific_now = utc_now. with_timezone ( & Pacific ) ;
29- if let Ok ( next) = parse ( cron_expr, & pacific_now) {
30- println ! ( "US/Pacific: {}" , next. format( "%Y-%m-%d %H:%M:%S %Z" ) ) ;
31- println ! (
32- " (UTC: {})" ,
33- next. with_timezone( & Utc ) . format( "%Y-%m-%d %H:%M:%S %Z" )
34- ) ;
35- }
27+ print_next ( "US/Pacific" , cron_expr, & pacific_now, true ) ;
3628
3729 // New York
3830 let ny_now = utc_now. with_timezone ( & New_York ) ;
39- if let Ok ( next) = parse ( cron_expr, & ny_now) {
40- println ! ( "America/New_York: {}" , next. format( "%Y-%m-%d %H:%M:%S %Z" ) ) ;
41- println ! (
42- " (UTC: {})" ,
43- next. with_timezone( & Utc ) . format( "%Y-%m-%d %H:%M:%S %Z" )
44- ) ;
45- }
31+ print_next ( "America/New_York" , cron_expr, & ny_now, true ) ;
4632
4733 // London
4834 let london_now = utc_now. with_timezone ( & London ) ;
49- if let Ok ( next) = parse ( cron_expr, & london_now) {
50- println ! ( "Europe/London: {}" , next. format( "%Y-%m-%d %H:%M:%S %Z" ) ) ;
51- println ! (
52- " (UTC: {})" ,
53- next. with_timezone( & Utc ) . format( "%Y-%m-%d %H:%M:%S %Z" )
54- ) ;
55- }
35+ print_next ( "Europe/London" , cron_expr, & london_now, true ) ;
5636
5737 // Tokyo
5838 let tokyo_now = utc_now. with_timezone ( & Tokyo ) ;
59- if let Ok ( next) = parse ( cron_expr, & tokyo_now) {
60- println ! ( "Asia/Tokyo: {}" , next. format( "%Y-%m-%d %H:%M:%S %Z" ) ) ;
61- println ! (
62- " (UTC: {})" ,
63- next. with_timezone( & Utc ) . format( "%Y-%m-%d %H:%M:%S %Z" )
64- ) ;
65- }
39+ print_next ( "Asia/Tokyo" , cron_expr, & tokyo_now, true ) ;
6640
6741 println ! ( ) ;
6842 println ! ( "Note: The same cron expression produces different absolute times" ) ;
6943 println ! ( "depending on the timezone, but represents the same local time." ) ;
7044}
45+
46+ fn print_next < TZ : chrono:: TimeZone > (
47+ label : & str ,
48+ cron_expr : & str ,
49+ now : & chrono:: DateTime < TZ > ,
50+ also_print_utc : bool ,
51+ ) where
52+ TZ :: Offset : std:: fmt:: Display ,
53+ {
54+ match parse ( cron_expr, now) {
55+ Ok ( next) => {
56+ println ! ( "{label:<16} {}" , next. format( "%Y-%m-%d %H:%M:%S %Z" ) ) ;
57+ if also_print_utc {
58+ println ! (
59+ "{:16} (UTC: {})" ,
60+ "" ,
61+ next. with_timezone( & Utc ) . format( "%Y-%m-%d %H:%M:%S %Z" )
62+ ) ;
63+ }
64+ }
65+ Err ( e) => println ! ( "{label:<16} Error: {e:?}" ) ,
66+ }
67+ }
0 commit comments