libsdfio 1.0.0
Reader and writer for ISO 25178-71 SDF surface data files in C
Loading...
Searching...
No Matches
custom_timezone.c
/* Convert the dates of an ISO-1.0 file with a fixed-offset time zone. */
#include <sdfio/file.h>
#include <sdfio/sdfio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* A time zone with a fixed offset from UTC and without daylight saving time. A
* program on a system where the C library knows the zone uses
* sdfio_system_time_zone() and not its own conversion. */
typedef struct zone
{
time_t offset;
} zone;
static time_t local_to_utc(void *user, const sdfio_date *local)
{
time_t clock = 0;
return sdfio_date_to_time(local, &clock) == SDFIO_OK
? clock - ((const zone *)user)->offset
}
static bool utc_to_local(void *user, time_t utc, sdfio_date *local)
{
return sdfio_date_from_time(utc + ((const zone *)user)->offset, local)
}
static void print_date(const char *label, time_t date)
{
char text[32];
strftime(text, sizeof text, "%Y-%m-%d %H:%M", gmtime(&date));
printf("%-38s %s\n", label, text);
}
static time_t read_creation_date(const sdfio_time_zone *time_zone,
const char *path)
{
sdfio_reader *reader = NULL;
sdfio_reader_set_time_zone(reader, time_zone);
time_t date = SDFIO_NO_TIME;
if (sdfio_open_file(reader, path) == SDFIO_OK)
{
}
return date;
}
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "usage: %s output.sdf\n", argv[0]);
return EXIT_FAILURE;
}
const char *path = argv[1];
zone central_europe = {3600};
const sdfio_time_zone time_zone = {&central_europe, local_to_utc,
utc_to_local};
const time_t written = 1782900000; /* 2026-07-01 10:00 UTC */
const float height = 1e-6F;
sdfio_header *header = NULL;
sdfio_header_set_created(header, written);
sdfio_header_set_modified(header, written);
sdfio_writer *writer = NULL;
sdfio_writer_set_time_zone(writer, &time_zone);
sdfio_status status = sdfio_create_file(writer, path, header);
if (status == SDFIO_OK)
{
}
if (status == SDFIO_OK)
{
status = sdfio_end_file(writer);
}
if (status != SDFIO_OK)
{
fprintf(stderr, "%s\n", sdfio_writer_error_message(writer));
}
if (status != SDFIO_OK)
{
return EXIT_FAILURE;
}
print_date("written, UTC:", written);
print_date("read with the time zone, UTC:",
read_creation_date(&time_zone, path));
print_date("read without, the clock of the file:",
read_creation_date(NULL, path));
return EXIT_SUCCESS;
}
Files by path, and the stream over a C file.
sdfio_status sdfio_open_file(sdfio_reader *reader, const char *path)
Opens the file at a path as the source and reads its header, as sdfio_open_stream() does for a stream...
sdfio_status sdfio_create_file(sdfio_writer *writer, const char *path, const sdfio_header *header)
Creates the file at a path and starts it as sdfio_create_stream() does.
struct sdfio_header sdfio_header
Header of a file, with the field names of the standard.
Definition header.h:42
sdfio_status sdfio_header_set_revision(sdfio_header *header, sdfio_revision revision)
Sets the revision of the format.
void sdfio_header_free(sdfio_header *header)
Releases a header that sdfio_header_create() created.
time_t sdfio_header_created(const sdfio_header *header)
Returns the original creation date and time as a time_t, which is read as integer seconds since 1970-...
sdfio_status sdfio_header_set_modified(sdfio_header *header, time_t modified)
Sets the last modification date and time.
sdfio_status sdfio_header_create(const sdfio_allocator *allocator, sdfio_header **header)
Creates a header with default values.
sdfio_status sdfio_header_set_created(sdfio_header *header, time_t created)
Sets the original creation date and time, rounded down to the minute.
sdfio_status sdfio_header_set_rows(sdfio_header *header, uint32_t rows)
Sets the number of rows N of the data matrix.
sdfio_status sdfio_header_set_columns(sdfio_header *header, uint32_t columns)
Sets the number of columns M of the data matrix.
sdfio_status sdfio_header_set_data_type(sdfio_header *header, sdfio_data_type data_type)
Sets the data type of the coded values in the data area.
const sdfio_layout * sdfio_layout_packed(sdfio_data_type type)
Returns the layout of a plain array: the elements follow each other and the rows follow each other.
sdfio_status sdfio_reader_set_time_zone(sdfio_reader *reader, const sdfio_time_zone *time_zone)
Sets the conversion of the dates of ISO-1.0 files.
sdfio_status sdfio_reader_add_binary(sdfio_reader *reader)
Adds the binary format to the formats that the reader reads, the version numbers bISO-1....
const sdfio_header * sdfio_reader_header(const sdfio_reader *reader)
Returns the header of the opened file, or null if no file is open.
struct sdfio_reader sdfio_reader
Opaque handle of a reader.
Definition reader.h:58
sdfio_status sdfio_reader_create(const sdfio_allocator *allocator, sdfio_reader **reader)
Creates a reader.
void sdfio_reader_free(sdfio_reader *reader)
Releases a reader and closes the file that sdfio_open_file() opened, if any.
Reading and writing of ISO 25178-71 SDF surface data files.
Calendar fields of a date and the time of day, with a resolution of one minute.
Definition types.h:251
Conversion of the dates of ISO-1.0 files between the local clock of the writer and UTC.
Definition types.h:311
@ SDFIO_ISO_1_0
Revision ISO-1.0.
Definition types.h:74
sdfio_status
Result of a function call.
Definition types.h:31
@ SDFIO_OK
The call succeeded.
Definition types.h:33
const sdfio_allocator * sdfio_default_allocator(void)
Returns the allocator that uses malloc() and free().
@ SDFIO_TYPE_FLOAT
float: IEEE 754 binary32.
Definition types.h:114
@ SDFIO_TYPE_INT16
int16_t.
Definition types.h:118
#define SDFIO_NO_TIME
Value of a date that is not recorded: a time_t of -1.
Definition types.h:146
sdfio_status sdfio_date_to_time(const sdfio_date *date, time_t *utc)
Converts the fields of a date to a time, as if they were UTC.
sdfio_status sdfio_date_from_time(time_t utc, sdfio_date *date)
Converts a time to the fields of its UTC date.
sdfio_status sdfio_writer_add_binary(sdfio_writer *writer)
Adds the binary format to the formats that the writer writes.
sdfio_status sdfio_writer_create(const sdfio_allocator *allocator, sdfio_writer **writer)
Creates a writer.
struct sdfio_writer sdfio_writer
Opaque handle of a writer.
Definition writer.h:69
const char * sdfio_writer_error_message(const sdfio_writer *writer)
Returns a description of the most recent failure of the writer, or an empty string if none occurred.
void sdfio_writer_free(sdfio_writer *writer)
Releases a writer.
sdfio_status sdfio_write_heights(sdfio_writer *writer, const sdfio_layout *layout, const void *src)
Writes the whole data area from a matrix of heights in metres that lies in memory as the layout descr...
sdfio_status sdfio_writer_set_time_zone(sdfio_writer *writer, const sdfio_time_zone *time_zone)
Sets the conversion of the dates of ISO-1.0 files.
sdfio_status sdfio_end_file(sdfio_writer *writer)
Ends the file: closes the trailer, the last record, flushes the stream and, for a file that sdfio_cre...