duration.c 6.93 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <embb/base/c/duration.h>
#include <embb/base/c/internal/platform.h>
#include <limits.h>
#include <assert.h>

const embb_duration_t* embb_duration_max() {
  static embb_duration_t max = { EMBB_DURATION_MAX_SECONDS, 0 };
  return &max;
}

const embb_duration_t* embb_duration_min() {
  static embb_duration_t min = { 0, EMBB_DURATION_MIN_NANOSECONDS };
  return &min;
}

const embb_duration_t* embb_duration_zero() {
  static embb_duration_t zero = { 0, 0 };
  return &zero;
}

int embb_duration_set_nanoseconds(embb_duration_t* duration,
                                  unsigned long long nanoseconds) {
49 50 51
  if (duration == NULL) {
    return EMBB_ERROR;
  }
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  if (nanoseconds > 0) {
    if (embb_duration_min()->nanoseconds > nanoseconds) {
      return EMBB_UNDERFLOW;
    }
    const embb_duration_t* max = embb_duration_max();
    if (max->seconds * 1000000000 + max->nanoseconds < nanoseconds) {
      return EMBB_OVERFLOW;
    }
  }
  duration->seconds = nanoseconds / 1000000000;
  duration->nanoseconds = (nanoseconds % 1000000000);
  return EMBB_SUCCESS;
}

int embb_duration_set_microseconds(embb_duration_t* duration,
                                   unsigned long long microseconds) {
68 69 70
  if (duration == NULL) {
    return EMBB_ERROR;
  }
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  if (microseconds > 0) {
    if (embb_duration_min()->nanoseconds > microseconds*1000) {
      return EMBB_UNDERFLOW;
    }
    const embb_duration_t* max = embb_duration_max();
    if (max->seconds * 1000000 + max->nanoseconds / 1000 < microseconds) {
      return EMBB_OVERFLOW;
    }
  }
  duration->seconds = microseconds / 1000000;
  duration->nanoseconds = (microseconds % 1000000) * 1000;
  return EMBB_SUCCESS;
}

int embb_duration_set_milliseconds(embb_duration_t* duration,
                                   unsigned long long milliseconds) {
87 88 89
  if (duration == NULL) {
    return EMBB_ERROR;
  }
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  if (milliseconds > 0) {
    if (embb_duration_min()->nanoseconds > milliseconds*1000000) {
      return EMBB_UNDERFLOW;
    }
    const embb_duration_t* max = embb_duration_max();
    if (max->seconds * 1000 + max->nanoseconds / 1000000 < milliseconds) {
      return EMBB_OVERFLOW;
    }
  }
  duration->seconds = milliseconds / 1000;
  duration->nanoseconds = (milliseconds % 1000) * 1000000;
  return EMBB_SUCCESS;
}

int embb_duration_set_seconds(embb_duration_t* duration,
                              unsigned long long seconds) {
106 107 108
  if (duration == NULL) {
    return EMBB_ERROR;
  }
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  if (seconds > 0) {
    if (embb_duration_min()->nanoseconds > seconds*1000000000) {
      return EMBB_UNDERFLOW;
    }
    const embb_duration_t* max = embb_duration_max();
    if (max->seconds + max->nanoseconds / 1000000000 < seconds) {
      return EMBB_OVERFLOW;
    }
  }
  duration->seconds = seconds;
  duration->nanoseconds = 0;
  return EMBB_SUCCESS;
}

int embb_duration_add(embb_duration_t* lhs, const embb_duration_t* rhs) {
124 125 126
  if (lhs == NULL || rhs == NULL) {
    return EMBB_ERROR;
  }
127 128 129 130 131 132 133 134 135 136 137
  int carry = (int)((lhs->nanoseconds + rhs->nanoseconds) / 1000000000);
  if (lhs->seconds + rhs->seconds + carry > EMBB_DURATION_MAX_SECONDS) {
    return EMBB_OVERFLOW;
  }
  lhs->nanoseconds = (lhs->nanoseconds + rhs->nanoseconds) % 1000000000;
  lhs->seconds = lhs->seconds + rhs->seconds + carry;
  return EMBB_SUCCESS;
}

int embb_duration_as_nanoseconds(const embb_duration_t* duration,
                                 unsigned long long* nanoseconds) {
138 139 140
  if (duration == NULL || nanoseconds == NULL) {
    return EMBB_ERROR;
  }
141 142 143 144 145 146 147 148 149
  if (duration->seconds*1000000000 + duration->nanoseconds > ULLONG_MAX) {
    return EMBB_OVERFLOW;
  }
  *nanoseconds = duration->seconds*1000000000 + duration->nanoseconds;
  return EMBB_SUCCESS;
}

int embb_duration_as_microseconds(const embb_duration_t* duration,
                                  unsigned long long* microseconds) {
150 151 152
  if (duration == NULL || microseconds == NULL) {
    return EMBB_ERROR;
  }
153 154 155 156 157 158 159 160 161 162 163 164
  if (duration->nanoseconds % 1000 > 0) {
    return EMBB_UNDERFLOW;
  }
  if (duration->seconds*1000000 + duration->nanoseconds/1000 > ULLONG_MAX) {
    return EMBB_OVERFLOW;
  }
  *microseconds = duration->seconds*1000000 + duration->nanoseconds / 1000;
  return EMBB_SUCCESS;
}

int embb_duration_as_milliseconds(const embb_duration_t* duration,
                                  unsigned long long* milliseconds) {
165 166 167
  if (duration == NULL || milliseconds == NULL) {
    return EMBB_ERROR;
  }
168 169 170 171 172 173 174 175 176 177 178 179
  if (duration->nanoseconds % 1000000 > 0) {
    return EMBB_UNDERFLOW;
  }
  if (duration->seconds*1000 + duration->nanoseconds/1000000 > ULLONG_MAX) {
    return EMBB_OVERFLOW;
  }
  *milliseconds = duration->seconds*1000 + duration->nanoseconds / 1000000;
  return EMBB_SUCCESS;
}

int embb_duration_as_seconds(const embb_duration_t* duration,
                             unsigned long long* seconds) {
180 181 182
  if (duration == NULL || seconds == NULL) {
    return EMBB_ERROR;
  }
183 184 185 186 187 188 189 190 191 192 193 194 195
  if (duration->nanoseconds % 1000000000 > 0) {
    return EMBB_UNDERFLOW;
  }
  assert(duration->nanoseconds % 1000000000 == 0);
  if (duration->seconds > ULLONG_MAX) {
    return EMBB_OVERFLOW;
  }
  *seconds = duration->seconds;
  return EMBB_SUCCESS;
}

int embb_duration_compare(const embb_duration_t* lhs,
                          const embb_duration_t* rhs) {
196 197 198 199 200 201
  if (lhs == NULL || rhs == NULL) {
    return EMBB_ERROR;
  }
  if (lhs->nanoseconds >= 1000000000 || rhs->nanoseconds >= 1000000000) {
    return EMBB_ERROR;
  }
202 203 204 205 206 207 208 209 210 211 212 213 214
  if (lhs->seconds > rhs->seconds) {
    return 1;
  } else if (lhs->seconds < rhs->seconds) {
    return -1;
  } else { /* Seconds are equal */
    if (lhs->nanoseconds > rhs->nanoseconds) {
      return 1;
    } else if (lhs->nanoseconds < rhs->nanoseconds) {
      return -1;
    }
  }
  return 0;
}