473,473 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

tm_yday to time_t?

bob

If I have a year and the day of the year (e.g. yyyy.ddd), is there
some simple way to convert this to a time_t? mktime(), of course, does
just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.

There's the obvious brute force approach of subtracting 31, 28, 31,
30, etc... until you arrive at the right result (being careful to allow
for leap years, of course), but it seems like there ought to be some
standard library routine for this purpose.

It only has to work for "modern" days, post 1970.

Thanks,
Bob Armstrong

Jun 30 '06 #1
7 8652
On 30 Jun 2006 15:14:32 -0700, bo*@jfcl.com wrote:

If I have a year and the day of the year (e.g. yyyy.ddd), is there
some simple way to convert this to a time_t? mktime(), of course, does
just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.

There's the obvious brute force approach of subtracting 31, 28, 31,
30, etc... until you arrive at the right result (being careful to allow
for leap years, of course), but it seems like there ought to be some
standard library routine for this purpose.

It only has to work for "modern" days, post 1970.


Google for "serial date" or "Julian date". You should find algorithms
that can be adapted. I'd say more, but I'm out of here until next
Wednesday!

--
Al Balmer
Sun City, AZ
Jun 30 '06 #2
bo*@jfcl.com writes:
If I have a year and the day of the year (e.g. yyyy.ddd), is there
some simple way to convert this to a time_t? mktime(), of course, does
just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.

There's the obvious brute force approach of subtracting 31, 28, 31,
30, etc... until you arrive at the right result (being careful to allow
for leap years, of course), but it seems like there ought to be some
standard library routine for this purpose.

It only has to work for "modern" days, post 1970.


So, you have the year and the day of the year and you want to get the
right year, month and day (or the right time_t value based on those
values)?

mktime() need a pointer to a struct tm because it modifies it's
argument, meaning that it normalizes your date. For example, the
following program:

<code>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {

struct tm mytm;

mytm.tm_year=2006-1900;
mytm.tm_mon=0;
mytm.tm_mday=365;
mytm.tm_isdst=-1;
mytm.tm_hour=0;
mytm.tm_min=0;
mytm.tm_sec=0;

if(mktime(&mytm)==-1) {
printf("I cannot represent the calendar time :-(\n");
exit(EXIT_FAILURE);
}

printf("%d\t%d\t%d\n",mytm.tm_year+1900,mytm.tm_mo n,mytm.tm_mday);

return 0;
}
</code>

Will output
2006 11 31
(if mktime can represent the time).

The values of the 6 fields that matter for mktime (I don't count
"dst" here, now) don't have to be in the specified ranges and mktime
will change it's argument by setting the components to the correct
values. This means that if you say year=106, mon=0 (Jan) mday=32 (1
more than the max number), hour=0, min=0 and sec=0 the values that
exceed the ranges will be added (or substracted) to the higher group
i.e. mday=32 means 31 day in January + 1 the next month which will
make mon=1 and leave everything else unchanged.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 1 '06 #3
bob

Nelu wrote:
mytm.tm_mday=365;
....


That is just extraordinarily clever! And it actually works, too :-)

I knew there was some easier way to do it.

Thanks,
Bob

Jul 1 '06 #4
Nelu wrote:
bo*@jfcl.com writes:
If I have a year and the day of the year (e.g. yyyy.ddd), is there
some simple way to convert this to a time_t? mktime(), of course, does
just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.

There's the obvious brute force approach of subtracting 31, 28, 31,
30, etc... until you arrive at the right result (being careful to allow
for leap years, of course), but it seems like there ought to be some
standard library routine for this purpose.

It only has to work for "modern" days, post 1970.


So, you have the year and the day of the year and you want to get the
right year, month and day (or the right time_t value based on those
values)?

mktime() need a pointer to a struct tm because it modifies it's
argument, meaning that it normalizes your date. For example, the
following program:

<code>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {

struct tm mytm;

mytm.tm_year=2006-1900;
mytm.tm_mon=0;
mytm.tm_mday=365;
mytm.tm_isdst=-1;
mytm.tm_hour=0;
mytm.tm_min=0;
mytm.tm_sec=0;

if(mktime(&mytm)==-1) {
printf("I cannot represent the calendar time :-(\n");
exit(EXIT_FAILURE);
}

printf("%d\t%d\t%d\n",mytm.tm_year+1900,mytm.tm_mo n,mytm.tm_mday);

return 0;
}
</code>

Will output
2006 11 31
(if mktime can represent the time).

The values of the 6 fields that matter for mktime (I don't count
"dst" here, now) don't have to be in the specified ranges and mktime
will change it's argument by setting the components to the correct
values. This means that if you say year=106, mon=0 (Jan) mday=32 (1
more than the max number), hour=0, min=0 and sec=0 the values that
exceed the ranges will be added (or substracted) to the higher group
i.e. mday=32 means 31 day in January + 1 the next month which will
make mon=1 and leave everything else unchanged.

November 31 ?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 1 '06 #5
Joe Wright <jo********@comcast.net> writes:
Nelu wrote:
bo*@jfcl.com writes:
If I have a year and the day of the year (e.g. yyyy.ddd), is there
some simple way to convert this to a time_t? mktime(), of course, does
just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.

There's the obvious brute force approach of subtracting 31, 28, 31,
30, etc... until you arrive at the right result (being careful to allow
for leap years, of course), but it seems like there ought to be some
standard library routine for this purpose.

It only has to work for "modern" days, post 1970.

So, you have the year and the day of the year and you want to get the
right year, month and day (or the right time_t value based on those
values)?
mktime() need a pointer to a struct tm because it modifies it's
argument, meaning that it normalizes your date. For example, the
following program:
<code>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void) {
struct tm mytm;
mytm.tm_year=2006-1900;
mytm.tm_mon=0;
mytm.tm_mday=365;
mytm.tm_isdst=-1;
mytm.tm_hour=0;
mytm.tm_min=0;
mytm.tm_sec=0;
if(mktime(&mytm)==-1) {
printf("I cannot represent the calendar time :-(\n");
exit(EXIT_FAILURE);
}
printf("%d\t%d\t%d\n",mytm.tm_year+1900,mytm.tm_mo n,mytm.tm_mday);
return 0;
}
</code>
Will output 2006 11 31
(if mktime can represent the time).
The values of the 6 fields that matter for mktime (I don't count
"dst" here, now) don't have to be in the specified ranges and mktime
will change it's argument by setting the components to the correct
values. This means that if you say year=106, mon=0 (Jan) mday=32 (1
more than the max number), hour=0, min=0 and sec=0 the values that
exceed the ranges will be added (or substracted) to the higher group
i.e. mday=32 means 31 day in January + 1 the next month which will
make mon=1 and leave everything else unchanged.

November 31 ?


The month is 0 based. (0 - January, 11 - December). I set
mytm.tm_mon=0. If you complain about 11 why don't you complain about
0? :-)

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 1 '06 #6
Nelu <sp*******@gmail.com> writes:
Joe Wright <jo********@comcast.net> writes:
Nelu wrote:
[snip]

November 31 ?


The month is 0 based. (0 - January, 11 - December). I set
mytm.tm_mon=0. If you complain about 11 why don't you complain about
0? :-)


Then again... scratch the last two sentences. Sorry. mday=365 is also out
of range for day in month so it could've been interpreted that way for
the month, too. I should've put tm_mon+1 instead on tm_mon in printf
so it won't lead to confusion. I hope the OP got it right.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 1 '06 #7
Nelu wrote:
Nelu <sp*******@gmail.com> writes:
Joe Wright <jo********@comcast.net> writes:
Nelu wrote:
[snip]
November 31 ?

The month is 0 based. (0 - January, 11 - December). I set
mytm.tm_mon=0. If you complain about 11 why don't you complain about
0? :-)


Then again... scratch the last two sentences. Sorry. mday=365 is also out
of range for day in month so it could've been interpreted that way for
the month, too. I should've put tm_mon+1 instead on tm_mon in printf
so it won't lead to confusion. I hope the OP got it right.

Well, I apologize. tm_mon is indeed 0..11 (12 months) and tm_yday is
0..365 (366 days).

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 1 '06 #8

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

Similar topics

1
by: Anand CS | last post by:
Hi All I have question regarding time data structures... I have 64 bit unsigned microsecond resolution (unsigned __int64 for windows/and unsigned long long for others) variable. It stores the...
6
by: j0mbolar | last post by:
i've read recently that time_t doesn't have to be in second resolution but if it isn't, then what resolution could it be in? microseconds, milliseconds, are all valid? also, does anyone know...
8
by: Ian Pilcher | last post by:
When adding two values of type time_t, how can I check for overflow? Maybe I'm just brain-cramped today, but I can't figure out how to do it. Thanks! --...
0
by: Zwyatt | last post by:
I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum; time_t aTime; }
0
by: Zwyatt | last post by:
having a really weird little bug w/ time_t...check it out: I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum;
8
by: Henrik Goldman | last post by:
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...
45
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error,...
7
by: Nick Keighley | last post by:
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...
8
by: Abubakar | last post by:
Hi, Lets say I have three integers (date, month, and year) as part of a struct lets say like struct mydate{ int day, month, year}; I want to add x number of days to this date. And it should be a...
0
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,...
1
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...
0
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.