Gossamer Forum
Home : Products : Others : Gossamer Community :

Date convert

Quote Reply
Date convert
Anyone with a quick Global for Date/Time for use in Communities (I prefer to set the format manually)

How can I convert the outout of:
<%Community::Web::Tools::convert_date($comm_created)%>

To %mmmm% %dddd% %yyyy% etc..


Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Date convert In reply to
Hi,

You could use GT::Date for this:

<%GT::Date::date_get($comm_created, "%mmmm% %dddd% %yyyy%")%>

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Date convert In reply to
Thanks Alex,

How do I get current date and time (like the two globals in Links - they are not working as they are in Links)..

I used the below to get the date and time - but can't seem to format it the same way as you suggested earlier..?!?

sub {
my ($date) = scalar(localtime);
return $date;
}



Cheers
Klaus

http://www.ameinfo.com

Last edited by:

klauslovgreen: Jan 21, 2003, 12:27 PM
Quote Reply
Re: [klauslovgreen] Date convert In reply to
You could try:

<%GT::Date::date_get(0, "%mmmm% %dddd% %yyyy%")%>

to display the current time.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Date convert In reply to
Perfect!

Now if I wanted to show another time zone? Could I do that by adding +x or?

Thanks for your help
Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Date convert In reply to
You could check GT::Date documentation...

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
How can I translate months and days in my language? I must open GT libraries? What about the all translateable vars in gt apps? Unsure
Max
The one with Mac OS X Server 10.4 :)
Quote Reply
Re: [maxpico] Date convert In reply to
Well you can change date language texts in Admin config.

However you are right, the support for template based, full language support of date language texts is still MISSING. Frown
I would also welcome this improvement of GT::Date module... Not a big task, tough. I believe.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
What do you mean by Admin config?
Max
The one with Mac OS X Server 10.4 :)
Quote Reply
Re: [maxpico] Date convert In reply to
Go Admin/Setup/Date Options.
There are Date text options: date_month_long, date_month_short, date_days_long, date_days_short

Unfortunately Date text options can not changed for each template, in language.txt.
This means if you have a Spanish, French, English templates, you can display your date texts in one language only, e.g in English.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
I did read the documentation (perhaps I just did not understand what I was reading) - but I did not find a way to add/deduct hours - I can see how to show a date 7 days ago.. but not hours..

Anyway - I'll try to find a way.. thanks!

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Date convert In reply to
Is this what you were looking for?
From GT::Date docs: %o% + or - GMT offset

Hope this helps.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
Yes I tried that and it show GMT -+ - but what I want is to add to the actual time displayed - not just get the GMT offset

Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Date convert In reply to
Probably the following function may help: date_transform ($date, $orig_fmt, $new_fmt);
And use the %o% in both formats.

I hope GT::Date can treat this conversion and will return the date in correct format...
Give it a try, you can't lose anything. Wink

I hope it will work.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [klauslovgreen] Date convert In reply to
If you can get the date into timestamp format, you can then calculate the number of seconds to add/subtract to find your new timezone...eg...

my $time = time; # GMT for me.

...New York is 5 hours behind so to find their time/date I'd do:

my $nyc = time - (60 * 60 * 5);

Then you can format the date as you wish.

Is that what you were looking for?
Quote Reply
Re: [Paul] Date convert In reply to
That's it!
Thanks Paul

Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Date convert In reply to
It's clear now, what you did need.
Paul gave you a correct solution.

As for the GT::Date module, I aggree, it would be fine to be able to do second, hour, month, year based operations, too, in addition to currently available day operations. Currently a possible workaround is, to convert a date to unix time, do operations, and convert back.

Following functions could be implemented:
- date_add_second, date_add_hour, date_add_month, date_add_year
- date_sub_second, date_sub_hour, date_sub_month, date_sub_year
- date_diff_second, date_diff_hour, date_diff_month, date_diff_year (e.g. result of date_diff_month will be a rounded month number)
Would not be too difficult to have these functions implemented.

Example for date_sub_hour function:
E.g.: $date = date_sub_hour ("2003-01-23 01:05:01 +0100", 3 ); # This would subtract 3 hour from date, and would result "2003-01-22 22:05:01 +0100".
Ok. These were my suggestions.

I also have a question, related to GMT dates. It's not clear how is possible to treat GMT based dates, using GT::Date functions. The docs unfortunately does not give examples about this.
I know & use several functions of GT::Date, but there are some undocumented features, and some are not documented well enough. I spent a few hours today to check how the features in GT::Date works, but GMT dates are not clear, yet.

Anybody has experience with GT::Date, in particular with GMT date usage?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
Perhaps you'd care to show some example code of how you'd minus 3 hours from the following string...

Code:
2003-01-23 01:05:01 +0100

I wouldn't like to take the challenge Crazy

Last edited by:

Paul: Jan 22, 2003, 5:36 PM
Quote Reply
Re: [Paul] Date convert In reply to
Not sure what you want to say. Could you clarify?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
Welp, you said...

Quote:
Would not be too difficult to have these functions implemented.

So I thought perhaps you could show some simple code that will do it.

Last edited by:

Paul: Jan 22, 2003, 5:44 PM
Quote Reply
Re: [Paul] Date convert In reply to
I will not post exact code right now, but I will show the logic how would I do it.
And of course I would also use existing GT::Date functions.
So, we have that date:
2003-01-23 01:05:01 +0100
And we want to subtract 3 hours, and display the result.

Solution A:
0) We input 2 values: a) date string b) output date format
1) Use parse_format - Takes a string and a date format and returns an array ref of the first 7 arguments returned by localtime().
We can use to have a date array.
Ok. we now have a date array.
2) We subtract 3 from 3rd array element (this is the hour).
3) if the array element would go below 0 because the subtract, we decrease the day, too. We have to take care of month, year changes if the subtract affects them.
So we have the correct date in the array.
4) Use format_date - Takes an array from localtime or equiv and a date format and returns date.
We can use that function to convert date array to formatted date. We use the second input value if it was given.
5) return result date


Solution B:
0) We input 2 values: a) date string b) output date format
1) Use _date_str_to_time - Takes a date string and converts it to a unix time.
Ok. We now have the date in unix time.
2) We subtract 60 * 60 * 3 from the unix time.
3) Use date_get - Return today's date or a date from a time() that you pass in.
So we use the second feature, that we give input, and we get output in date string format
4) return result date


I gave you 2 solutions. The second is the best of them.
Was that difficult?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
Quote:
Was that difficult?

An explanation is not difficult of course :)
Quote Reply
Re: [Paul] Date convert In reply to
Would you mean to insinuate that I could not program it?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
In post 18 I said:

Quote:
I wouldn't like to take the challenge

My point is that you said it wasn't too difficult and so I thought perhaps you could show us how it was done. I've never seen you post any code so I was trying to encourage you to do so to see what solution you came up with.

To modify a date string is really not that easy at all (without using a pre-made module) so by asking you to write some code I thought perhaps I could make you realise that without saying outright.

However you didn't take the challenge :)
Quote Reply
Re: [Paul] Date convert In reply to
Paul,
Of course I will not take the challenge, because
1) I have a lot other things to work on
2) It's not my job to do improvements on the GT::Date. I gave the suggestions & for your request I gave the synopse how can be done
3) I wrote a script for date format change (convert_date.pl) 3 years ago, does the same what GT::Date does. My script does not know so much, as GT::Date, is not module based, but converts dates correctly from any format to any format, to unix time and from unix time back to chosen format, also can display date languages in many language, including Hungarian, German, English, French, Russian, Spanish, and probably more automatically, just by setting the correct locale.
convert_date.pl v1.00 (was finished on 03.01.2000)
convert_date.pl v2.0beta3 (the last version was modified on 07.30.2001)


So I don't want, don't need, and don't have to prove anything about my programming knowledge, especially not for you.
I'm not professional perl programmer, and probably I don't know so much like a professional, because I don't program perl in full time. It's my hobby.
But I refuse your doubt about my programming knowledge!!!

Are you provocative, aren't you?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
Quote:
So I don't want, don't need, and don't have to prove anything about my programming knowledge, especially not for you.
I'm not professional perl programmer, and probably I don't know so much like a professional, because I don't program perl in full time. It's my hobby.
But I refuse your doubt about my programming knowledge!!!

Are you provocative, aren't you?

I think you have totally missed the intention of my posts. I never asked you to prove anything or doubted your skills, I asked to see how you would tackle the challenge. I was interested in how you would go about it.

All you had to say was you don't have time or don't want to. Why you have to make a big drama out of everything I don't know.
Quote Reply
Re: [Paul] Date convert In reply to
Quote:
I was interested in how you would go about it.
If you would, then you would satisfy the 2 algorythms, I gave you.

Quote:
I thought perhaps I could make you realise that without saying outright.
However you didn't take the challenge :)
I think this was provocative, and based on my English knowledge, I feel, it questioned my abilities, and was ironical, I felt..

If you did not wrote with that intention, then my apologies, but I doubt, that was intended to be a straight message... IMHO.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Date convert In reply to
du calme... Tongue

Webmaster33 can I have your script? Is it free?
This would be very useful... If I had that script before... Smile
Thanks
Max
The one with Mac OS X Server 10.4 :)
Quote Reply
Re: [webmaster33] Date convert In reply to
Lets not drag this on further than necessary :)

I explained my intentions, if you are accusing me of lying then that's your perogative.

Edit: Oh well perogative must be spelt wrong as I can't find it in the dictionary Wink

Last edited by:

Paul: Jan 23, 2003, 6:06 AM
Quote Reply
Re: [Paul] Date convert In reply to
prerogative Wink

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] Date convert In reply to
Right you are!

I would have never guessed at that r being there Blush
Quote Reply
Re: [webmaster33] Date convert In reply to
In Reply To:
Solution B:
0) We input 2 values: a) date string b) output date format
1) Use _date_str_to_time - Takes a date string and converts it to a unix time.
Ok. We now have the date in unix time.
2) We subtract 60 * 60 * 3 from the unix time.
3) Use date_get - Return today's date or a date from a time() that you pass in.
So we use the second feature, that we give input, and we get output in date string format
4) return result date

Now, now, let's calm down.

The second solution above certainly seems the most logical, but in fact it is not the most logical in terms of how most people think. Both solutions, in fact, as well as the date_add and date_sub functions in GT::Date, have what seems to be quite a significant flaw - basically, they oversimplify what a "day", "month", or "year" means.

For example, 3 hours before 03:30 is usually 00:30, but could be either 23:30 or 01:30. The second way does a better job due to the fact that unix times are time-zone and daylight-savings independant.

Consider the following two times:

1004233600
1004320000

They are 86400 seconds apart - 24 hours, in other words. This would be acceptable for an add/subtract_hours function called with 24 hours, but would not be acceptable for a add/subtract_days function called with 1 day. The reason can be shown here:

Code:
perl -e 'print localtime(1004233600) . "\n" . localtime(1004320000) . "\n"'

Sat Oct 27 18:46:40 2001
Sun Oct 28 17:46:40 2001

From a non-pragmatic (i.e. users') point of view, that seems wrong. Consider a calendar application - it can reasonably be argued and expected that setting a reminder for "1 day from now" should happen at the same time tomorrow. Obviously, as a programmer, I can appreciate that this doesn't "make sense" pragmatically, but as a user of an application, that is the behaviour I would expect. Likewise, adding any other unit larger than hours needs to also take into account that it map overlap these boundary - and possibly even multiple times.

The situation becomes even more vague and imprecise when you get up to to adding months - what should happen if I try to add 1 month from January 29th, 30th, or 31st? I would expect the last day of February - which could be anywhere from 28 days to 31 days. Then leap days come into effect, and must be considered as well (1 month from Feb. 15 should be Mar. 15, regardless of whether or not there is a 29th of February).

However, there is still another problem that needs to be considered where the server is in a time zone where daylight savings has taken effect, while a user may be in a time zone where the daylight savings time either does not happen, or starts at a different time (for example, there is a difference, if I remember correctly, of about 2 weeks between the commencement of daylight savings time in Europe and North America. Using a blanket "time_offset" variable (or two of them that work together, as is done in Gossamer Forum) is more of an easy hack than a real solution. I'd love to be able to add a "select your timezone" feature to Gossamer Forum, rather than making users enter an offset from the server time.

So in short, yes, we do need a date (not unix time) manipulation library. We have discussed this internally and we agree on this fact. So it will be done - the only question is when. Right now, however, our focus is on stabilizing and enhancing our existing products - such as the recent beta releases of Community and the Links SQL shopping cart plugin). So, basically, we agree with you, but at the moment we have more pressing development that we feel deserves a higher priority.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] Date convert In reply to
Date modules are so complicated - rather you than me :(
Quote Reply
Re: [Jagerman] Date convert In reply to
Jason,
I'm grateful for your detailed answer! I really enjoyed it!

Yes, my suggested solution was for clean dates, not for timezoned dates. It is another thing to have functions which can also treat time zones correctly. It's the same thing as you have date_get and date_get_gm functions.

Also you showed me some good examples for those cases, e.g. when doing operations with months. Yes, I aggree. These are exceptions, should be handled in the function. Of course these functions will be more complex than the one I described.

Quote:
So it will be done - the only question is when.
That's good enough for me. I did not expect suggestions as quick change, just suggested that would be fine to have them.
Quote:
So, basically, we agree with you
Thank you very much!
Quote:
at the moment we have more pressing development that we feel deserves a higher priority
Of course developments with higher priority, should be the first!

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Jan 23, 2003, 9:31 AM
Quote Reply
Re: [maxpico] Date convert In reply to
Maxpico,
Thanks for your interest!

Let me tell some words about the date convert script developments... I decided developing the convert date script because I did need it for Links 2.0. You can find my posts in that subject if you search for them in Links 2.0 forums. When I finished the v1.0 version, it was the first perl script ever I done. Cool The 1.0 version did work fine. It was 3 years ago. Since I basically created for myself, I didn't plan to release it. However I saw the public demand for such script, so I decided to improve it, and planned to sell it for developers for reasonable price. At that time there was such date module which would satisfy my needs, and probably other's too. Therefore I developed the 2.0 version. But before I would ever release it I would need to create a module from it. At that time I did not have that knowledge, additionally in the meantime appeared a module which does the same, what my script does.
So I will never finish development of convert date script, because there are already module solutions, which knows much more than my script.
One was Class::Date (can find on CPAN or the dev site here: http://hacks.dlux.hu/Class-Date/). Yeah, he is also Hungarian who did such nice module. Wink
The other was GT::Date, what I currently use. IMO, if GT::Date would not exist, I would use Class::Date for sure.

Now back to my script:
# Description: Converts any date into any other date format (doesn't compute or validate dates, just changing their format)
# Usage: &convert_date ($date, $input_format, $output_format, $language);
# Example: &convert_date ('09-11-1998', 'dd-mm-yyyy', 'yyyy.mmm.dd.', 'English');
# Format strings: yyyy, mmmm, mmm, mm, dddd, ddd, dd, HH, MM, SS
# Notes:
# - input format MUST match the input date format
# - ddd and dddd will be always based (computed) on the given input date
#Changes in v2.0beta3:
# - some small bug changes
#Changes in v2.0beta2:
# - strict usage - hopefully now is mod_perl compatible
# - multi level debug added
# - exception added to convert special partial formats
#Changes in v2.0beta1:
# - completely reworked algorithm for format converting
# - error messages added, error checking works fine as I know
# - now validates dates, prints error message if a date element is not valid
# - added support for yy format. e.g. dd/mm/yy
# Known problems:
# - no support for GMT & UTC time zones (yet)
# - no support for 12-24 hour format converting (yet)
# - no support for language to language converting
And as I mentioned earlier, can convert dates correctly from any format to any format, to unix time and from unix time back to chosen format, also can display date languages in many language, including Hungarian, German, English, French, Russian, Spanish, and probably more automatically, just by setting the correct locale.

As for the used tag format, as Paul suggested a few years ago, probably I would change the formatting to use ``%tag%'' style instead of just using ``tag'' (if I would finish the script).

I will likely never release the script for the public. There are better date converting modules, I mentioned.
Also I would not want, that Paul and maybe other skilled programmers would munching on my 2 year ago used programming solutions... Using my current knowledge I would do it module based, likely better structured, and faster.

Finally I attached the test results file of my script (test_result.txt). See the results yourself.

If you checked the test results and still find the script useful, we can discuss about my date converting script privately.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...