From f7e077a7c53fab6b54a4364d47169194b7f13909 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Tue, 29 Aug 2023 15:02:37 +0200 Subject: [PATCH] add a TimeZone::PrintOffset(...) method --- winixd/core/timezone.cpp | 41 +++++++++++++++++++++++++++++++++++++++- winixd/core/timezone.h | 5 +++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/winixd/core/timezone.cpp b/winixd/core/timezone.cpp index 74253aa..73ba18c 100644 --- a/winixd/core/timezone.cpp +++ b/winixd/core/timezone.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2012-2021, Tomasz Sowa + * Copyright (c) 2012-2023, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -166,6 +166,45 @@ return local; } +void TimeZone::PrintOffsetPart(long val, pt::Stream & str) +{ + if( val < 10 ) + { + str << '0'; + } + + str << val; +} + + +void TimeZone::PrintOffset(time_t offset, pt::Stream & str) +{ + bool has_sign = false; + + if( offset < 0 ) + { + has_sign = true; + offset = 0 - offset; + } + + time_t days = offset / 60 / 60 / 24; + + if( days == 0 ) + { + str << ((has_sign) ? '-' : '+'); + time_t diff = offset - days * 60 * 60 * 24; + + long hour = int(diff / 60 / 60); + long min = int((diff - hour * 60 * 60) / 60); + + PrintOffsetPart(hour, str); + str << ':'; + PrintOffsetPart(min, str); + } +} + + + time_t TimeZone::CalcUTCOffset(const pt::Date & local_date) { time_t dst_offset = 0; diff --git a/winixd/core/timezone.h b/winixd/core/timezone.h index 21a7b29..47c7580 100644 --- a/winixd/core/timezone.h +++ b/winixd/core/timezone.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2012-2014, Tomasz Sowa + * Copyright (c) 2012-2023, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -115,7 +115,7 @@ public: time_t CalcLocalOffset(const pt::Date & utc_date); time_t ToLocal(time_t utc_time); pt::Date ToLocal(const pt::Date & utc_date); - + static void PrintOffset(time_t offset, pt::Stream & str); // prints the offset as [+|-]HH:MM:SS e.g.: +02:00:00 /* converting from local time to UTC @@ -157,6 +157,7 @@ private: time_t ParseStrOffset(const wchar_t * str); time_t GetOffset(pt::Space & space); bool SetTzDst(pt::Space & year); + static void PrintOffsetPart(long val, pt::Stream & str); };