Log in |
Custom and international time and date formatsVersion 0.2 This howto explains how to create custom and international formats to insert time and date values in Zope. I gathered this information from the [email protected] mailing list. Feel free to get in touch about this document. Version history
Simple timestampsTo enter the edit time for an objet, use: <dtml-var bobobase_modification_time> To enter the current time in Zope, use: <dtml-var ZopeTime> In both cases the result is a long timestamp in GMT format. Example: 1999/10/22 13:01:33.059 GMT+1 C-style format stringsIf you want another format, you can use C-style format strings. Example: <dtml-var bobobase_modification_time fmt="%d/%m/%Y - %H:%M"> You get the following results: 22/10/1999 - 11:01 Here is a table of useful C-style time and date formats. I copied it from a Linux reference document (I can't remember which one) for the C strftime function. Note that the output is GMT-based. So you cannot use these codes directly to format date and time values for say European timezones. %a An abbreviation for the day of the week. %A The full name for the day of the week. %b An abbreviation for the month name. %B The full name of the month. %c A string representing the complete date and time; on my computer it's in the form: 10/22/99 19:03:23 %d The day of the month, formatted with two digits. %H The hour (on a 24-hour clock), formatted with two digits. %I The hour (on a 12-hour clock), formatted with two digits. %j The count of days in the year, formatted with three digits (from 001 to 366). %m The month number, formatted with two digits. %M The minute, formatted with two digits. %p Either AM or PM as appropriate. %S The second, formatted with two digits. %U The week number, formatted with two digits (from 00 to 53; week number 1 is taken as beginning with the first Sunday in a year). See also %W. %w A single digit representing the day of the week: Sunday is day 0. %W Another version of the week number: like %U, but counting week 1 as beginning with the first Monday in a year. %x A string representing the complete date; on my computer it's in the format 10/22/99. %X A string representing the full time of day (hours, minutes, and seconds), in a format like the following example: 13:13:13 %y The last two digits of the year. %Y The full year, formatted with four digits to include the century. %Z Defined by ANSI C as eliciting the time zone, if available; it is not available in this implementation (which accepts %Z but generates no output for it). %% A single character, %. Note you can also use the following syntax: <dtml-var "bobobase_modification_time().strftime('%d/%m/%Y - %H:%M')"> For more information about these formats, see the Zope Quick Reference Guide (section about the var tag). The toZone methodThe toZone('myTimeZone') method can be used to convert time/date values to a specific timezone. myTimeZone is a string that holds the name of the timezone. Example: <dtml-var "bobobase_modification_time().toZone('US/Indiana-Starke')"> The output is: 1999/10/22 12:35:32.481 US/Indiana-Starke The timezone names are defined in the Zope_dir\lib\python\DateTime\DateTimeZone.py file. I couldn't find one for most European timezones (Zope 2.0.1). Chris Allen has provided files to support Australian timezone names. You can also add the C-formats mentioned above. Example: <dtml-var "bobobase_modification_time(). toZone('US/Indiana-Starke').strftime('%d/%m/ %Y - %H:%M')"> Or <dtml-var "bobobase_modification_time(). toZone('US/Indiana-Starke')" fmt="%d/%m/%Y - %H:%M"> Correcting a date or time valueYou can add a value to a date to create a later date. You can also substract a value to create a sooner date. Example: <dtml-var "ZopeTime() + 2"> The result date is 2 days from now, i.e. "1999/10/24 12:12:14.932 GMT+1" at the time of writing. Metropolitan French time is GMT + 2 hours during summer time. 2 hours are 0.0833 day. So I guess I could use the following code to get a correct French date format: <dtml-var "bobobase_modification_time() + 0.0833" fmt="%d/%m/%Y - %H:%M"> This works. However, it is quite ugly and it will break when we change to winter time. I'd need to store the correction value in a global variable. Build-in functionsMartijn Pieters provided a better solution because AFAIK it doesn't need to be updated for winter time: <dtml-with bobobase_modification_time> <dtml-var "'%s/%s/%i - %s' % (dd(), mm(), year(), TimeMinutes())"> </dtml-with> The output is: 22/10/1999 - 20:31 This is a Python expression. The 1st, 2nd and 4th functions return a string value. That's why we use the %s placeholder in the format. The 3rd function returns an integer. Hence the %i placeholder. To customize this example for your own purpose you may need to modify or add placeholders. Example: <dtml-with bobobase_modification_time> <dtml-var "'This is %s, %s/%s/%i, %s' % (Day(), mm(), dd(), year(), TimeMinutes())"> </dtml-with> The output is: This is Friday, 10/22/1999, 20:44 For more information about the dd(), mm(), year(), TimeMinutes() and similar functions, see the Zope_dir\lib\python\DateTime\DateTime.html document in your Zope directory. See also the Zope Quick Reference Guide (DateTime section) The complete expression is a bit complex. You could store it in a DTML method at the root of your Zope server and call it from documents in subdirectories. Example: <dtml-var myDateTime>The output is: 22/10/1999 - 20:50 I guess you could also write a small Python external method to easily store custom date formats. I haven't tried that yet. If you have code examples to offer feel free to send them over for inclusion in this document. |