Skip to content

Instantly share code, notes, and snippets.

@bearice
Created August 24, 2011 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bearice/1167445 to your computer and use it in GitHub Desktop.
Save bearice/1167445 to your computer and use it in GitHub Desktop.
strftime for erlang
-module(time_utils).
-author("Bearice Ren <bearice@gmail.com>").
-export([strftime/2]).
strftime(Fmt,{_,_,_}=Time)->
strftime(Fmt,calendar:now_to_datetime(Time));
strftime(Fmt,{_,_}=Time)->
lists:reverse(strftime(str,Fmt,Time,[])).
strftime(str,[$%|Rest],Time,Acc)->
strftime(fmt,Rest,Time,Acc);
strftime(str,[Any|Rest],Time,Acc)->
strftime(str,Rest,Time,[Any|Acc]);
strftime(str,[],_,Acc)->
Acc;
%% %% A literal '%' character.
strftime(fmt,[$%|Rest],Time,Acc)->
strftime(str,Rest,Time,[$%|Acc]);
%% %Z Time zone name (no characters if no time zone exists).
strftime(fmt,[$Z|Rest],Time,Acc)->
strftime(str,Rest,Time,["UTC"|Acc]);
%% %Y Year with century as a decimal number.
strftime(fmt,[$Y|Rest],Time,Acc)->
{{Year,_,_},_} = Time,
strftime(str,Rest,Time,[integer_to_list(Year)|Acc]);
%% %y Year without century as a decimal number [00,99].
strftime(fmt,[$y|Rest],Time,Acc)->
{{Year,_,_},_} = Time,
[_,_|SortYear] = integer_to_list(Year),
strftime(str,Rest,Time,[SortYear|Acc]);
%% %X Locale’s appropriate time representation.
strftime(fmt,[$X|Rest],Time,Acc)->
{_,{H,M,S}} = Time,
strftime(str,Rest,Time,[io_lib:format("~2..0b:~2..0b:~2..0b",[H,M,S])|Acc]);
%% %x Locale’s appropriate date representation.
strftime(fmt,[$x|Rest],Time,Acc)->
{{Y,M,D},_} = Time,
strftime(str,Rest,Time,[io_lib:format("~2..0b-~2..0b-~2..0b",[Y,M,D])|Acc]);
%% %W Week number of the year (Monday as the first day of the week) as a decimal number [00,53].
strftime(fmt,[$W|Rest],Time,Acc)->
{Date,_} = Time,
{_,Week} = calendar:iso_week_number(Date),
strftime(str,Rest,Time,[io_lib:format("~2..0b",[Week-1])|Acc]);
%% %w Weekday as a decimal number [0(Sunday),6].
strftime(fmt,[$w|Rest],Time,Acc)->
{Date,_} = Time,
DoW = calendar:day_of_the_week(Date),
strftime(str,Rest,Time,[io_lib:format("~2..0b",[DoW rem 7])|Acc]);
%% %S Second as a decimal number [00,61]. (2)
strftime(fmt,[$S|Rest],Time,Acc)->
{_,{_,_,Sec}} = Time,
strftime(str,Rest,Time,[io_lib:format("~2..0b",[Sec])|Acc]);
%% %p Locale’s equivalent of either AM or PM. (1)
strftime(fmt,[$p|Rest],Time,Acc)->
{_,{H,_,_}} = Time,
APM = if H > 12 -> "PM"; true -> "AM" end,
strftime(str,Rest,Time,[APM|Acc]);
%% %M Minute as a decimal number [00,59].
strftime(fmt,[$M|Rest],Time,Acc)->
{_,{_,M,_}} = Time,
strftime(str,Rest,Time,[io_lib:format("~2..0b",[M])|Acc]);
%% %m Month as a decimal number [01,12].
strftime(fmt,[$m|Rest],Time,Acc)->
{{_,M,_},_} = Time,
strftime(str,Rest,Time,[io_lib:format("~2..0b",[M])|Acc]);
%% %j Day of the year as a decimal number [001,366].
strftime(fmt,[$j|Rest],Time,Acc)->
{{Y,_,_},_} = Time,
{Days,_} = calendar:time_difference({{Y,1,1},{0,0,0}},Time),
strftime(str,Rest,Time,[io_lib:format("~3..0b",[Days])|Acc]);
%% %I Hour (12-hour clock) as a decimal number [01,12].
strftime(fmt,[$I|Rest],Time,Acc)->
{_,{H,_,_}} = Time,
Hr = if H > 12 -> H-12; true -> H end,
strftime(str,Rest,Time,[io_lib:format("~2..0b",[Hr])|Acc]);
%% %H Hour (24-hour clock) as a decimal number [00,23].
strftime(fmt,[$H|Rest],Time,Acc)->
{_,{H,_,_}} = Time,
strftime(str,Rest,Time,[io_lib:format("~2..0b",[H])|Acc]);
%% %d Day of the month as a decimal number [01,31].
strftime(fmt,[$d|Rest],Time,Acc)->
{{Y,M,_},_} = Time,
{Days,_} = calendar:time_difference({{Y,M,1},{0,0,0}},Time),
strftime(str,Rest,Time,[io_lib:format("~2..0b",[Days])|Acc]);
%% %c Locale’s appropriate date and time representation.
strftime(fmt,[$c|Rest],Time,Acc)->
{{Y,Mo,D},{H,M,S}} = Time,
strftime(str,Rest,Time,[io_lib:format("~2..0b-~2..0b-~2..0b ~2..0b:~2..0b:~2..0b",[Y,Mo,D,H,M,S])|Acc]);
%% %B Locale’s full month name.
strftime(fmt,[$B|Rest],Time,Acc)->
{{_,M,_},_} = Time,
strftime(str,Rest,Time,[long_month_name(M)|Acc]);
%% %b Locale’s abbreviated month name.
strftime(fmt,[$b|Rest],Time,Acc)->
{{_,M,_},_} = Time,
strftime(str,Rest,Time,[sort_month_name(M)|Acc]);
%% %A Locale’s full weekday name.
strftime(fmt,[$A|Rest],Time,Acc)->
{Date,_} = Time,
DoW = calendar:day_of_the_week(Date),
strftime(str,Rest,Time,[long_dow_name(DoW)|Acc]);
%% %a Locale’s abbreviated weekday name.
strftime(fmt,[$a|Rest],Time,Acc)->
{Date,_} = Time,
DoW = calendar:day_of_the_week(Date),
strftime(str,Rest,Time,[sort_dow_name(DoW)|Acc]).
sort_month_name(1) -> "Jan";
sort_month_name(2) -> "Feb";
sort_month_name(3) -> "Mar";
sort_month_name(4) -> "Apr";
sort_month_name(5) -> "May";
sort_month_name(6) -> "Jun";
sort_month_name(7) -> "Jul";
sort_month_name(8) -> "Aug";
sort_month_name(9) -> "Sep";
sort_month_name(10) -> "Oct";
sort_month_name(11) -> "Nov";
sort_month_name(12) -> "Dec".
long_month_name(1) -> "January";
long_month_name(2) -> "February";
long_month_name(3) -> "March";
long_month_name(4) -> "April";
long_month_name(5) -> "May";
long_month_name(6) -> "June";
long_month_name(7) -> "July";
long_month_name(8) -> "August";
long_month_name(9) -> "September";
long_month_name(10) -> "October";
long_month_name(11) -> "November";
long_month_name(12) -> "December".
sort_dow_name(7) -> "Sun";
sort_dow_name(1) -> "Mon";
sort_dow_name(2) -> "Tue";
sort_dow_name(3) -> "Wed";
sort_dow_name(4) -> "Thu";
sort_dow_name(5) -> "Fri";
sort_dow_name(6) -> "Sat".
long_dow_name(7) -> "Sunday";
long_dow_name(1) -> "Monday";
long_dow_name(2) -> "Tuesday";
long_dow_name(3) -> "Wednesday";
long_dow_name(4) -> "Thursday";
long_dow_name(5) -> "Friday";
long_dow_name(6) -> "Saturaday".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment