Time formatting

Format dates and times in a variety of ways.

VCL

Use this solution in your VCL service (click RUN below to test this solution or clone it to make changes):

Compute@Edge

Use this solution in your Compute@Edge service:

  1. Rust
// setting date/time variables
let dt = Utc.ymd(2014, 11, 28).and_hms(12, 0, 9);
let fixed_dt = dt.with_timezone(&FixedOffset::east(9 * 3600));
// you can also take offset from timezone of connected user
let geo = req.get_client_ip_addr().and_then(geo_lookup).unwrap();
let _local_dt = dt.with_timezone(&geo.utc_offset());
// setting time by time string parsing
// method 1
let tt = "2014-11-28T12:00:09Z".parse::<DateTime<Utc>>().unwrap();
assert_eq!(tt.clone(), dt.clone());
let tt = "2014-11-28T21:00:09+09:00"
.parse::<DateTime<Utc>>()
.unwrap();
assert_eq!(tt.clone(), dt.clone());
let tt = "2014-11-28T21:00:09+09:00"
.parse::<DateTime<FixedOffset>>()
.unwrap();
assert_eq!(tt, fixed_dt.clone());
// method 2
let tt =
DateTime::parse_from_str("2014-11-28 21:00:09 +09:00", "%Y-%m-%d %H:%M:%S %z").unwrap();
assert_eq!(tt.clone(), fixed_dt.clone());
let tt = DateTime::parse_from_rfc2822("Fri, 28 Nov 2014 21:00:09 +0900").unwrap();
assert_eq!(tt.clone(), fixed_dt.clone());
let tt = DateTime::parse_from_rfc3339("2014-11-28T21:00:09+09:00").unwrap();
assert_eq!(tt.clone(), fixed_dt.clone());
// method 3
let tt = Utc
.datetime_from_str("2014-11-28 12:00:09", "%Y-%m-%d %H:%M:%S")
.unwrap();
assert_eq!(tt.clone(), dt.clone());
let tt = Utc
.datetime_from_str("Fri Nov 28 12:00:09 2014", "%a %b %e %T %Y")
.unwrap();
assert_eq!(tt.clone(), dt.clone());
// time formatting
// RFC 2822 or RFC 5322
assert_eq!(dt.to_rfc2822(), "Fri, 28 Nov 2014 12:00:09 +0000");
// RFC 3339
assert_eq!(dt.to_rfc3339(), "2014-11-28T12:00:09+00:00");
// Unix EPOCH format
assert_eq!(dt.format("%s").to_string(), "1417176009");
// Other options
assert_eq!(dt.to_string(), "2014-11-28 12:00:09 UTC");
assert_eq!(
dt.format("%Y-%m-%d %H:%M:%S").to_string(),
"2014-11-28 12:00:09"
);
assert_eq!(
dt.format("%a %b %e %T %Y").to_string(),
"Fri Nov 28 12:00:09 2014"
);