#timezone #time #different #getting #world #client #ip

worldtimeapi

A simple API for getting the current time in different timezones

7 releases (4 breaking)

new 0.5.1 May 24, 2024
0.5.0 Oct 18, 2023
0.4.1 Dec 14, 2022
0.4.0 Nov 18, 2022
0.1.0 Mar 29, 2022

#598 in Web programming

Download history 2/week @ 2024-02-23 2/week @ 2024-03-01 21/week @ 2024-03-29 1/week @ 2024-04-05 5/week @ 2024-04-19 81/week @ 2024-04-26

86 downloads per month

MIT license

16KB
184 lines

World Time Wrapper

Rust

This is a simple wrapper for the World Time API. This crate is based on the WorldTimeAPI wrapper by Dulatr.

Usage

To use this crate, add worldtimeapi to your Cargo.toml:

[dependencies]
worldtimeapi = "0.5.1"

Then create a client for an endpoint (currently they only offer "ip" and "timezone"):

use std::collections::HashMap;

use worldtimeapi::service::{Client, Endpoint};

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
   let client = Client::new(Endpoint::Timezone).await?;

   let mut payload = HashMap::new();
   payload.insert("area", "America");
   payload.insert("location", "New_York");

   let result = client.get(payload).await?;
   println!("{}", result.datetime());
   Ok(())
}

To get a list of regions and locations, use the regions method:

use worldtimeapi::service::{Client, Endpoint};

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
   let client = Client::new(Endpoint::Timezone).await?;
   let regions = client.regions();
   println!("{:?}", regions);
   Ok(())
}

Dependencies

~6–20MB
~270K SLoC