subr_fattime.c (7ea93e912bf0ef51cec2ce4e86557eef4039ce57) | subr_fattime.c (b39be1b35ced251ca5f39addb70d3c28e6fd2d54) |
---|---|
1/*- 2 * Copyright (c) 2006 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 34 unchanged lines hidden (view full) --- 43 * timezone much less if daylight savings time applies. 44 * 45 * Later on again, in Windows NT, timestamps were defined relative to GMT. 46 * 47 * Purists will point out that UTC replaced GMT for such uses around 48 * a century ago, already then. Ironically "NT" was an abbreviation of 49 * "New Technology". Anyway... 50 * | 1/*- 2 * Copyright (c) 2006 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 34 unchanged lines hidden (view full) --- 43 * timezone much less if daylight savings time applies. 44 * 45 * Later on again, in Windows NT, timestamps were defined relative to GMT. 46 * 47 * Purists will point out that UTC replaced GMT for such uses around 48 * a century ago, already then. Ironically "NT" was an abbreviation of 49 * "New Technology". Anyway... 50 * |
51 * The 'utc' argument determines if the resulting FATTIME timestamp 52 * should b on the UTC or local timezone calendar. | 51 * The functions below always assume UTC time, and the calling code 52 * must apply the local timezone offset as appropriate. Unless special 53 * conditions apply, the utc_offset() function be used for this. |
53 * 54 * The conversion functions below cut time into four-year leap-second 55 * cycles rather than single years and uses table lookups inside those 56 * cycles to get the months and years sorted out. 57 * 58 * Obviously we cannot calculate the correct table index going from 59 * a posix seconds count to Y/M/D, but we can get pretty close by 60 * dividing the daycount by 32 (giving a too low index), and then --- 69 unchanged lines hidden (view full) --- 130 { MAY + 3 * YEAR + 1, ENC(3, 6) }, { JUN + 3 * YEAR + 1, ENC(3, 7) }, 131 { JUL + 3 * YEAR + 1, ENC(3, 8) }, { AUG + 3 * YEAR + 1, ENC(3, 9) }, 132 { SEP + 3 * YEAR + 1, ENC(3, 10) }, { OCT + 3 * YEAR + 1, ENC(3, 11) }, 133 { NOV + 3 * YEAR + 1, ENC(3, 12) } 134}; 135 136 137void | 54 * 55 * The conversion functions below cut time into four-year leap-second 56 * cycles rather than single years and uses table lookups inside those 57 * cycles to get the months and years sorted out. 58 * 59 * Obviously we cannot calculate the correct table index going from 60 * a posix seconds count to Y/M/D, but we can get pretty close by 61 * dividing the daycount by 32 (giving a too low index), and then --- 69 unchanged lines hidden (view full) --- 131 { MAY + 3 * YEAR + 1, ENC(3, 6) }, { JUN + 3 * YEAR + 1, ENC(3, 7) }, 132 { JUL + 3 * YEAR + 1, ENC(3, 8) }, { AUG + 3 * YEAR + 1, ENC(3, 9) }, 133 { SEP + 3 * YEAR + 1, ENC(3, 10) }, { OCT + 3 * YEAR + 1, ENC(3, 11) }, 134 { NOV + 3 * YEAR + 1, ENC(3, 12) } 135}; 136 137 138void |
138timespec2fattime(struct timespec *tsp, int utc, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp) | 139timet2fattime(struct timespec *tsp, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp) |
139{ 140 time_t t1; 141 unsigned t2, l, m; 142 143 t1 = tsp->tv_sec; | 140{ 141 time_t t1; 142 unsigned t2, l, m; 143 144 t1 = tsp->tv_sec; |
144 if (!utc) 145 t1 -= utc_offset(); | |
146 147 if (dhp != NULL) 148 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; 149 if (dtp != NULL) { 150 *dtp = (t1 / 2) % 30; 151 *dtp |= ((t1 / 60) % 60) << 5; 152 *dtp |= ((t1 / 3600) % 24) << 11; 153 } --- 56 unchanged lines hidden (view full) --- 210 DCOD(NOV, 2, 1), DCOD(DEC, 2, 1), 0, 0, 211 0, DCOD( 0, 3, 1), DCOD(JAN, 3, 1), DCOD(FEB, 3, 1), 212 DCOD(MAR, 3, 1), DCOD(APR, 3, 1), DCOD(MAY, 3, 1), DCOD(JUN, 3, 1), 213 DCOD(JUL, 3, 1), DCOD(AUG, 3, 1), DCOD(SEP, 3, 1), DCOD(OCT, 3, 1), 214 DCOD(NOV, 3, 1), DCOD(DEC, 3, 1), 0, 0 215}; 216 217void | 145 146 if (dhp != NULL) 147 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; 148 if (dtp != NULL) { 149 *dtp = (t1 / 2) % 30; 150 *dtp |= ((t1 / 60) % 60) << 5; 151 *dtp |= ((t1 / 3600) % 24) << 11; 152 } --- 56 unchanged lines hidden (view full) --- 209 DCOD(NOV, 2, 1), DCOD(DEC, 2, 1), 0, 0, 210 0, DCOD( 0, 3, 1), DCOD(JAN, 3, 1), DCOD(FEB, 3, 1), 211 DCOD(MAR, 3, 1), DCOD(APR, 3, 1), DCOD(MAY, 3, 1), DCOD(JUN, 3, 1), 212 DCOD(JUL, 3, 1), DCOD(AUG, 3, 1), DCOD(SEP, 3, 1), DCOD(OCT, 3, 1), 213 DCOD(NOV, 3, 1), DCOD(DEC, 3, 1), 0, 0 214}; 215 216void |
218fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc, struct timespec *tsp) | 217fattime2timet(unsigned dd, unsigned dt, unsigned dh, struct timespec *tsp) |
219{ 220 unsigned day; 221 222 /* Unpack time fields */ 223 tsp->tv_sec = (dt & 0x1f) << 1; 224 tsp->tv_sec += ((dt & 0x7e0) >> 5) * 60; 225 tsp->tv_sec += ((dt & 0xf800) >> 11) * 3600; 226 tsp->tv_sec += dh / 100; --- 14 unchanged lines hidden (view full) --- 241 */ 242 if (day >= ((2100 - 1980) / 4 * LYC + FEB)) 243 day--; 244 245 /* Align with time_t epoch */ 246 day += T1980; 247 248 tsp->tv_sec += DAY * day; | 218{ 219 unsigned day; 220 221 /* Unpack time fields */ 222 tsp->tv_sec = (dt & 0x1f) << 1; 223 tsp->tv_sec += ((dt & 0x7e0) >> 5) * 60; 224 tsp->tv_sec += ((dt & 0xf800) >> 11) * 3600; 225 tsp->tv_sec += dh / 100; --- 14 unchanged lines hidden (view full) --- 240 */ 241 if (day >= ((2100 - 1980) / 4 * LYC + FEB)) 242 day--; 243 244 /* Align with time_t epoch */ 245 day += T1980; 246 247 tsp->tv_sec += DAY * day; |
249 if (!utc) 250 tsp->tv_sec += utc_offset(); | |
251} 252 253#ifdef TEST_DRIVER 254 255#include <stdio.h> 256#include <unistd.h> 257#include <stdlib.h> 258 --- 49 unchanged lines hidden --- | 248} 249 250#ifdef TEST_DRIVER 251 252#include <stdio.h> 253#include <unistd.h> 254#include <stdlib.h> 255 --- 49 unchanged lines hidden --- |