473,804 Members | 3,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading local time into time_t

I have strings like "2006-03-26 21.51" which I would like to convert into
time_t. I know that the string is in localtime.

So far I've written the following code:

time_t LocalTimeFromSt ring(string str)
{
struct tm t;
memset(&t, 0, sizeof(t));

if (str.length() != 16)
return 0;

sscanf(str.c_st r(), "%04d-%02d-%02d %02d:%02d",
&t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);

t.tm_year -= 1900;
t.tm_mon -= 1;

return mktime(&t);
}

However it seems that time_t must be in UTC time. If I would use ctime() on
the time_t result I don't get the right result back.

How would I go about adjusting the result for localtime? The solution must
be platform independent since it's suppose to work on both Unix and Windows.

Thanks in advance.

-- Henrik
Mar 26 '06 #1
8 12344
Henrik Goldman wrote:
I have strings like "2006-03-26 21.51" which I would like to convert into
time_t. I know that the string is in localtime.

So far I've written the following code:

time_t LocalTimeFromSt ring(string str)
{
struct tm t;
memset(&t, 0, sizeof(t));

if (str.length() != 16)
return 0;

sscanf(str.c_st r(), "%04d-%02d-%02d %02d:%02d",
&t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);

t.tm_year -= 1900;
t.tm_mon -= 1;

return mktime(&t);
}

However it seems that time_t must be in UTC time. If I would use ctime() on
the time_t result I don't get the right result back.

How would I go about adjusting the result for localtime? The solution must
be platform independent since it's suppose to work on both Unix and Windows.

Thanks in advance.

-- Henrik

Check out the date/time library from boost to do this.

It's one thing to do this as a learning excercise but since you say it
needs to be platform independent I suspect that this is not a learning
project.

Mar 26 '06 #2
> Check out the date/time library from boost to do this.

Thanks for the suggestion but unfortunatly not usable in my case. Since it's
a library that I write I cannot put boost into it. There are too many
requirements and obstacles to make this into a solution, unfortunatly.
It's one thing to do this as a learning excercise but since you say it
needs to be platform independent I suspect that this is not a learning
project.


True. I guess it should be possible to solve by looking at the environment
variables for daylight adjustment and timezone but I doubt I'm the first one
to have come into this problem. Therefore I thought someone else could
perhaps point me to a website that shows such a solution.

Thanks in advance.
-- Henrik
Mar 26 '06 #3

Henrik Goldman wrote:
Check out the date/time library from boost to do this.


Thanks for the suggestion but unfortunatly not usable in my case. Since it's
a library that I write I cannot put boost into it. There are too many
requirements and obstacles to make this into a solution, unfortunatly.
It's one thing to do this as a learning excercise but since you say it
needs to be platform independent I suspect that this is not a learning
project.


True. I guess it should be possible to solve by looking at the environment
variables for daylight adjustment and timezone but I doubt I'm the first one
to have come into this problem. Therefore I thought someone else could
perhaps point me to a website that shows such a solution.

Thanks in advance.
-- Henrik

Why can't you "put" boost into a library you write? Who or what
is putting this constraint on you?

Mar 26 '06 #4
Henrik Goldman wrote:
I have strings like "2006-03-26 21.51" which I would like to convert into
time_t. I know that the string is in localtime.

So far I've written the following code:

time_t LocalTimeFromSt ring(string str)
{
struct tm t;
memset(&t, 0, sizeof(t));

if (str.length() != 16)
return 0;

sscanf(str.c_st r(), "%04d-%02d-%02d %02d:%02d",
&t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);

t.tm_year -= 1900;
t.tm_mon -= 1;

return mktime(&t);
}

However it seems that time_t must be in UTC time. If I would use ctime() on
the time_t result I don't get the right result back.

How would I go about adjusting the result for localtime? The solution must
be platform independent since it's suppose to work on both Unix and Windows.

Thanks in advance.

-- Henrik

A quick look at the various time functions yields this algorithm:

Just adjust your time_t result returned from mktime() by the time
difference between local and UTC time. This difference is fixed except
when you cross into (or out of) daylight savings time.

You can get the UTC time and local time using gmtime() and
localtime() functions (both convert time_t into struct tm). Now use
mktime() function to convert these struct tms into time_ts. And then
use difftime() to get the actual difference. You should be able to use
this difference to adjust your time_t values.

Mar 26 '06 #5
> You can get the UTC time and local time using gmtime() and
localtime() functions (both convert time_t into struct tm). Now use
mktime() function to convert these struct tms into time_ts. And then
use difftime() to get the actual difference. You should be able to use
this difference to adjust your time_t values.


Thanks. This sounds like a good idea. Which time do you suggest I should use
those first functions on? I guess I should use the un-adjusted time_t,
right?

Thanks.
-- Henrik
Mar 27 '06 #6
> Why can't you "put" boost into a library you write? Who or what
is putting this constraint on you?


It's a static library that I'm creating. In other words it's linked at the
client site together with code that I have no control over.
This means if they use boost as well then there will eventually be trouble
(unless I would rename the namespace so I get my own "version" of boost).
Besides this it adds extra code in there which is not a good idea (esp.
since it's planned to be ported to embedded OS's in future). Last but not
least it makes it harder to port the code for new compilers. I will only be
able to compile my code if boost supports the platform. I don't say it's a
realistic problem since I know they do alot of platform support, but still
it's something that I need to worry about.

Those requirements are what users expect of my library, not my own ideas.

-- Henrik
Mar 27 '06 #7
Henrik Goldman wrote:
You can get the UTC time and local time using gmtime() and
localtime() functions (both convert time_t into struct tm). Now use
mktime() function to convert these struct tms into time_ts. And then
use difftime() to get the actual difference. You should be able to use
this difference to adjust your time_t values.


Thanks. This sounds like a good idea. Which time do you suggest I should use
those first functions on? I guess I should use the un-adjusted time_t,
right?


I went through this mess a couple of years ago.
You have to make the calls to localtime and gmtime every time, though.

Consider if you take the time differential at the start of your program,
and during your run, daylight savings time starts or ends (possible if
you have a program designed to run for days at a time). Then your time
differential is incorrect after a while. So you need to perform get the
local and gmtime every time you wish to convert a time_t struct. I
personally believe that there should have been a mkgmtime() as the
inverse of gmtime().
Mar 27 '06 #8
red floyd <no*****@here.d ude> wrote:
Consider if you take the time differential at the start of your program,
and during your run, daylight savings time starts or ends (possible if
you have a program designed to run for days at a time). Then your time
differential is incorrect after a while. So you need to perform get the
local and gmtime every time you wish to convert a time_t struct.


I'm not sure if DST is standardized, but if it is, you could cache the
value and only update if you see a date that is across the DST status
change.

Well, I just checked and a struct tm does have a field tm_isdst, so
maybe this could be queried to see if you need to update the correction
factor.

--
Marcus Kwok
Mar 28 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3111
by: Michael B Allen | last post by:
Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? For example; void fn(struct foo *f, int bar) { struct abc d; int i;
4
4777
by: Ekong, Samuel Akpan | last post by:
Hi, I am parsing a binary file and intend to read a date(not time) field encoded thus: 0xA1290B(little-endian). Now I know the date to be 12/04/2003 but just cannot get any of the known datetime functions to return this value either as a tm structure or as a string value. It's more accurate to say that my attempts return a date in the year 1970. I have been able to read the time accurately but I just want to be able to parse correctly...
14
2506
by: George | last post by:
In Time.h there is a structure defined for time settings. I'm building an embedded system that has a Real Time Clock but it's not PC compatible. My question is: I don't some elements of the structure such as day of the week or day of the year, but I do know year,month,date,hour,min,sec. Does the language support filling in the missing elements. And is there a consistency check for that time structure.
6
4933
by: leorulez | last post by:
I was wondering if there is any way in C to read the date and time (either system time or from the keyboard) and see if it falls between certain date and time? I am not sure how to compare the 2 entries of date/time. Any help on this would be great. Thanks
1
5661
by: Generic Usenet Account | last post by:
I am trying to find the best way to get the difference between UTC and the local time? Here's a small code snippet that I have written for this purpose. Is there a better way to do this? Thanks, Bala /*************************************************************************/ time_t getTimeOffset() {
18
3123
by: moni | last post by:
I have 2 time values: System time and an input from the user. 1) System time is in the form of seconds from 1/1/1970 calculated by using
2
18284
by: ZR | last post by:
Hello, I need to convert a GMT time string to local time. I can fill out a "tm" structure with the GMT time string. Are there any standard C (or OS) time functions that will allow me to do this? "subtracting ### hours depending on the locale" is not an option because I need this to work on any machine anywhere in the world, unless there is a way to get the ### (from OS or machine using a standard C or C++ system function calls).
4
2833
by: Seth7TS | last post by:
Hi, I want to make a trial version of a software but if i read the system clock the user can modify it... how can i prevent this?? i thought to read the time from internet but i don't know if there are api to do this... thanks everybody Seth
5
8530
by: Grey Alien | last post by:
I need to convert timestamps that are given as the number of seconds that have elapsed since midnight UTC of January 1, 1970, (not counting leap seconds). It seems all of the std C functions expect positive offsets from this date and are incapable of working on dates preceeding the epoch (i.e. negative offsets) - which IMHO shows a remarkable lack of foresight - and is *just* a little bit annoying. Does anyone know of an algo I can...
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.