duration.c 7.46 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
  if (nanoseconds > 0) {
    if (embb_duration_min()->nanoseconds > nanoseconds) {
54 55
      duration->seconds = 0;
      duration->nanoseconds = 0;
56 57 58 59
      return EMBB_UNDERFLOW;
    }
    const embb_duration_t* max = embb_duration_max();
    if (max->seconds * 1000000000 + max->nanoseconds < nanoseconds) {
60 61
      duration->seconds = max->seconds;
      duration->nanoseconds = max->nanoseconds;
62 63 64 65 66 67 68 69 70 71
      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) {
72 73 74
  if (duration == NULL) {
    return EMBB_ERROR;
  }
75 76
  if (microseconds > 0) {
    if (embb_duration_min()->nanoseconds > microseconds*1000) {
77 78
      duration->seconds = 0;
      duration->nanoseconds = 0;
79 80 81 82
      return EMBB_UNDERFLOW;
    }
    const embb_duration_t* max = embb_duration_max();
    if (max->seconds * 1000000 + max->nanoseconds / 1000 < microseconds) {
83 84
      duration->seconds = max->seconds;
      duration->nanoseconds = max->nanoseconds;
85 86 87 88 89 90 91 92 93 94
      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) {
95 96 97
  if (duration == NULL) {
    return EMBB_ERROR;
  }
98 99
  if (milliseconds > 0) {
    if (embb_duration_min()->nanoseconds > milliseconds*1000000) {
100 101
      duration->seconds = 0;
      duration->nanoseconds = 0;
102 103 104 105
      return EMBB_UNDERFLOW;
    }
    const embb_duration_t* max = embb_duration_max();
    if (max->seconds * 1000 + max->nanoseconds / 1000000 < milliseconds) {
106 107
      duration->seconds = max->seconds;
      duration->nanoseconds = max->nanoseconds;
108 109 110 111 112 113 114 115 116 117
      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) {
118 119 120
  if (duration == NULL) {
    return EMBB_ERROR;
  }
121 122
  if (seconds > 0) {
    if (embb_duration_min()->nanoseconds > seconds*1000000000) {
123 124
      duration->seconds = 0;
      duration->nanoseconds = 0;
125 126 127 128
      return EMBB_UNDERFLOW;
    }
    const embb_duration_t* max = embb_duration_max();
    if (max->seconds + max->nanoseconds / 1000000000 < seconds) {
129 130
      duration->seconds = max->seconds;
      duration->nanoseconds = max->nanoseconds;
131 132 133 134 135 136 137 138 139
      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) {
140 141 142
  if (lhs == NULL || rhs == NULL) {
    return EMBB_ERROR;
  }
143 144
  int carry = (int)((lhs->nanoseconds + rhs->nanoseconds) / 1000000000);
  if (lhs->seconds + rhs->seconds + carry > EMBB_DURATION_MAX_SECONDS) {
145 146
    lhs->seconds = 0;
    lhs->nanoseconds = 0;
147 148 149 150 151 152 153 154 155
    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) {
156 157 158
  if (duration == NULL || nanoseconds == NULL) {
    return EMBB_ERROR;
  }
159 160 161 162 163 164 165 166 167
  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) {
168 169 170
  if (duration == NULL || microseconds == NULL) {
    return EMBB_ERROR;
  }
171 172 173 174 175 176 177 178 179 180 181 182
  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) {
183 184 185
  if (duration == NULL || milliseconds == NULL) {
    return EMBB_ERROR;
  }
186 187 188 189 190 191 192 193 194 195 196 197
  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) {
198 199 200
  if (duration == NULL || seconds == NULL) {
    return EMBB_ERROR;
  }
201 202 203 204 205 206 207 208 209 210 211 212
  if (duration->nanoseconds % 1000000000 > 0) {
    return EMBB_UNDERFLOW;
  }
  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) {
213 214
  assert(lhs != NULL && rhs != NULL);
  assert(lhs->nanoseconds < 1000000000 && rhs->nanoseconds < 1000000000);
215 216 217 218 219 220 221 222 223 224 225 226 227
  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;
}