Aller au contenu principal

datetime

Manage date.

DATETIME

NAME

datetime - Manages date.

SYNOPSIS

datetime([lg: "fr|en"][, locale: "…"][, extended: true|false][, date: "yyyy-mm-dd hh:mm:ss"][, op: "op…"][, format: "…"][, gmt: true|false][, offset: integer][, table: string])

DESCRIPTION

This function is used to read a date, perform some operations and output it in a specific format.

PARAMETERS

All parameters are optionnal. If no parameter is given, the function will output the current date of the day (ex: "2021-09-01 08:30:31")

date (string): The input date. Supports four formats:

  • yyyy-mm-dd hh:mm:ss
  • yyyy-mm-dd
  • Unix in secs since 1970
  • Thu, 19 Apr 2018 11:00:49 +0000.

op (string): Operation is to modify the input date. It is formatted with a sign (+, -), followed by a number then by a single character which can be:

  • s : second
  • m : minut
  • h : hour
  • d : day
  • w : week
  • b : month
  • y : year

ex: "+2d"

locale (string): for the locale. if `locale` is defined, the `table` parameter will be ignored. This is available in version 5.159.

ex: "fr_CA.utf8"

lg (string): for the language. Note that when using this parameter, you can use the parameter `table` to specify which language table the function should be using. This parameter can only be used inside a framework where the langauge tables are available. (ex: a resource or a page in extenso, not as a seperate sncode script)

ex: "fr"

table (string): is used to specify the table where the languages are defined. Starting with version 5.146, the function will try to guess which table to use if it's not specified 

  • sed_site_lg
  • sn_language

ex: "sed_site_lg"

gmt (bool): If `gmt` is set to true, it will return the current time with timezone GMT. To use this parameter, no other parameter should be used otherwise the `gmt` parameter will be ignored. Available in Sncode +5.148 

offset (int): If offset is defined and contains a number of second, the time will be return with this offset from GMT. Act like if parametre `gmt` is true. Don't put `gmt` otherwise the offest parameter will be ignored. Available in Sncode +5.148

extended (bool): Whether to use the extended set of formats or not. `extended: true` is required to use those formats:

  • %Sednove1 
  • %Sednove2
  • %Sednove3
  • %SNlastdayofmonth

format (string): The format supports the usual strftime format, which can be:

  • %a : The abbreviated weekday name according to the current locale.
  • %A : The full weekday name according to the current locale.
  • %b : The abbreviated month name according to the current locale.
  • %B : The full month name according to the current locale.
  • %c : The preferred date and time representation for the current locale.
  • %C : The century number (year/100) as a 2-digit integer. (SU)
  • %d : The day of the month as a decimal number (range 01 to 31).
  • %D : Equivalent to %m/%d/%y. (Yecch — for Americans only. Americans should note that in other countries %d/%m/%y is rather common. This means that in international context this format is ambigu- ous and should not be used.) (SU)
  • %e : Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (SU)
  • %E : Modifier: use alternative format, see below. (SU)
  • %F : Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
  • %G : The ISO 8601 year with century as a decimal number. The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %y, except that if the ISO week number belongs to the previous or next year, that year is used instead. (TZ)
  • %g : Like %G, but without century, i.e., with a 2-digit year (00-99). (TZ)
  • %h : Equivalent to %b. (SU)
  • %H : The hour as a decimal number using a 24-hour clock (range 00 to 23).
  • %I : The hour as a decimal number using a 12-hour clock (range 01 to 12).
  • %j : The day of the year as a decimal number (range 001 to 366).
  • %k : The hour (24-hour clock) as a decimal number (range 0 to 23 single digits are preceded by a blank. (See also %H.) (TZ)
  • %l : The hour (12-hour clock) as a decimal number (range 1 to 12 single digits are preceded by a blank. (See also %I.) (TZ)
  • %m : The month as a decimal number (range 01 to 12).
  • %M : The minute as a decimal number (range 00 to 59).
  • %n : A newline character. (SU)
  • %O : Modifier: use alternative format, see below. (SU)
  • %p : Either ‘AM’ or ‘PM’ according to the given time value, or the corresponding strings for the current locale. Noon is treated as ‘pm’ and midnight as ‘am’.
  • %P : Like %p but in lowercase: ‘am’ or ‘pm’ or a corresponding string for the current locale. (GNU)
  • %r : The time in a.m. or p.m. notation. In the POSIX locale this is equivalent to ‘%I:%M:%S %p’. (SU)
  • %R : The time in 24-hour notation (%H:%M). (SU) For a version includ- ing the seconds, see %T below.
  • %s : The number of seconds since the Epoch, i.e., since 1970-01-01 00:00:00 UTC. (TZ)
  • %S : The second as a decimal number (range 00 to 60). (The range is up to 60 to allow for occasional leap seconds.)
  • %t : A tab character. (SU)
  • %T : The time in 24-hour notation (%H:%M:%S). (SU)
  • %u : The day of the week as a decimal, range 1 to 7, Monday being 1. See also %w. (SU)
  • %U : The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01. See also %V and %W.
  • %V : The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week. See also %U and %W. (SU)
  • %w : The day of the week as a decimal, range 0 to 6, Sunday being 0. See also %u.
  • %W : The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01.
  • %x : The preferred date representation for the current locale without the time.
  • %X : The preferred time representation for the current locale without the date.
  • %y : The year as a decimal number without a century (range 00 to 99).
  • %Y : The year as a decimal number including the century.
  • %z : The time-zone as hour offset from GMT. Required to emit RFC 822-conformant dates (using "%a, %d %b %Y %H:%M:%S %z"). (GNU)
  • %Z : The time zone or name or abbreviation.
  • %+ : The date and time in date(1) format. (TZ) (Not supported in glibc2.)
  • %% : A literal ‘%’ character.
  • %Sednove1 : return the month and the day according to the locale like September 1st
  • %Sednove2 : return the date with the format Sun, 23 Sep 2013 00:51:16 GMT. This doesn't support multi-lingual
  • %Sednove3 : return the day of the month according to the locale like 1st or 1er in french
  • %SNlastdayofmonth : return the last day of the month as a decimal number (28, 30, 31)

Examples

datetime(); // 2021-09-01 (The current date of the day)

datetime(date:"2013-01"); // 2013-01-01 00:00:00

datetime(date:"2013-01-01"); // 2013-01-01 00:00:00

datetime(date: "2013-01-01 10:10:10"); // 2013-01-01 10:10:10

datetime(date: "1714-04-30", op: "+2d"); // 1714-05-02 00:00:00

datetime(date: "2014-04-30", op: "-13b"); // 2013-03-30 00:00:00

datetime(date: "2014-04-30", op: "-13b,+2d"); // 2013-04-01 00:00:00

datetime(date: "2013-12-05", locale: "fr_CA.utf8", format: "%B"); // décembre

datetime(date: "2011-02-03", locale: "fr_CA.UTF-8", format: "%b"); // fév

// lg parameter - Must be in an environement where a db is available. Parameter `table` is optional if sed_site_lg is the target table (version 5.146)
datetime(date: "2018-02-28", lg: "fr", format: "%B"); // février

// Extended formats
datetime(date: "2012-04-01", lg: "fr", extended: true, format: "%Sednove1"); // 1er avril
datetime(date: "2012-04-01", lg: "en", extended: true, format: "%Sednove1"); // April 1st

datetime(date: "2012-04-01", extended: true, format: "%Sednove2"); // Sun, 01 Apr 2012 04:00:00 GMT

datetime(date: "2012-04-01", lg: "fr", extended: true, format: "%Sednove3"); // 1er
datetime(date: "2012-04-01", lg: "en", extended: true, format: "%Sednove3"); // 1st

datetime(date: "2012-04-01", extended: true, format: "%SNlastdayofmonth"); // 30

// if we are at a current time of 2021-09-02 09:26:46 on a server that is -04:00 from Greenwich Mean Time, it will output
datetime(gmt: true); // 2021-09-02 14:34:02
datetime(offset: 120); // 2021-09-02 14:36:02 <- gmt:true is implicit 


Output of all format

       - %a     The abbreviated weekday name according to the current locale.
Sun

       - %A     The full weekday name according to the current locale.
Sunday

       - %b     The abbreviated month name according to the current locale.
Sep

       - %B     The full month name according to the current locale.
September

       - %c     The preferred date  and  time  representation  for  the  current locale.
Sun Sep 22 09:06:42 2013

       - %C     The century number (year/100) as a 2-digit integer. (SU)
20

       - %d     The day of the month as a decimal number (range 01 to 31).
22

       - %D     Equivalent  to %m/%d/%y. (Yecch — for Americans only.  Americans should note that in other countries %d/%m/%y is  rather  common.
                This  means that in international context this format is ambiguous and should not be used.) (SU)
09/22/13

       - %e     Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (SU)
22

       - %E     Modifier: use alternative format, see below. (SU)
%E

       - %F     Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
2013-09-22

       - %G     The ISO 8601 year with century as a decimal number.  The 4-digit year corresponding to the ISO week number (see  %V).   This  has
                the  same  format  and  value as %y, except that if the ISO week number belongs to the previous or next year, that year  is  used
                instead. (TZ)
2013

       - %g     Like %G, but without century, i.e., with a 2-digit year (00-99).
               (TZ)
13

       - %h     Equivalent to %b. (SU)
Sep

       - %H     The hour as a decimal number using a 24-hour clock (range 00  to 23).
09

       - %I     The  hour as a decimal number using a 12-hour clock (range 01 to 12).
09

       - %j     The day of the year as a decimal number (range 001 to 366).
265

       - %k     The hour (24-hour clock) as a decimal number (range  0  to  23 single digits are preceded by a blank. (See also %H.) (TZ)
 9

       - %l    The  hour  (12-hour  clock) as a decimal number (range 1 to 12 single digits are preceded by a blank. (See also %I.) (TZ)
 9

       - %m     The month as a decimal number (range 01 to 12).
09

       - %M     The minute as a decimal number (range 00 to 59).
06

       - %n     A newline character. (SU)



       - %O     Modifier: use alternative format, see below. (SU)
%O

       - %p     Either ‘AM’ or ‘PM’ according to the given time  value,  or  the
                corresponding  strings  for the current locale.  Noon is treated as ‘pm’ and midnight 
                as ‘am’.
AM

       - %P     Like %p but in lowercase: ‘am’ or ‘pm’ or a corresponding string
                for the current locale. (GNU)
am

       - %r     The  time in a.m. or p.m. notation.  In the POSIX locale this is equivalent to ‘%I:%M:%S %p’. (SU)
09:06:42 AM

       - %R     The time in 24-hour notation (%H:%M). (SU) For a version including the seconds, see %T below.
09:06

       - %s     The  number  of  seconds since the Epoch, i.e., since 1970-01-01 00:00:00 UTC. (TZ)
1379855202

       - %S     The second as a decimal number (range 00 to 60).  (The range  is up to 60 to allow for occasional leap seconds.)
42

       - %t     A tab character. (SU)


       - %T     The time in 24-hour notation (%H:%M:%S). (SU)
09:06:42

       - %u     The  day of the week as a decimal, range 1 to 7, Monday being 1.
                See also %w. (SU)
7

       - %U     The week number of the current year as a decimal  number,  range 00  to  53,  starting  with the first Sunday as the first day of
                week 01. See also %V and %W.
38

       - %V     The ISO 8601:1988 week number of the current year as  a  decimal number,  range 01 to 53, where week 1 is the first week that has
                at least 4 days in the current year,  and  with  Monday  as  the first day of the week. See also %U and %W. (SU)
38

       - %w     The  day of the week as a decimal, range 0 to 6, Sunday being 0.
                See also %u.
0

       - %W     The week number of the current year as a decimal  number,  range 00 to 53, starting  with the first Monday as the first day of
                week 01.
37

       - %x     The preferred date representation for the current locale without the time.
09/22/13

       - %X     The preferred time representation for the current locale without the date.
09:06:42

       - %y     The year as a decimal number without a century (range 00 to 99).
13

       - %Y     The year as a decimal number including the century.
2013

       - %z     The  time-zone  as  hour  offset  from  GMT.   Required  to emit
                RFC 822-conformant dates (using "%a, %d  %b  %Y  %H:%M:%S  %z").
               (GNU)
-0400

       - %Z     The time zone or name or abbreviation.
EDT

       - %+     The  date  and  time  in  date(1) format. (TZ) (Not supported in glibc2.)
%+

With the parameter `extended: true`

       - %Sednove1 
September 22nd

       - %Sednove2 
Sun, 23 Sep 2013 00:51:16 GMT

       - %Sednove3
22nd

       - %SNlastdayofmonth (for a month that has 31 days) 
31

 

 

 

 

SEE ALSO

AUTHOR

Written by Pierre Laplante and Caroline Laplante, <laplante@sednove.com>

MODIFICATIONS

1.0 2014-09-09 21:24:14 laplante@sednove.com

1.1 2018-04-21 laplante@sednove.com Add support for parsing date in format Thu, 19 Apr 2018 11:00:49 +0000 from version 5.104

1.2 2021-01-20 laplante@sednove.com Add support to test for table sed_site_lg

1.3 2021-08-26 laplante@sednove.com Add support to specify locale withiout reading a table.

Edit

© 2024 extenso Inc. All rights reserved.