Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <stdint.h>
- #include <getopt.h>
- #define OFFSET 2208988800ULL
- int main(int argc, char *argv[]) {
- struct timeval unix;
- unsigned char ntp[8];
- gettimeofday(&t, NULL);
- printf("unix:\n");
- printf("%ld.%06ld\n", unix->tv_sec, unix->tv_usec);
- printf("unix2ntp:\n");
- unix_to_ntp(&t, ntp);
- int i;
- int res = 0;
- for (i = 0; i < sizeof ntp; i++) {
- if (i == sizeof ntp / 2)
- res += printf(".");
- res += printf("%02x", ntp[i]);
- } /* for */
- res += printf("\n");
- printf("%d\n", res):
- printf("ntp2unix:\n");
- ntp_to_unix(ntp, &t);
- printf("%ld.%06ld\n", unix->tv_sec, unix->tv_usec);
- }
- void unix_to_ntp(struct timeval *unix, unsigned char *ntp[]) {
- unsigned long aux = 0;
- unsigned char *p = ntp + sizeof ntp;
- int i;
- aux = unix -> tv_usec;
- aux <<= 32;
- aux /= 1000000;
- for (i = 0; i < sizeof ntp/2; i++) {
- *--p = aux & 0xFF;
- aux >>= 8;
- }
- aux = unix -> tv_sec;
- aux += OFFSET;
- for (i; i < sizeof ntp; i++) {
- *--p = aux & 0xFF;
- aux >>= 8;
- }
- }
- void ntp_to_unix(unsigned char *ntp[], struct timeval *unix) {
- unsigned long aux = 0;
- unsigned char *p = ntp;
- int i;
- for (i = 0; i < sizeof ntp/2; i++) {
- aux <<= 8;
- aux |= *p++;
- }
- aux -= OFFSET;
- unix -> tv_sec = aux;
- aux = 0;
- for (i; i < sizeof ntp; i++) {
- aux <<= 8;
- aux |= *p++;
- }
- aux *= 1000000;
- aux >>= 32;
- unix -> tv_usec = aux;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement