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

Home Posts Topics Members FAQ

UTC string -> time_t

Hi,

this is probably quite easy...

How do I convert a UTC string into a time_t?

eg. "2007-12-18 13:37:26" - a time_t.

Now FAQ 13.3 says use mktime() to convert a struct tm into
a time_t or you have to use some non-standard thing to parse the
string.
Assume I can parse the string and build a struct tm, how can I turn
it
into a time_t?

mktime() works on *local time* so it is going to apply the timezone
change
(the incoming string may have come from another timezone).

--
Nick Keighley
Dec 18 '07 #1
7 6033
On 18 Dec, 13:41, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
Hi,

this is probably quite easy...

How do I convert a UTC string into a time_t?

eg. "2007-12-18 13:37:26" - a time_t.

Now FAQ 13.3 says use mktime() to convert a struct tm into
a time_t or you have to use some non-standard thing to parse the
string.
Assume I can parse the string and build a struct tm, how can I turn
it
into a time_t?

mktime() works on *local time* so it is going to apply the timezone
change
(the incoming string may have come from another timezone).
to save anyone the trouble of answering it looks like the way to
go is to calculate the difference between local time and UTC
by using difftime() on the results of localtime() and gmtime().
Adjusting the input value in struct tm then using mktime().

eek!
--
Nick Keighley

Dec 18 '07 #2
Nick Keighley wrote:
On 18 Dec, 13:41, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
>Hi,

this is probably quite easy...

How do I convert a UTC string into a time_t?

eg. "2007-12-18 13:37:26" - a time_t.

Now FAQ 13.3 says use mktime() to convert a struct tm into
a time_t or you have to use some non-standard thing to parse the
string.
Assume I can parse the string and build a struct tm, how can I turn
it
into a time_t?

mktime() works on *local time* so it is going to apply the timezone
change
(the incoming string may have come from another timezone).

to save anyone the trouble of answering it looks like the way to
go is to calculate the difference between local time and UTC
by using difftime() on the results of localtime() and gmtime().
Adjusting the input value in struct tm then using mktime().
difftime() operates on time_t values, not on the
struct tm values produced by localtime() and mktime().
Still, you can inspect the elements of the struct tm
objects to get the offset in minutes.

The tricky part is that the difference varies with,
er, time. You can eliminate the seasonal variation (if
any) by setting the tm_isdst member to zero, but that
still leaves statutory variations unaccounted for. For
example, the Venezuela-to-UTC offset today is not the
same as it was two weeks ago. (True, the computer
systems' time services may not have caught up with the
change yet, but once they do they should give different
UTC offsets for 2007-12-08 and 2007-12-10.)

The only fully-portable approach I can think of is
to use both localtime() and gmtime() on the same time_t
value to get an initial estimate of the offset. Then
build a struct tm with the desired time, fudge it by the
estimated offset, call mktime() to get a time_t, and run
that time_t through gmtime(). If the result matches the
(un-fudged) original, you're done. Otherwise, adjust
the offset and try again.

It's a Darn Shame the standard library does not
include mkgmtime().

--
Er*********@sun .com
Dec 18 '07 #3
Eric Sosman said:

<snip>
It's a Darn Shame the standard library does not
include mkgmtime().
<shrugI just use mktime, which does precisely the same thing as far as
I'm concerned. :-)

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dec 18 '07 #4
Richard Heathfield wrote:
Eric Sosman said:

<snip>
>It's a Darn Shame the standard library does not include mkgmtime().

<shrugI just use mktime, which does precisely the same thing as
far as I'm concerned. :-)
Works for anyone. Set your system time to GMT. If you don't live
there you can add or subtract some number. It doesn't require
values larger than 13 (or 24 for the result).

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Dec 20 '07 #5
CBFalconer wrote:
Richard Heathfield wrote:
>Eric Sosman said:

<snip>
>>It's a Darn Shame the standard library does not include mkgmtime().
<shrugI just use mktime, which does precisely the same thing as
far as I'm concerned. :-)

Works for anyone. Set your system time to GMT. If you don't live
there you can add or subtract some number. It doesn't require
values larger than 13 (or 24 for the result).
However, the method for setting the system time to GMT is
implementation-defined.
Dec 20 '07 #6
James Kuyper said:

<snip>
7.23.1p1:
"The local time zone and Daylight Saving Time are
implementation-defined."

If the local time zone is settable, I don't see how the implementation
could properly define it without describing how to set it.
Borland's Turbo C conformance docs read, for the C90 equivalent of the
above: "Defined as local PC time and date."

Perfectly adequate definition, yet with no mention whatsoever of how to set
the time zone.
If it's not settable, then it's a non-issue.
I think it's a non-issue anyway. :-)

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dec 20 '07 #7
On Dec 18, 5:41*am, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
Hi,

this is probably quite easy...

How do I convert a UTC string into a time_t?

eg. "2007-12-18 13:37:26" *-*a time_t.

Now FAQ 13.3 says use mktime() to convert a struct tm into
a time_t or you have to use some non-standard thing to parse the
string.
Assume I can parse the string and build a struct tm, how can I *turn
it
into a time_t?

mktime() works on *local time* so it is going to apply the timezone
change
(the incoming string may have come from another timezone).
I recommend:
http://www.twinsun.com/tz/tz-link.htm

The PostgreSQL code base is Berkeley licensed and has date/time
manipulation routines to do just about everything you can imagine.
All C too (no C++ or assembly).
http://www.postgresql.org/
They have a version of libtz incorporated into PostgreSQL directly,
also.
Dec 21 '07 #8

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

Similar topics

16
6757
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site... this number is always changing as I have hundreds of thousand of pages of text on my site. Problem: - in my opinion this just not only look weird, but the variable "n" (number)
5
31184
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it will split myString up using the delimiter of 1 space so that
9
8006
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
10
8198
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
2
4787
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
4327
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
2
3181
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was a tasty burger"
15
50272
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
3076
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove spaces, but not in the inner string(\"and a inner string\\\"). Will anyone please tell me how to do this?
8
4745
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and add the unique words in the string to a map. Eg:
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.