Skip to content

C SDK

Install

cd sdk/c && mkdir -p build && cd build
cmake .. && make

Produces libtimenow.a. Requires libcurl.

Quick Start

#include "timenow.h"
#include <stdio.h>

int main(void) {
    timenow_client_t client;
    timenow_init(&client, "http://localhost:8090");

    // Solar time for London
    timenow_solar_result_t solar;
    if (timenow_get_solar(&client, 51.5074, -0.1278, NULL, &solar) == TIMENOW_OK) {
        printf("Solar time: %s\n", solar.local_solar_time);
        printf("Offset: %.1f min\n", solar.civil_solar_offset_min);
    }

    // Social jetlag
    timenow_jetlag_result_t jetlag;
    if (timenow_get_jetlag(&client, 39.47, 75.99, &jetlag) == TIMENOW_OK) {
        printf("Risk: %s\n", jetlag.risk_level);
    }

    timenow_cleanup(&client);
    return 0;
}

Link with: gcc myapp.c -ltimenow -lcurl -o myapp

Functions

Function Parameters Returns API endpoint
timenow_init client, base_url timenow_error_t
timenow_cleanup client void
timenow_get_solar client, lat, lng, dt, result timenow_error_t GET /solar
timenow_get_jetlag client, lat, lng, result timenow_error_t GET /solar/jetlag
timenow_get_prayer client, lat, lng, date, buf, size timenow_error_t GET /solar/prayer
timenow_get_almanac client, lat, lng, year, buf, size timenow_error_t GET /solar/almanac
timenow_get_reform client, lat, lng, buf, size timenow_error_t GET /solar/reform
timenow_get_health client, buf, size timenow_error_t GET /health
timenow_get_raw client, path, buf, size timenow_error_t Any GET

Types

typedef struct {
    char base_url[256];
    void *_curl;
} timenow_client_t;

typedef struct {
    double lat, lng;
    char local_solar_time[64];
    char solar_noon[64];
    char sunrise[64], sunset[64];
    double solar_elevation_deg;
    double civil_solar_offset_min;
} timenow_solar_result_t;

typedef struct {
    char risk_level[32];
    double civil_solar_offset_min;
    double weekly_sleep_loss_min;
    double annual_sleep_loss_h;
    char equivalent_flight[128];
    char verdict[256];
} timenow_jetlag_result_t;

typedef enum {
    TIMENOW_OK       =  0,
    TIMENOW_ERR_CURL = -1,
    TIMENOW_ERR_HTTP = -2,
    TIMENOW_ERR_JSON = -3,
    TIMENOW_ERR_INIT = -4
} timenow_error_t;

Error Handling

All functions return timenow_error_t. Check the return value before using results:

timenow_error_t err = timenow_get_solar(&client, 999, 0, NULL, &solar);
if (err != TIMENOW_OK) {
    fprintf(stderr, "Error: %d\n", err);
}