libfreerdp-utils: added GeneralizedTime parser

This commit is contained in:
Marc-André Moreau
2012-03-06 15:12:26 -05:00
parent 6af090c55e
commit 3d112b116c
3 changed files with 40 additions and 11 deletions

View File

@@ -20,6 +20,7 @@
#ifndef FREERDP_TIME_UTILS_H
#define FREERDP_TIME_UTILS_H
#define __USE_XOPEN
#include <time.h>
#ifndef _WIN32
@@ -32,5 +33,6 @@
FREERDP_API uint64 freerdp_windows_gmtime();
FREERDP_API uint64 freerdp_get_windows_time_from_unix_time(time_t unix_time);
FREERDP_API time_t freerdp_get_unix_time_from_windows_time(uint64 windows_time);
FREERDP_API time_t freerdp_get_unix_time_from_generalized_time(const char* generalized_time);
#endif /* FREERDP_TIME_UTILS_H */

View File

@@ -32,9 +32,6 @@
#include <sys/socket.h>
#endif
#define __USE_XOPEN
#include <time.h>
#include "kerberos.h"
#include "kerberos_crypto.h"
#include "kerberos_encode.h"
@@ -43,6 +40,7 @@
#include <freerdp/sspi/sspi.h>
#include <freerdp/utils/tcp.h>
#include <freerdp/utils/time.h>
#include <freerdp/utils/blob.h>
#include <freerdp/utils/print.h>
#include <freerdp/utils/memory.h>
@@ -90,22 +88,20 @@ char* get_utc_time(time_t t)
{
char* str;
struct tm* utc;
if(t == 0)
if (t == 0)
t = time(NULL);
utc = gmtime(&t);
str = (char*)xzalloc(16);
str = (char*) xzalloc(16);
strftime(str, 16, "%Y%m%d%H%M%SZ", utc);
return str;
}
time_t get_local_time(char* str)
{
time_t t;
struct tm utc;
memset(&utc, 0, sizeof(struct tm));
strptime(str, "%Y%m%d%H%M%SZ", &utc);
t = timegm(&utc);
return t;
return freerdp_get_unix_time_from_generalized_time(str);
}
uint32 get_clock_skew(char* str)

View File

@@ -44,3 +44,34 @@ time_t freerdp_get_unix_time_from_windows_time(uint64 windows_time)
unix_time = (windows_time - 621355968000000000ULL) / 10000000;
return unix_time;
}
time_t freerdp_get_unix_time_from_generalized_time(const char* generalized_time)
{
int Y, m, d;
int H, M, S;
struct tm gt;
time_t unix_time = 0;
/*
* GeneralizedTime:
*
* 12th November 1997 at 15:30:10,5 PM
*
* "19971112153010.5Z"
* "19971112173010.5+0200"
*/
memset(&gt, 0, sizeof(struct tm));
sscanf(generalized_time, "%4d%2d%2d%2d%2d%2d", &Y, &m, &d, &H, &M, &S);
gt.tm_year = Y - 1900; /* Year since 1900 */
gt.tm_mon = m; /* Months since January */
gt.tm_mday = d; /* Day of the month */
gt.tm_hour = H; /* Hour since midnight */
gt.tm_min = M; /* Minutes after the hour */
gt.tm_sec = S; /* Seconds after the minute */
unix_time = mktime(&gt);
return unix_time;
}