Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CheckOptions:
- key: readability-identifier-length.IgnoredParameterNames
value: '^fd|fp$'
- key: readability-identifier-length.IgnoredVariableNames
value: '^fd|p|q|st|tm|tz$'
value: '^fd|n|p|q|st|tm|tz$'
- key: readability-identifier-naming.EnumCase
value: lower_case
- key: readability-identifier-naming.FunctionCase
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ src_files = \
src/core/ndarray.c \
src/core/ndarray_convert.c \
src/core/software.c \
src/core/time.c \
src/emitter.c \
src/error.c \
src/event.c \
Expand Down
1 change: 1 addition & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ install(
asdf/core/extension_metadata.h
asdf/core/software.h
asdf/core/ndarray.h
asdf/core/time.h

DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/asdf/core"
)
Expand Down
1 change: 1 addition & 0 deletions include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ nobase_include_HEADERS = \
asdf/core/history_entry.h \
asdf/core/ndarray.h \
asdf/core/software.h \
asdf/core/time.h \
asdf/emitter.h \
asdf/error.h \
asdf/event.h \
Expand Down
1 change: 1 addition & 0 deletions include/asdf/core/asdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <asdf/core/extension_metadata.h>
#include <asdf/core/history_entry.h>
#include <asdf/core/software.h>
#include <asdf/core/time.h>
#include <asdf/extension.h>
#include <asdf/util.h>

Expand Down
8 changes: 5 additions & 3 deletions include/asdf/core/history_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#include <asdf/extension.h>
#include <asdf/file.h>

#include <asdf/core/time.h>


ASDF_BEGIN_DECLS

typedef struct {
const char *description;
struct timespec time;
const asdf_time_t *time;
const asdf_software_t **software;
} asdf_history_entry_t;

Expand All @@ -28,8 +30,8 @@ ASDF_DECLARE_EXTENSION(history_entry, asdf_history_entry_t);
* Allow passing a timestamp for the history entry as well, or an extended
* version that accepts a timestamp.
*
* :params file: Open `asdf_file_t *` handle
* :params description: The text to add to the history entry
* :param file: Open `asdf_file_t *` handle
* :param description: The text to add to the history entry
* :return: Non-zero if adding the history entry failed
*/
ASDF_EXPORT int asdf_history_entry_add(asdf_file_t *file, const char *description);
Expand Down
86 changes: 86 additions & 0 deletions include/asdf/core/time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/** Data type and extension for the stsci.edu/schemas/asdf/time/time schema */
#ifndef ASDF_CORE_TIME_H
#define ASDF_CORE_TIME_H

#include <asdf/extension.h>
#include <sys/time.h>


ASDF_BEGIN_DECLS

#define ASDF_CORE_TIME_TAG "tag:stsci.edu:asdf/time/time-1.4.0"
#define ASDF_TIME_TIMESTR_MAXLEN 255

typedef enum {
ASDF_TIME_FORMAT_ISO_TIME = 0,
ASDF_TIME_FORMAT_YDAY,
ASDF_TIME_FORMAT_BYEAR,
ASDF_TIME_FORMAT_JYEAR,
ASDF_TIME_FORMAT_DECIMALYEAR,
ASDF_TIME_FORMAT_JD,
ASDF_TIME_FORMAT_MJD,
ASDF_TIME_FORMAT_GPS,
ASDF_TIME_FORMAT_UNIX,
ASDF_TIME_FORMAT_UTIME,
ASDF_TIME_FORMAT_TAI_SECONDS,
ASDF_TIME_FORMAT_CXCSEC,
ASDF_TIME_FORMAT_GALEXSEC,
ASDF_TIME_FORMAT_UNIX_TAI,
ASDF_TIME_FORMAT_RESERVED1,
/* "other" format(s) below */
ASDF_TIME_FORMAT_BYEAR_STR,
ASDF_TIME_FORMAT_DATETIME,
ASDF_TIME_FORMAT_FITS,
ASDF_TIME_FORMAT_ISOT,
ASDF_TIME_FORMAT_JYEAR_STR,
ASDF_TIME_FORMAT_PLOT_DATE,
ASDF_TIME_FORMAT_YMDHMS,
ASDF_TIME_FORMAT_datetime64,
} asdf_time_base_format_t;


typedef enum {
ASDF_TIME_SCALE_UTC = 0,
ASDF_TIME_SCALE_TAI,
ASDF_TIME_SCALE_TCB,
ASDF_TIME_SCALE_TCG,
ASDF_TIME_SCALE_TDB,
ASDF_TIME_SCALE_TT,
ASDF_TIME_SCALE_UT1,
} asdf_time_scale_t;

typedef struct {
double longitude;
double latitude;
double height;
} asdf_time_location_t;

typedef struct {
bool is_base_format;
asdf_time_base_format_t type;
} asdf_time_format_t;

struct asdf_time_info_t {
struct timespec ts;
struct tm tm;
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but then don't use the _t suffix for anything that isn't typedef'd; then it's hard to remember it needs struct :)


typedef struct {
char *value;
struct asdf_time_info_t info;
asdf_time_format_t format;
asdf_time_scale_t scale;
asdf_time_location_t location;
} asdf_time_t;

ASDF_DECLARE_EXTENSION(time, asdf_time_t);

ASDF_LOCAL int asdf_time_parse_std(
const char *s, const asdf_time_format_t *format, struct asdf_time_info_t *out);
ASDF_LOCAL int asdf_time_parse_byear(const char *s, struct asdf_time_info_t *out);
ASDF_LOCAL int asdf_time_parse_yday(const char *s, struct asdf_time_info_t *out);


ASDF_END_DECLS

#endif /* ASDF_CORE_TIME_H */
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(libasdf_sources
core/ndarray.c
core/ndarray_convert.c
core/software.c
core/time.c
block.c
context.c
error.c
Expand Down
137 changes: 33 additions & 104 deletions src/core/history_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
#include "config.h"
#endif

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "../error.h"
#include "../extension_util.h"
Expand All @@ -17,98 +15,11 @@
#include "asdf.h"
#include "history_entry.h"
#include "software.h"
#include "time.h"


#define ASDF_CORE_HISTORY_ENTRY_TAG ASDF_CORE_TAG_PREFIX "history_entry-1.0.0"

/*
* Parse a YAML-serialized timestamp
*
* Generally in ISO8601 but can be "relaxed" having a space between the date and the time (the
* Python asdf actually appears to output in this format though maybe it depends on the Python
* yaml version--we should specify this more strictly maybe...
*/
#ifdef HAVE_STRPTIME
#define NSEC_PER_SEC 1e9 // In case this ever changes
#define SEC_PER_HOUR 3600
#define SEC_PER_MIN 60

static int asdf_parse_datetime(const char *scalar, struct timespec *out) {
if (!scalar || !out)
return -1;

struct tm tm = {0};
char tz_sign = 0;
int tz_hour = 0;
int tz_min = 0;
long nsec = 0;
bool has_time = false;
char *rest = NULL;
char *buf = strdup(scalar);

if (!buf)
return -1;

// Normalize separators (replace 'T' or 't' with space)
for (char *chr = buf; *chr; ++chr)
if (*chr == 'T' || *chr == 't')
*chr = ' ';

// Try to parse date and time (without optional fractional seconds and timezone)
rest = strptime(buf, "%Y-%m-%d %H:%M:%S", &tm);

if (!rest)
rest = strptime(buf, "%Y-%m-%d", &tm);
else
has_time = true;

if (!rest) {
free(buf);
return -1;
}

// Handle optional fractional seconds
if (has_time) {
const char *dot = strchr(rest, '.');
if (dot) {
double frac = 0;
sscanf(dot, "%lf", &frac);
nsec = (long)((frac - (int)frac) * NSEC_PER_SEC);
}

// Handle timezone offsets (Z/z = Zulu is ignored, just don't add any offset)
const char *tz = strpbrk(rest, "+-");
if (tz && (*tz == '+' || *tz == '-')) {
tz_sign = (*tz == '-') ? -1 : 1;
if (sscanf(tz + 1, "%2d:%2d", &tz_hour, &tz_min) < 1)
sscanf(tz + 1, "%2d", &tz_hour);
}
}

// Convert to time_t and adjust for time zone
time_t time = timegm(&tm);
if (time == (time_t)-1) {
free(buf);
return -1;
}

time -= (long)tz_sign * (tz_hour * SEC_PER_HOUR + tz_min * SEC_PER_MIN);
out->tv_sec = time;
out->tv_nsec = nsec;
free(buf);
return 0;
}
#else
#warning "strptime() not available, times will not be parsed"
static int asdf_parse_datetime(UNUSED(const char *s), struct timespec *out) {
if (out) {
out->tv_sec = 0;
out->tv_nsec = 0;
}
return 0;
}
#endif


static asdf_value_t *asdf_history_entry_serialize(
asdf_file_t *file,
Expand Down Expand Up @@ -237,14 +148,13 @@ static asdf_software_t **asdf_history_entry_deserialize_software(asdf_value_t *v
return software;
}


static asdf_value_err_t asdf_history_entry_deserialize(
asdf_value_t *value, UNUSED(const void *userdata), void **out) {
asdf_value_err_t err = ASDF_VALUE_ERR_PARSE_FAILURE;
asdf_value_t *prop = NULL;
const char *description = NULL;
const char *time_str = NULL;
struct timespec time = {0};
asdf_time_t *time = NULL;
asdf_software_t **software = NULL;
asdf_mapping_t *entry_map = NULL;

Expand All @@ -264,24 +174,33 @@ static asdf_value_err_t asdf_history_entry_deserialize(
prop = asdf_mapping_get(entry_map, "time");

if (prop) {
bool valid_time = false;
if (ASDF_VALUE_OK == asdf_value_as_string0(prop, &time_str)) {
if (0 == asdf_parse_datetime(time_str, &time))

// cast the value of "time" to an asdf_time_t
const asdf_extension_t *time_ext = asdf_extension_get(value->file, ASDF_CORE_TIME_TAG);
if (time_ext) {
bool valid_time = false;
time_ext->deserialize(prop, NULL, (void *)&time);

if (time) {
valid_time = true;
}
}

#ifdef ASDF_LOG_ENABLED
if (!valid_time) {
if (ASDF_VALUE_OK != asdf_value_as_scalar0(prop, &time_str)) {
time_str = "<unreadable>";
if (!valid_time) {
if (ASDF_VALUE_OK != asdf_value_as_scalar0(prop, &time_str)) {
time_str = "<unreadable>";
}
ASDF_LOG(
value->file,
ASDF_LOG_WARN,
"ignoring invalid time %s in history_entry",
time_str);
}
ASDF_LOG(
value->file, ASDF_LOG_WARN, "ignoring invalid time %s in history_entry", time_str);
}
#endif
}
asdf_value_destroy(prop);
}

asdf_value_destroy(prop);

/* Software can be either an array of software or a single entry, but here it is always
* returned as a NULL-terminated array of asdf_software_t *
Expand All @@ -305,6 +224,7 @@ static asdf_value_err_t asdf_history_entry_deserialize(
entry->time = time;
entry->software = (const asdf_software_t **)software;
*out = entry;

return ASDF_VALUE_OK;
failure:
asdf_value_destroy(prop);
Expand All @@ -321,6 +241,10 @@ static void asdf_history_entry_dealloc(void *value) {

free((void *)entry->description);

if (entry->time) {
asdf_time_destroy((asdf_time_t *)entry->time);
}

if (entry->software) {
for (asdf_software_t **sp = (asdf_software_t **)entry->software; *sp; ++sp) {
asdf_software_destroy(*sp);
Expand Down Expand Up @@ -350,7 +274,12 @@ static void *asdf_history_entry_copy(const void *value) {
goto failure;
}

copy->time = entry->time;
if (entry->time) {
copy->time = asdf_time_clone(entry->time);

if (!copy->time)
goto failure;
}

if (entry->software) {
copy->software = (const asdf_software_t **)asdf_software_array_clone(entry->software);
Expand Down
Loading
Loading