Skip to content
Snippets Groups Projects
mtime_timedelta.c 73.24 KiB
/**
 * @file mtime_timedelta.c
 *
 * @brief TimeDelta and some operations supported on TimeDelta.
 *
 * @author Luis Kornblueh, Rahul Sinha. MPIM.
 * @date March 2013
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <math.h>
#include "int_div.h"

#include "mtime_timedelta.h"

#include "mtime_julianDay.h"
#include "mtime_datetime.h"
#include "mtime_iso8601.h"



/* MIN allowed year : -2147483648
   MAX allowed year :  2147483647
   Upto MilliSecond resolution supported. */



/**
 * @brief Construct new TimeDelta using an ISO 8601 conforming string.
 *
 * @param  tds
 *         A pointer to char. The string should contain parameters with which TimeDelta is to be created.
 *
 * @return td
 *         A pointer to a filled TimeDelta.
 */

struct _timedelta*
newTimeDelta(const char* tds)
{
  if ((tds != NULL) && (getCalendarType()))
    {
      char duration_type_flag = 0;
      /* Verify string is ISO8601 compliant. */
      struct iso8601_duration* isoDuration = new_iso8601_duration('+', 0, 0, 0, 0, 0, 0, 0);
      if (isoDuration == NULL )
        return NULL ;

      if (
           ((duration_type_flag = verify_string_duration(tds, isoDuration)) != DURATION_MATCH_STD)
           &&
           (duration_type_flag != DURATION_MATCH_LONG)
         )
        {
          deallocate_iso8601_duration(isoDuration);
          isoDuration = NULL;
          return NULL ;
        }

      /* Create TimeDelta object. */
      struct _timedelta* td = (struct _timedelta *)calloc(1,sizeof(struct _timedelta));
      if (td == NULL )
        {
          deallocate_iso8601_duration(isoDuration);
          isoDuration = NULL;